blob: 96ee2f037de8327ac2868b9763ebe2e6d2c49e74 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090019import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
satoke7c6998e2011-06-03 17:57:59 +090031import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090033import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35import android.app.ActivityManagerNative;
36import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090037import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090038import android.app.Notification;
39import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070040import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.DialogInterface.OnCancelListener;
46import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090047import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070054import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.Resources;
56import android.content.res.TypedArray;
57import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080058import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
63import android.os.IInterface;
64import android.os.Message;
65import android.os.Parcel;
66import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080067import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090072import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090074import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090076import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090077import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.PrintWriterPrinter;
79import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090080import android.util.Slog;
81import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090083import android.view.LayoutInflater;
84import android.view.View;
85import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090087import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.inputmethod.InputBinding;
89import android.view.inputmethod.InputMethod;
90import android.view.inputmethod.InputMethodInfo;
91import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090092import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090093import android.widget.ArrayAdapter;
94import android.widget.RadioButton;
95import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
satoke7c6998e2011-06-03 17:57:59 +090097import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090099import java.io.FileInputStream;
100import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.IOException;
102import java.io.PrintWriter;
103import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900104import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900105import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900107import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import java.util.List;
satok913a8922010-08-26 21:53:41 +0900109import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111/**
112 * This class provides a system service that manages input methods.
113 */
114public class InputMethodManagerService extends IInputMethodManager.Stub
115 implements ServiceConnection, Handler.Callback {
116 static final boolean DEBUG = false;
117 static final String TAG = "InputManagerService";
118
119 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900120 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900121 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900122 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 static final int MSG_UNBIND_INPUT = 1000;
125 static final int MSG_BIND_INPUT = 1010;
126 static final int MSG_SHOW_SOFT_INPUT = 1020;
127 static final int MSG_HIDE_SOFT_INPUT = 1030;
128 static final int MSG_ATTACH_TOKEN = 1040;
129 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 static final int MSG_START_INPUT = 2000;
132 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 static final int MSG_UNBIND_METHOD = 3000;
135 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800138
satokf9f01002011-05-19 21:31:50 +0900139 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
140
satokab751aa2010-09-14 19:17:36 +0900141 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900142 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900143 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
144 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900145 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900146 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
147 "EnabledWhenDefaultIsNotAsciiCapable";
148 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok4e4569d2010-11-19 18:45:53 +0900149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800151 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900153 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final IWindowManager mIWindowManager;
156 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900157 private final InputMethodFileManager mFileManager;
satok688bd472012-02-09 20:09:17 +0900158 private final InputMethodAndSubtypeListManager mImListManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 // All known input methods. mMethodMap also serves as the global
163 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900164 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
165 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900166 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
167 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800168
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700169 // Used to bring IME service up to visible adjustment while it is being shown.
170 final ServiceConnection mVisibleConnection = new ServiceConnection() {
171 @Override public void onServiceConnected(ComponentName name, IBinder service) {
172 }
173
174 @Override public void onServiceDisconnected(ComponentName name) {
175 }
176 };
177 boolean mVisibleBound = false;
178
satok7cfc0ed2011-06-20 21:29:36 +0900179 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700180 private NotificationManager mNotificationManager;
181 private KeyguardManager mKeyguardManager;
182 private StatusBarManagerService mStatusBar;
183 private Notification mImeSwitcherNotification;
184 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900185 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900186 private boolean mNotificationShown;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 class SessionState {
189 final ClientState client;
190 final IInputMethod method;
191 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 @Override
194 public String toString() {
195 return "SessionState{uid " + client.uid + " pid " + client.pid
196 + " method " + Integer.toHexString(
197 System.identityHashCode(method))
198 + " session " + Integer.toHexString(
199 System.identityHashCode(session))
200 + "}";
201 }
202
203 SessionState(ClientState _client, IInputMethod _method,
204 IInputMethodSession _session) {
205 client = _client;
206 method = _method;
207 session = _session;
208 }
209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 class ClientState {
212 final IInputMethodClient client;
213 final IInputContext inputContext;
214 final int uid;
215 final int pid;
216 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 boolean sessionRequested;
219 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 @Override
222 public String toString() {
223 return "ClientState{" + Integer.toHexString(
224 System.identityHashCode(this)) + " uid " + uid
225 + " pid " + pid + "}";
226 }
227
228 ClientState(IInputMethodClient _client, IInputContext _inputContext,
229 int _uid, int _pid) {
230 client = _client;
231 inputContext = _inputContext;
232 uid = _uid;
233 pid = _pid;
234 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
235 }
236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 final HashMap<IBinder, ClientState> mClients
239 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700242 * Set once the system is ready to run third party code.
243 */
244 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * Id of the currently selected input method.
248 */
249 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * The current binding sequence number, incremented every time there is
253 * a new bind performed.
254 */
255 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The client that is currently bound to an input method.
259 */
260 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700263 * The last window token that gained focus.
264 */
265 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * The input context last provided by the current client.
269 */
270 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 /**
273 * The attributes last provided by the current client.
274 */
275 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The input method ID of the input method service that we are currently
279 * connected to or in the process of connecting to.
280 */
281 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
satokab751aa2010-09-14 19:17:36 +0900284 * The current subtype of the current input method.
285 */
286 private InputMethodSubtype mCurrentSubtype;
287
satok4e4569d2010-11-19 18:45:53 +0900288 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900289 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
290 mShortcutInputMethodsAndSubtypes =
291 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900292
293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 * Set to true if our ServiceConnection is currently actively bound to
295 * a service (whether or not we have gotten its IBinder back yet).
296 */
297 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Set if the client has asked for the input method to be shown.
301 */
302 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * Set if we were explicitly told to show the input method.
306 */
307 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * Set if we were forced to be shown.
311 */
312 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * Set if we last told the input method to show itself.
316 */
317 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * The Intent used to connect to the current input method.
321 */
322 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * The token we have made for the currently active input method, to
326 * identify it in the future.
327 */
328 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * If non-null, this is the input method service we are currently connected
332 * to.
333 */
334 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
337 * Time that we last initiated a bind to the input method, to determine
338 * if we should try to disconnect and reconnect to it.
339 */
340 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
343 * Have we called mCurMethod.bindInput()?
344 */
345 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 /**
348 * Currently enabled session. Only touched by service thread, not
349 * protected by a lock.
350 */
351 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 /**
354 * True if the screen is on. The value is true initially.
355 */
356 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
Joe Onorato857fd9b2011-01-27 15:08:35 -0800358 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
359 int mImeWindowVis;
360
Ken Wakasa05dbb652011-08-22 15:22:43 +0900361 private AlertDialog.Builder mDialogBuilder;
362 private AlertDialog mSwitchingDialog;
363 private InputMethodInfo[] mIms;
364 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 class SettingsObserver extends ContentObserver {
367 SettingsObserver(Handler handler) {
368 super(handler);
369 ContentResolver resolver = mContext.getContentResolver();
370 resolver.registerContentObserver(Settings.Secure.getUriFor(
371 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900372 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900373 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
374 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900375 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 @Override public void onChange(boolean selfChange) {
379 synchronized (mMethodMap) {
380 updateFromSettingsLocked();
381 }
382 }
383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
386 @Override
387 public void onReceive(Context context, Intent intent) {
388 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
389 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900390 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
392 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900393 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700394 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
395 hideInputMethodMenu();
396 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800398 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400
401 // Inform the current client of the change in active status
402 try {
403 if (mCurClient != null && mCurClient.client != null) {
404 mCurClient.client.setActive(mScreenOn);
405 }
406 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800407 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 + mCurClient.pid + " uid " + mCurClient.uid);
409 }
410 }
411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800412
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800413 class MyPackageMonitor extends PackageMonitor {
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800416 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800418 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
420 final int N = mMethodList.size();
421 if (curInputMethodId != null) {
422 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800423 InputMethodInfo imi = mMethodList.get(i);
424 if (imi.getId().equals(curInputMethodId)) {
425 for (String pkg : packages) {
426 if (imi.getPackageName().equals(pkg)) {
427 if (!doit) {
428 return true;
429 }
satok723a27e2010-11-11 14:58:11 +0900430 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800431 chooseNewDefaultIMELocked();
432 return true;
433 }
434 }
435 }
436 }
437 }
438 }
439 return false;
440 }
441
442 @Override
443 public void onSomePackagesChanged() {
444 synchronized (mMethodMap) {
445 InputMethodInfo curIm = null;
446 String curInputMethodId = Settings.Secure.getString(mContext
447 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
448 final int N = mMethodList.size();
449 if (curInputMethodId != null) {
450 for (int i=0; i<N; i++) {
451 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900452 final String imiId = imi.getId();
453 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 curIm = imi;
455 }
satoke7c6998e2011-06-03 17:57:59 +0900456
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900458 if (isPackageModified(imi.getPackageName())) {
459 mFileManager.deleteAllInputMethodSubtypes(imiId);
460 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 if (change == PACKAGE_TEMPORARY_CHANGE
462 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800463 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 + imi.getComponent());
465 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800469
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 buildInputMethodListLocked(mMethodList, mMethodMap);
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800473
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 int change = isPackageDisappearing(curIm.getPackageName());
476 if (change == PACKAGE_TEMPORARY_CHANGE
477 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 ServiceInfo si = null;
479 try {
480 si = mContext.getPackageManager().getServiceInfo(
481 curIm.getComponent(), 0);
482 } catch (PackageManager.NameNotFoundException ex) {
483 }
484 if (si == null) {
485 // Uh oh, current input method is no longer around!
486 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800487 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900488 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800490 changed = true;
491 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800492 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900493 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800494 }
495 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800496 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 }
satokab751aa2010-09-14 19:17:36 +0900498
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800499 if (curIm == null) {
500 // We currently don't have a default input method... is
501 // one now available?
502 changed = chooseNewDefaultIMELocked();
503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 if (changed) {
506 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 }
509 }
510 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800511
Jean Chalarde0d32a62011-10-20 20:36:07 +0900512 private static class MethodCallback extends IInputMethodCallback.Stub {
513 private final IInputMethod mMethod;
514 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
Jean Chalarde0d32a62011-10-20 20:36:07 +0900516 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900518 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800520
satoke7c6998e2011-06-03 17:57:59 +0900521 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public void finishedEvent(int seq, boolean handled) throws RemoteException {
523 }
524
satoke7c6998e2011-06-03 17:57:59 +0900525 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900527 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800530
Dianne Hackborn661cd522011-08-22 00:26:20 -0700531 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800533 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 mHandler = new Handler(this);
535 mIWindowManager = IWindowManager.Stub.asInterface(
536 ServiceManager.getService(Context.WINDOW_SERVICE));
537 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900538 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 public void executeMessage(Message msg) {
540 handleMessage(msg);
541 }
542 });
satok7cfc0ed2011-06-20 21:29:36 +0900543
satok7cfc0ed2011-06-20 21:29:36 +0900544 mImeSwitcherNotification = new Notification();
545 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
546 mImeSwitcherNotification.when = 0;
547 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
548 mImeSwitcherNotification.tickerText = null;
549 mImeSwitcherNotification.defaults = 0; // please be quiet
550 mImeSwitcherNotification.sound = null;
551 mImeSwitcherNotification.vibrate = null;
552 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900553 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900554
555 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900556
satoke7c6998e2011-06-03 17:57:59 +0900557 synchronized (mMethodMap) {
558 mFileManager = new InputMethodFileManager(mMethodMap);
559 }
satok688bd472012-02-09 20:09:17 +0900560 mImListManager = new InputMethodAndSubtypeListManager(context, this);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800562 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 IntentFilter screenOnOffFilt = new IntentFilter();
565 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
566 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700567 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800569
satok7cfc0ed2011-06-20 21:29:36 +0900570 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900571
satokd87c2592010-09-29 11:52:06 +0900572 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900573 mSettings = new InputMethodSettings(
574 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900576 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
satokd87c2592010-09-29 11:52:06 +0900578 if (TextUtils.isEmpty(Settings.Secure.getString(
579 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900581 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
583 try {
satokd87c2592010-09-29 11:52:06 +0900584 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 imi.getPackageName(), 0).getResources();
586 if (res.getBoolean(imi.getIsDefaultResourceId())) {
587 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590 } catch (PackageManager.NameNotFoundException ex) {
591 } catch (Resources.NotFoundException ex) {
592 }
593 }
594 }
satokd87c2592010-09-29 11:52:06 +0900595 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900596 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800597 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900600 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 mSettingsObserver = new SettingsObserver(mHandler);
605 updateFromSettingsLocked();
606 }
607
608 @Override
609 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
610 throws RemoteException {
611 try {
612 return super.onTransact(code, data, reply, flags);
613 } catch (RuntimeException e) {
614 // The input method manager only throws security exceptions, so let's
615 // log all others.
616 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
619 throw e;
620 }
621 }
622
Dianne Hackborn661cd522011-08-22 00:26:20 -0700623 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700624 synchronized (mMethodMap) {
625 if (!mSystemReady) {
626 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700627 mKeyguardManager = (KeyguardManager)
628 mContext.getSystemService(Context.KEYGUARD_SERVICE);
629 mNotificationManager = (NotificationManager)
630 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
631 mStatusBar = statusBar;
632 statusBar.setIconVisibility("ime", false);
633 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900634 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
635 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700636 try {
637 startInputInnerLocked();
638 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800639 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700640 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700641 }
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
satok15452a42011-10-28 17:58:28 +0900645 private void setImeWindowVisibilityStatusHiddenLocked() {
646 mImeWindowVis = 0;
647 updateImeWindowStatusLocked();
648 }
649
satok3afd6c02011-11-18 08:38:19 +0900650 private void refreshImeWindowVisibilityLocked() {
651 final Configuration conf = mRes.getConfiguration();
652 final boolean haveHardKeyboard = conf.keyboard
653 != Configuration.KEYBOARD_NOKEYS;
654 final boolean hardKeyShown = haveHardKeyboard
655 && conf.hardKeyboardHidden
656 != Configuration.HARDKEYBOARDHIDDEN_YES;
657 final boolean isScreenLocked = mKeyguardManager != null
658 && mKeyguardManager.isKeyguardLocked()
659 && mKeyguardManager.isKeyguardSecure();
660 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
661 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
662 updateImeWindowStatusLocked();
663 }
664
satok15452a42011-10-28 17:58:28 +0900665 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900666 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700667 }
668
satoke7c6998e2011-06-03 17:57:59 +0900669 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 public List<InputMethodInfo> getInputMethodList() {
671 synchronized (mMethodMap) {
672 return new ArrayList<InputMethodInfo>(mMethodList);
673 }
674 }
675
satoke7c6998e2011-06-03 17:57:59 +0900676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public List<InputMethodInfo> getEnabledInputMethodList() {
678 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900679 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 }
681 }
682
satokbb4aa062011-01-19 21:40:27 +0900683 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
684 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
685 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
686 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
687 for (InputMethodInfo imi: getEnabledInputMethodList()) {
688 enabledInputMethodAndSubtypes.put(
689 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
690 }
691 return enabledInputMethodAndSubtypes;
692 }
693
694 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
695 boolean allowsImplicitlySelectedSubtypes) {
696 if (imi == null && mCurMethodId != null) {
697 imi = mMethodMap.get(mCurMethodId);
698 }
satok7265d9b2011-02-14 15:47:30 +0900699 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900700 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900701 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900702 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900703 }
satok7265d9b2011-02-14 15:47:30 +0900704 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900705 }
706
satoke7c6998e2011-06-03 17:57:59 +0900707 @Override
satok16331c82010-12-20 23:48:46 +0900708 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
709 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900710 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900711 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900712 }
713 }
714
satoke7c6998e2011-06-03 17:57:59 +0900715 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 public void addClient(IInputMethodClient client,
717 IInputContext inputContext, int uid, int pid) {
718 synchronized (mMethodMap) {
719 mClients.put(client.asBinder(), new ClientState(client,
720 inputContext, uid, pid));
721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
satoke7c6998e2011-06-03 17:57:59 +0900724 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 public void removeClient(IInputMethodClient client) {
726 synchronized (mMethodMap) {
727 mClients.remove(client.asBinder());
728 }
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 void executeOrSendMessage(IInterface target, Message msg) {
732 if (target.asBinder() instanceof Binder) {
733 mCaller.sendMessage(msg);
734 } else {
735 handleMessage(msg);
736 msg.recycle();
737 }
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700740 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 + mCurClient.client.asBinder());
744 if (mBoundToMethod) {
745 mBoundToMethod = false;
746 if (mCurMethod != null) {
747 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
748 MSG_UNBIND_INPUT, mCurMethod));
749 }
750 }
751 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
752 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
753 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 // Call setActive(false) on the old client
756 try {
757 mCurClient.client.setActive(false);
758 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800759 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 + mCurClient.pid + " uid " + mCurClient.uid);
761 }
762 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
The Android Open Source Project10592532009-03-18 17:39:46 -0700764 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 private int getImeShowFlags() {
769 int flags = 0;
770 if (mShowForced) {
771 flags |= InputMethod.SHOW_FORCED
772 | InputMethod.SHOW_EXPLICIT;
773 } else if (mShowExplicitlyRequested) {
774 flags |= InputMethod.SHOW_EXPLICIT;
775 }
776 return flags;
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 private int getAppShowFlags() {
780 int flags = 0;
781 if (mShowForced) {
782 flags |= InputMethodManager.SHOW_FORCED;
783 } else if (!mShowExplicitlyRequested) {
784 flags |= InputMethodManager.SHOW_IMPLICIT;
785 }
786 return flags;
787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
Dianne Hackborn7663d802012-02-24 13:08:49 -0800789 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 if (!mBoundToMethod) {
791 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
792 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
793 mBoundToMethod = true;
794 }
795 final SessionState session = mCurClient.curSession;
796 if (initial) {
797 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
798 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
799 } else {
800 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
801 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
802 }
803 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800804 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800805 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800807 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800811 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 // If no method is currently selected, do nothing.
813 if (mCurMethodId == null) {
814 return mNoBinding;
815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 ClientState cs = mClients.get(client.asBinder());
818 if (cs == null) {
819 throw new IllegalArgumentException("unknown client "
820 + client.asBinder());
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 try {
824 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
825 // Check with the window manager to make sure this client actually
826 // has a window with focus. If not, reject. This is thread safe
827 // because if the focus changes some time before or after, the
828 // next client receiving focus that has any interest in input will
829 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
832 return null;
833 }
834 } catch (RemoteException e) {
835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800836
Dianne Hackborn7663d802012-02-24 13:08:49 -0800837 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
838 }
839
840 InputBindResult startInputUncheckedLocked(ClientState cs,
841 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
842 // If no method is currently selected, do nothing.
843 if (mCurMethodId == null) {
844 return mNoBinding;
845 }
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (mCurClient != cs) {
848 // If the client is changing, we need to switch over to the new
849 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700850 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800851 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 + cs.client.asBinder());
853
854 // If the screen is on, inform the new client it is active
855 if (mScreenOn) {
856 try {
857 cs.client.setActive(mScreenOn);
858 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800859 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 + cs.pid + " uid " + cs.uid);
861 }
862 }
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 // Bump up the sequence for this client and attach it.
866 mCurSeq++;
867 if (mCurSeq <= 0) mCurSeq = 1;
868 mCurClient = cs;
869 mCurInputContext = inputContext;
870 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 // Check if the input method is changing.
873 if (mCurId != null && mCurId.equals(mCurMethodId)) {
874 if (cs.curSession != null) {
875 // Fast case: if we are already connected to the input method,
876 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -0800877 return attachNewInputLocked(
878 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880 if (mHaveConnection) {
881 if (mCurMethod != null) {
882 if (!cs.sessionRequested) {
883 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800884 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
886 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900887 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889 // Return to client, and we will get back with it when
890 // we have had a session made for it.
891 return new InputBindResult(null, mCurId, mCurSeq);
892 } else if (SystemClock.uptimeMillis()
893 < (mLastBindTime+TIME_TO_RECONNECT)) {
894 // In this case we have connected to the service, but
895 // don't yet have its interface. If it hasn't been too
896 // long since we did the connection, we'll return to
897 // the client and wait to get the service interface so
898 // we can report back. If it has been too long, we want
899 // to fall through so we can try a disconnect/reconnect
900 // to see if we can get back in touch with the service.
901 return new InputBindResult(null, mCurId, mCurSeq);
902 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
904 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906 }
907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700909 return startInputInnerLocked();
910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800911
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700912 InputBindResult startInputInnerLocked() {
913 if (mCurMethodId == null) {
914 return mNoBinding;
915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700917 if (!mSystemReady) {
918 // If the system is not yet ready, we shouldn't be running third
919 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700920 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 InputMethodInfo info = mMethodMap.get(mCurMethodId);
924 if (info == null) {
925 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700928 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
931 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700932 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
933 com.android.internal.R.string.input_method_binding_label);
934 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
935 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700936 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
937 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 mLastBindTime = SystemClock.uptimeMillis();
939 mHaveConnection = true;
940 mCurId = info.getId();
941 mCurToken = new Binder();
942 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800943 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 mIWindowManager.addWindowToken(mCurToken,
945 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
946 } catch (RemoteException e) {
947 }
948 return new InputBindResult(null, mCurId, mCurSeq);
949 } else {
950 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800951 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 + mCurIntent);
953 }
954 return null;
955 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800956
satoke7c6998e2011-06-03 17:57:59 +0900957 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800959 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 synchronized (mMethodMap) {
961 final long ident = Binder.clearCallingIdentity();
962 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -0800963 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 } finally {
965 Binder.restoreCallingIdentity(ident);
966 }
967 }
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
satoke7c6998e2011-06-03 17:57:59 +0900970 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 public void finishInput(IInputMethodClient client) {
972 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800973
satoke7c6998e2011-06-03 17:57:59 +0900974 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public void onServiceConnected(ComponentName name, IBinder service) {
976 synchronized (mMethodMap) {
977 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
978 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700979 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800980 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700981 unbindCurrentMethodLocked(false);
982 return;
983 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800984 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700985 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
986 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800988 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700989 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700991 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900992 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 }
995 }
996 }
997
998 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
999 synchronized (mMethodMap) {
1000 if (mCurMethod != null && method != null
1001 && mCurMethod.asBinder() == method.asBinder()) {
1002 if (mCurClient != null) {
1003 mCurClient.curSession = new SessionState(mCurClient,
1004 method, session);
1005 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001006 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (res.method != null) {
1008 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1009 MSG_BIND_METHOD, mCurClient.client, res));
1010 }
1011 }
1012 }
1013 }
1014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001016 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001017 if (mVisibleBound) {
1018 mContext.unbindService(mVisibleConnection);
1019 mVisibleBound = false;
1020 }
1021
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001022 if (mHaveConnection) {
1023 mContext.unbindService(this);
1024 mHaveConnection = false;
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001027 if (mCurToken != null) {
1028 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001029 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001030 mIWindowManager.removeWindowToken(mCurToken);
1031 } catch (RemoteException e) {
1032 }
1033 mCurToken = null;
1034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001035
The Android Open Source Project10592532009-03-18 17:39:46 -07001036 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001037 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001038
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001039 if (reportToClient && mCurClient != null) {
1040 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1041 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1042 }
1043 }
1044
Devin Taylor0c33ed22010-02-23 13:26:46 -06001045 private void finishSession(SessionState sessionState) {
1046 if (sessionState != null && sessionState.session != null) {
1047 try {
1048 sessionState.session.finishSession();
1049 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001050 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001051 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001052 }
1053 }
1054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001056 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 if (mCurMethod != null) {
1058 for (ClientState cs : mClients.values()) {
1059 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001060 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 cs.curSession = null;
1062 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001063
1064 finishSession(mEnabledSession);
1065 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 mCurMethod = null;
1067 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001068 if (mStatusBar != null) {
1069 mStatusBar.setIconVisibility("ime", false);
1070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
satoke7c6998e2011-06-03 17:57:59 +09001073 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 public void onServiceDisconnected(ComponentName name) {
1075 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001076 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 + " mCurIntent=" + mCurIntent);
1078 if (mCurMethod != null && mCurIntent != null
1079 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001080 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 // We consider this to be a new bind attempt, since the system
1082 // should now try to restart the service for us.
1083 mLastBindTime = SystemClock.uptimeMillis();
1084 mShowRequested = mInputShown;
1085 mInputShown = false;
1086 if (mCurClient != null) {
1087 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1088 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1089 }
1090 }
1091 }
1092 }
1093
satokf9f01002011-05-19 21:31:50 +09001094 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001096 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 long ident = Binder.clearCallingIdentity();
1098 try {
1099 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001100 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 return;
1102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 synchronized (mMethodMap) {
1105 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001106 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001107 if (mStatusBar != null) {
1108 mStatusBar.setIconVisibility("ime", false);
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001112 CharSequence contentDescription = null;
1113 try {
1114 PackageManager packageManager = mContext.getPackageManager();
1115 contentDescription = packageManager.getApplicationLabel(
1116 packageManager.getApplicationInfo(packageName, 0));
1117 } catch (NameNotFoundException nnfe) {
1118 /* ignore */
1119 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001120 if (mStatusBar != null) {
1121 mStatusBar.setIcon("ime", packageName, iconId, 0,
1122 contentDescription != null
1123 ? contentDescription.toString() : null);
1124 mStatusBar.setIconVisibility("ime", true);
1125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128 } finally {
1129 Binder.restoreCallingIdentity(ident);
1130 }
1131 }
1132
satok7cfc0ed2011-06-20 21:29:36 +09001133 private boolean needsToShowImeSwitchOngoingNotification() {
1134 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001135 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001136 synchronized (mMethodMap) {
1137 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1138 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001139 if (N > 2) return true;
1140 if (N < 1) return false;
1141 int nonAuxCount = 0;
1142 int auxCount = 0;
1143 InputMethodSubtype nonAuxSubtype = null;
1144 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001145 for(int i = 0; i < N; ++i) {
1146 final InputMethodInfo imi = imis.get(i);
1147 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1148 imi, true);
1149 final int subtypeCount = subtypes.size();
1150 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001151 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001152 } else {
1153 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001154 final InputMethodSubtype subtype = subtypes.get(j);
1155 if (!subtype.isAuxiliary()) {
1156 ++nonAuxCount;
1157 nonAuxSubtype = subtype;
1158 } else {
1159 ++auxCount;
1160 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001161 }
1162 }
1163 }
satok7cfc0ed2011-06-20 21:29:36 +09001164 }
satokb6359412011-06-28 17:47:41 +09001165 if (nonAuxCount > 1 || auxCount > 1) {
1166 return true;
1167 } else if (nonAuxCount == 1 && auxCount == 1) {
1168 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001169 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1170 || auxSubtype.overridesImplicitlyEnabledSubtype()
1171 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001172 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1173 return false;
1174 }
1175 return true;
1176 }
1177 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001178 }
satok7cfc0ed2011-06-20 21:29:36 +09001179 }
1180
satokdbf29502011-08-25 15:28:23 +09001181 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001182 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001183 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001184 int uid = Binder.getCallingUid();
1185 long ident = Binder.clearCallingIdentity();
1186 try {
1187 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001188 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001189 return;
1190 }
1191
1192 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001193 mImeWindowVis = vis;
1194 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001195 if (mStatusBar != null) {
1196 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1197 }
satok7cfc0ed2011-06-20 21:29:36 +09001198 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001199 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1200 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001201 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001202 final CharSequence title = mRes.getText(
1203 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001204 final CharSequence imiLabel = imi.loadLabel(pm);
1205 final CharSequence summary = mCurrentSubtype != null
1206 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1207 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1208 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001209 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001210 : imiLabel;
1211
satok7cfc0ed2011-06-20 21:29:36 +09001212 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001213 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001214 if (mNotificationManager != null) {
1215 mNotificationManager.notify(
1216 com.android.internal.R.string.select_input_method,
1217 mImeSwitcherNotification);
1218 mNotificationShown = true;
1219 }
satok7cfc0ed2011-06-20 21:29:36 +09001220 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001221 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001222 mNotificationManager.cancel(
1223 com.android.internal.R.string.select_input_method);
1224 mNotificationShown = false;
1225 }
1226 }
satok06487a52010-10-29 11:37:18 +09001227 }
1228 } finally {
1229 Binder.restoreCallingIdentity(ident);
1230 }
1231 }
1232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
satokf9f01002011-05-19 21:31:50 +09001234 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1235 synchronized (mMethodMap) {
1236 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1237 for (int i = 0; i < spans.length; ++i) {
1238 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001239 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001240 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001241 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001242 }
1243 }
1244 }
1245 }
1246
satoke7c6998e2011-06-03 17:57:59 +09001247 @Override
satokf9f01002011-05-19 21:31:50 +09001248 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1249 synchronized (mMethodMap) {
1250 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1251 // TODO: Do not send the intent if the process of the targetImi is already dead.
1252 if (targetImi != null) {
1253 final String[] suggestions = span.getSuggestions();
1254 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001255 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001256 final Intent intent = new Intent();
1257 // Ensures that only a class in the original IME package will receive the
1258 // notification.
satok42c5a162011-05-26 16:46:14 +09001259 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001260 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1261 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1262 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1263 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1264 mContext.sendBroadcast(intent);
1265 return true;
1266 }
1267 }
1268 return false;
1269 }
1270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001272 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1273 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1274 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1275 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001277 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001278 // There is no input method selected, try to choose new applicable input method.
1279 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1280 id = Settings.Secure.getString(mContext.getContentResolver(),
1281 Settings.Secure.DEFAULT_INPUT_METHOD);
1282 }
1283 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 try {
satokab751aa2010-09-14 19:17:36 +09001285 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001287 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001288 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001289 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
satokf3db1af2010-11-23 13:34:33 +09001291 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001292 } else {
1293 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001294 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001295 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
satokab751aa2010-09-14 19:17:36 +09001299 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 InputMethodInfo info = mMethodMap.get(id);
1301 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001302 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001306 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001307 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1308 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001309 }
1310 if (subtype != mCurrentSubtype) {
1311 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001312 if (subtype != null) {
1313 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1314 }
satokcd7cd292010-11-20 15:46:23 +09001315 if (mCurMethod != null) {
1316 try {
satok3afd6c02011-11-18 08:38:19 +09001317 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001318 // If subtype is null, try to find the most applicable one from
1319 // getCurrentInputMethodSubtype.
1320 if (subtype == null) {
1321 subtype = getCurrentInputMethodSubtype();
1322 }
1323 mCurMethod.changeInputMethodSubtype(subtype);
1324 } catch (RemoteException e) {
1325 return;
satokab751aa2010-09-14 19:17:36 +09001326 }
1327 }
1328 }
1329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 return;
1331 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 final long ident = Binder.clearCallingIdentity();
1334 try {
satokab751aa2010-09-14 19:17:36 +09001335 // Set a subtype to this input method.
1336 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001337 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1338 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1339 // because mCurMethodId is stored as a history in
1340 // setSelectedInputMethodAndSubtypeLocked().
1341 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342
1343 if (ActivityManagerNative.isSystemReady()) {
1344 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001345 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 intent.putExtra("input_method_id", id);
1347 mContext.sendBroadcast(intent);
1348 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001349 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 } finally {
1351 Binder.restoreCallingIdentity(ident);
1352 }
1353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001354
satok42c5a162011-05-26 16:46:14 +09001355 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001356 public boolean showSoftInput(IInputMethodClient client, int flags,
1357 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001358 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 long ident = Binder.clearCallingIdentity();
1360 try {
1361 synchronized (mMethodMap) {
1362 if (mCurClient == null || client == null
1363 || mCurClient.client.asBinder() != client.asBinder()) {
1364 try {
1365 // We need to check if this is the current client with
1366 // focus in the window manager, to allow this call to
1367 // be made before input is started in it.
1368 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001369 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001370 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001373 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001378 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
1380 } finally {
1381 Binder.restoreCallingIdentity(ident);
1382 }
1383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001384
The Android Open Source Project4df24232009-03-05 14:34:35 -08001385 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 mShowRequested = true;
1387 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1388 mShowExplicitlyRequested = true;
1389 }
1390 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1391 mShowExplicitlyRequested = true;
1392 mShowForced = true;
1393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001394
Dianne Hackborncc278702009-09-02 23:07:23 -07001395 if (!mSystemReady) {
1396 return false;
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
The Android Open Source Project4df24232009-03-05 14:34:35 -08001399 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001401 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1402 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1403 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001405 if (mHaveConnection && !mVisibleBound) {
1406 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1407 mVisibleBound = true;
1408 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001409 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001411 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 // The client has asked to have the input method shown, but
1413 // we have been sitting here too long with a connection to the
1414 // service and no interface received, so let's disconnect/connect
1415 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001418 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001420 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1421 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
The Android Open Source Project4df24232009-03-05 14:34:35 -08001424 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
satok42c5a162011-05-26 16:46:14 +09001427 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001428 public boolean hideSoftInput(IInputMethodClient client, int flags,
1429 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001430 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 long ident = Binder.clearCallingIdentity();
1432 try {
1433 synchronized (mMethodMap) {
1434 if (mCurClient == null || client == null
1435 || mCurClient.client.asBinder() != client.asBinder()) {
1436 try {
1437 // We need to check if this is the current client with
1438 // focus in the window manager, to allow this call to
1439 // be made before input is started in it.
1440 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001441 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1442 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001443 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001444 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
1446 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001447 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001448 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001451
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001453 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 } finally {
1456 Binder.restoreCallingIdentity(ident);
1457 }
1458 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
The Android Open Source Project4df24232009-03-05 14:34:35 -08001460 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1462 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001463 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001465 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001468 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001470 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001472 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001474 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1475 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1476 res = true;
1477 } else {
1478 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001480 if (mHaveConnection && mVisibleBound) {
1481 mContext.unbindService(mVisibleConnection);
1482 mVisibleBound = false;
1483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 mInputShown = false;
1485 mShowRequested = false;
1486 mShowExplicitlyRequested = false;
1487 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001488 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
satok42c5a162011-05-26 16:46:14 +09001491 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001492 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1493 int controlFlags, int softInputMode, int windowFlags,
1494 EditorInfo attribute, IInputContext inputContext) {
1495 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 long ident = Binder.clearCallingIdentity();
1497 try {
1498 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001499 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001500 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001502 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
Dianne Hackborn7663d802012-02-24 13:08:49 -08001504 ClientState cs = mClients.get(client.asBinder());
1505 if (cs == null) {
1506 throw new IllegalArgumentException("unknown client "
1507 + client.asBinder());
1508 }
1509
1510 try {
1511 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1512 // Check with the window manager to make sure this client actually
1513 // has a window with focus. If not, reject. This is thread safe
1514 // because if the focus changes some time before or after, the
1515 // next client receiving focus that has any interest in input will
1516 // be calling through here after that change happens.
1517 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1518 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1519 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001521 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001523
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001524 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001525 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001526 if (attribute != null) {
1527 return startInputUncheckedLocked(cs, inputContext, attribute,
1528 controlFlags);
1529 }
1530 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001531 }
1532 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001533
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001534 // Should we auto-show the IME even if the caller has not
1535 // specified what should be done with it?
1536 // We only do this automatically if the window can resize
1537 // to accommodate the IME (so what the user sees will give
1538 // them good context without input information being obscured
1539 // by the IME) or if running on a large screen where there
1540 // is more room for the target window + IME.
1541 final boolean doAutoShow =
1542 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1543 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1544 || mRes.getConfiguration().isLayoutSizeAtLeast(
1545 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001546 final boolean isTextEditor =
1547 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1548
1549 // We want to start input before showing the IME, but after closing
1550 // it. We want to do this after closing it to help the IME disappear
1551 // more quickly (not get stuck behind it initializing itself for the
1552 // new focused input, even if its window wants to hide the IME).
1553 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1556 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001557 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1559 // There is no focus view, and this window will
1560 // be behind any soft input window, so hide the
1561 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001562 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001563 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001565 } else if (isTextEditor && doAutoShow && (softInputMode &
1566 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 // There is a focus view, and we are navigating forward
1568 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001569 // We only do this automatically if the window can resize
1570 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001571 // them good context without input information being obscured
1572 // by the IME) or if running on a large screen where there
1573 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001574 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001575 if (attribute != null) {
1576 res = startInputUncheckedLocked(cs, inputContext, attribute,
1577 controlFlags);
1578 didStart = true;
1579 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001580 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582 break;
1583 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1584 // Do nothing.
1585 break;
1586 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1587 if ((softInputMode &
1588 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001590 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592 break;
1593 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001595 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 break;
1597 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1598 if ((softInputMode &
1599 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001601 if (attribute != null) {
1602 res = startInputUncheckedLocked(cs, inputContext, attribute,
1603 controlFlags);
1604 didStart = true;
1605 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001606 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
1608 break;
1609 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001610 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001611 if (attribute != null) {
1612 res = startInputUncheckedLocked(cs, inputContext, attribute,
1613 controlFlags);
1614 didStart = true;
1615 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001616 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 break;
1618 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001619
1620 if (!didStart && attribute != null) {
1621 res = startInputUncheckedLocked(cs, inputContext, attribute,
1622 controlFlags);
1623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 } finally {
1626 Binder.restoreCallingIdentity(ident);
1627 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001628
1629 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
satok42c5a162011-05-26 16:46:14 +09001632 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1634 synchronized (mMethodMap) {
1635 if (mCurClient == null || client == null
1636 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001637 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001638 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640
satok440aab52010-11-25 09:43:11 +09001641 // Always call subtype picker, because subtype picker is a superset of input method
1642 // picker.
satokab751aa2010-09-14 19:17:36 +09001643 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1644 }
1645 }
1646
satok42c5a162011-05-26 16:46:14 +09001647 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001649 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1650 }
1651
satok42c5a162011-05-26 16:46:14 +09001652 @Override
satok28203512010-11-24 11:06:49 +09001653 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1654 synchronized (mMethodMap) {
1655 if (subtype != null) {
1656 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1657 mMethodMap.get(id), subtype.hashCode()));
1658 } else {
1659 setInputMethod(token, id);
1660 }
1661 }
satokab751aa2010-09-14 19:17:36 +09001662 }
1663
satok42c5a162011-05-26 16:46:14 +09001664 @Override
satokb416a712010-11-25 20:42:14 +09001665 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001666 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001667 synchronized (mMethodMap) {
1668 if (mCurClient == null || client == null
1669 || mCurClient.client.asBinder() != client.asBinder()) {
1670 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1671 }
satok7fee71f2010-12-17 18:54:26 +09001672 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1673 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001674 }
1675 }
1676
satok4fc87d62011-05-20 16:13:43 +09001677 @Override
satok735cf382010-11-11 20:40:09 +09001678 public boolean switchToLastInputMethod(IBinder token) {
1679 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001680 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001681 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001682 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001683 lastImi = mMethodMap.get(lastIme.first);
1684 } else {
1685 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001686 }
satok4fc87d62011-05-20 16:13:43 +09001687 String targetLastImiId = null;
1688 int subtypeId = NOT_A_SUBTYPE_ID;
1689 if (lastIme != null && lastImi != null) {
1690 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1691 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1692 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1693 : mCurrentSubtype.hashCode();
1694 // If the last IME is the same as the current IME and the last subtype is not
1695 // defined, there is no need to switch to the last IME.
1696 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1697 targetLastImiId = lastIme.first;
1698 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1699 }
1700 }
1701
1702 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1703 // This is a safety net. If the currentSubtype can't be added to the history
1704 // and the framework couldn't find the last ime, we will make the last ime be
1705 // the most applicable enabled keyboard subtype of the system imes.
1706 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1707 if (enabled != null) {
1708 final int N = enabled.size();
1709 final String locale = mCurrentSubtype == null
1710 ? mRes.getConfiguration().locale.toString()
1711 : mCurrentSubtype.getLocale();
1712 for (int i = 0; i < N; ++i) {
1713 final InputMethodInfo imi = enabled.get(i);
1714 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1715 InputMethodSubtype keyboardSubtype =
1716 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1717 SUBTYPE_MODE_KEYBOARD, locale, true);
1718 if (keyboardSubtype != null) {
1719 targetLastImiId = imi.getId();
1720 subtypeId = getSubtypeIdFromHashCode(
1721 imi, keyboardSubtype.hashCode());
1722 if(keyboardSubtype.getLocale().equals(locale)) {
1723 break;
1724 }
1725 }
1726 }
1727 }
1728 }
1729 }
1730
1731 if (!TextUtils.isEmpty(targetLastImiId)) {
1732 if (DEBUG) {
1733 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1734 + ", from: " + mCurMethodId + ", " + subtypeId);
1735 }
1736 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1737 return true;
1738 } else {
1739 return false;
1740 }
satok735cf382010-11-11 20:40:09 +09001741 }
1742 }
1743
satoke7c6998e2011-06-03 17:57:59 +09001744 @Override
satok688bd472012-02-09 20:09:17 +09001745 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1746 synchronized (mMethodMap) {
1747 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1748 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1749 if (nextSubtype == null) {
1750 return false;
1751 }
1752 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1753 return true;
1754 }
1755 }
1756
1757 @Override
satok68f1b782011-04-11 14:26:04 +09001758 public InputMethodSubtype getLastInputMethodSubtype() {
1759 synchronized (mMethodMap) {
1760 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1761 // TODO: Handle the case of the last IME with no subtypes
1762 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1763 || TextUtils.isEmpty(lastIme.second)) return null;
1764 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1765 if (lastImi == null) return null;
1766 try {
1767 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001768 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1769 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1770 return null;
1771 }
1772 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001773 } catch (NumberFormatException e) {
1774 return null;
1775 }
1776 }
1777 }
1778
satoke7c6998e2011-06-03 17:57:59 +09001779 @Override
satokee5e77c2011-09-02 18:50:15 +09001780 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001781 // By this IPC call, only a process which shares the same uid with the IME can add
1782 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001783 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001784 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001785 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001786 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001787 final PackageManager pm = mContext.getPackageManager();
1788 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1789 if (packageInfos != null) {
1790 final int packageNum = packageInfos.length;
1791 for (int i = 0; i < packageNum; ++i) {
1792 if (packageInfos[i].equals(imi.getPackageName())) {
1793 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001794 final long ident = Binder.clearCallingIdentity();
1795 try {
1796 buildInputMethodListLocked(mMethodList, mMethodMap);
1797 } finally {
1798 Binder.restoreCallingIdentity(ident);
1799 }
satokee5e77c2011-09-02 18:50:15 +09001800 return;
satok91e88122011-07-18 11:11:42 +09001801 }
1802 }
1803 }
satoke7c6998e2011-06-03 17:57:59 +09001804 }
satokee5e77c2011-09-02 18:50:15 +09001805 return;
satoke7c6998e2011-06-03 17:57:59 +09001806 }
1807
satok28203512010-11-24 11:06:49 +09001808 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 synchronized (mMethodMap) {
1810 if (token == null) {
1811 if (mContext.checkCallingOrSelfPermission(
1812 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1813 != PackageManager.PERMISSION_GRANTED) {
1814 throw new SecurityException(
1815 "Using null token requires permission "
1816 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1817 }
1818 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001819 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1820 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 return;
1822 }
1823
satokc5933802011-08-31 21:26:04 +09001824 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 try {
satokab751aa2010-09-14 19:17:36 +09001826 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 } finally {
1828 Binder.restoreCallingIdentity(ident);
1829 }
1830 }
1831 }
1832
satok42c5a162011-05-26 16:46:14 +09001833 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 public void hideMySoftInput(IBinder token, int flags) {
1835 synchronized (mMethodMap) {
1836 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001837 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1838 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 return;
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 long ident = Binder.clearCallingIdentity();
1842 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001843 hideCurrentInputLocked(flags, null);
1844 } finally {
1845 Binder.restoreCallingIdentity(ident);
1846 }
1847 }
1848 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001849
satok42c5a162011-05-26 16:46:14 +09001850 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001851 public void showMySoftInput(IBinder token, int flags) {
1852 synchronized (mMethodMap) {
1853 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001854 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1855 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001856 return;
1857 }
1858 long ident = Binder.clearCallingIdentity();
1859 try {
1860 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 } finally {
1862 Binder.restoreCallingIdentity(ident);
1863 }
1864 }
1865 }
1866
1867 void setEnabledSessionInMainThread(SessionState session) {
1868 if (mEnabledSession != session) {
1869 if (mEnabledSession != null) {
1870 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001871 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 mEnabledSession.method.setSessionEnabled(
1873 mEnabledSession.session, false);
1874 } catch (RemoteException e) {
1875 }
1876 }
1877 mEnabledSession = session;
1878 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 session.method.setSessionEnabled(
1881 session.session, true);
1882 } catch (RemoteException e) {
1883 }
1884 }
1885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
satok42c5a162011-05-26 16:46:14 +09001887 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 public boolean handleMessage(Message msg) {
1889 HandlerCaller.SomeArgs args;
1890 switch (msg.what) {
1891 case MSG_SHOW_IM_PICKER:
1892 showInputMethodMenu();
1893 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001894
satokab751aa2010-09-14 19:17:36 +09001895 case MSG_SHOW_IM_SUBTYPE_PICKER:
1896 showInputMethodSubtypeMenu();
1897 return true;
1898
satok47a44912010-10-06 16:03:58 +09001899 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001900 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001901 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001902 return true;
1903
1904 case MSG_SHOW_IM_CONFIG:
1905 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001906 return true;
1907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 case MSG_UNBIND_INPUT:
1911 try {
1912 ((IInputMethod)msg.obj).unbindInput();
1913 } catch (RemoteException e) {
1914 // There is nothing interesting about the method dying.
1915 }
1916 return true;
1917 case MSG_BIND_INPUT:
1918 args = (HandlerCaller.SomeArgs)msg.obj;
1919 try {
1920 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1921 } catch (RemoteException e) {
1922 }
1923 return true;
1924 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001925 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001927 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1928 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 } catch (RemoteException e) {
1930 }
1931 return true;
1932 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001933 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 ((IInputMethod)args.arg1).hideSoftInput(0,
1936 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 } catch (RemoteException e) {
1938 }
1939 return true;
1940 case MSG_ATTACH_TOKEN:
1941 args = (HandlerCaller.SomeArgs)msg.obj;
1942 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001943 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1945 } catch (RemoteException e) {
1946 }
1947 return true;
1948 case MSG_CREATE_SESSION:
1949 args = (HandlerCaller.SomeArgs)msg.obj;
1950 try {
1951 ((IInputMethod)args.arg1).createSession(
1952 (IInputMethodCallback)args.arg2);
1953 } catch (RemoteException e) {
1954 }
1955 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 case MSG_START_INPUT:
1959 args = (HandlerCaller.SomeArgs)msg.obj;
1960 try {
1961 SessionState session = (SessionState)args.arg1;
1962 setEnabledSessionInMainThread(session);
1963 session.method.startInput((IInputContext)args.arg2,
1964 (EditorInfo)args.arg3);
1965 } catch (RemoteException e) {
1966 }
1967 return true;
1968 case MSG_RESTART_INPUT:
1969 args = (HandlerCaller.SomeArgs)msg.obj;
1970 try {
1971 SessionState session = (SessionState)args.arg1;
1972 setEnabledSessionInMainThread(session);
1973 session.method.restartInput((IInputContext)args.arg2,
1974 (EditorInfo)args.arg3);
1975 } catch (RemoteException e) {
1976 }
1977 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 case MSG_UNBIND_METHOD:
1982 try {
1983 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1984 } catch (RemoteException e) {
1985 // There is nothing interesting about the last client dying.
1986 }
1987 return true;
1988 case MSG_BIND_METHOD:
1989 args = (HandlerCaller.SomeArgs)msg.obj;
1990 try {
1991 ((IInputMethodClient)args.arg1).onBindMethod(
1992 (InputBindResult)args.arg2);
1993 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001994 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 return true;
1997 }
1998 return false;
1999 }
2000
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002001 private boolean isSystemIme(InputMethodInfo inputMethod) {
2002 return (inputMethod.getServiceInfo().applicationInfo.flags
2003 & ApplicationInfo.FLAG_SYSTEM) != 0;
2004 }
2005
Ken Wakasa586f0512011-01-20 22:31:01 +09002006 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2007 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2008 final int subtypeCount = imi.getSubtypeCount();
2009 for (int i = 0; i < subtypeCount; ++i) {
2010 subtypes.add(imi.getSubtypeAt(i));
2011 }
2012 return subtypes;
2013 }
2014
satoka86f5e42011-09-02 17:12:42 +09002015
2016 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2017 InputMethodInfo imi, String mode) {
2018 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2019 final int subtypeCount = imi.getSubtypeCount();
2020 for (int i = 0; i < subtypeCount; ++i) {
2021 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2022 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2023 subtypes.add(subtype);
2024 }
2025 }
2026 return subtypes;
2027 }
2028
satokdc9ddae2011-10-06 12:22:36 +09002029 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002030 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002031 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002032 // We'd prefer to fall back on a system IME, since that is safer.
2033 int i=enabled.size();
2034 while (i > 0) {
2035 i--;
satokdc9ddae2011-10-06 12:22:36 +09002036 final InputMethodInfo imi = enabled.get(i);
2037 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002038 break;
2039 }
2040 }
satokdc9ddae2011-10-06 12:22:36 +09002041 return enabled.get(i);
2042 }
2043 return null;
2044 }
2045
2046 private boolean chooseNewDefaultIMELocked() {
2047 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2048 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002049 if (DEBUG) {
2050 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2051 }
satok723a27e2010-11-11 14:58:11 +09002052 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002053 return true;
2054 }
2055
2056 return false;
2057 }
2058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2060 HashMap<String, InputMethodInfo> map) {
2061 list.clear();
2062 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002065 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002066 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2067 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2068 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2069 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070
2071 List<ResolveInfo> services = pm.queryIntentServices(
2072 new Intent(InputMethod.SERVICE_INTERFACE),
2073 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002074
satoke7c6998e2011-06-03 17:57:59 +09002075 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2076 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 for (int i = 0; i < services.size(); ++i) {
2078 ResolveInfo ri = services.get(i);
2079 ServiceInfo si = ri.serviceInfo;
2080 ComponentName compName = new ComponentName(si.packageName, si.name);
2081 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2082 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002083 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 + ": it does not require the permission "
2085 + android.Manifest.permission.BIND_INPUT_METHOD);
2086 continue;
2087 }
2088
Joe Onorato8a9b2202010-02-26 18:56:32 -08002089 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090
2091 try {
satoke7c6998e2011-06-03 17:57:59 +09002092 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002094 final String id = p.getId();
2095 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096
Amith Yamasanie861ec12010-03-24 21:39:27 -07002097 // System IMEs are enabled by default, unless there's a hard keyboard
2098 // and the system IME was explicitly disabled
2099 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2100 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002101 }
2102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002104 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002108 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002110 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
2112 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002113
2114 String defaultIme = Settings.Secure.getString(mContext
2115 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002116 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002117 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002118 updateFromSettingsLocked();
2119 }
2120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002124
satokab751aa2010-09-14 19:17:36 +09002125 private void showInputMethodMenu() {
2126 showInputMethodMenuInternal(false);
2127 }
2128
2129 private void showInputMethodSubtypeMenu() {
2130 showInputMethodMenuInternal(true);
2131 }
2132
satok217f5482010-12-15 05:19:19 +09002133 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002134 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002135 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002136 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2137 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002138 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002139 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002140 }
satok217f5482010-12-15 05:19:19 +09002141 mContext.startActivity(intent);
2142 }
2143
2144 private void showConfigureInputMethods() {
2145 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2146 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2147 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2148 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002149 mContext.startActivity(intent);
2150 }
2151
satok2c93efc2012-04-02 19:33:47 +09002152 private boolean isScreenLocked() {
2153 return mKeyguardManager != null
2154 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2155 }
satokab751aa2010-09-14 19:17:36 +09002156 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002157 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002161 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002162
satok2c93efc2012-04-02 19:33:47 +09002163 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002165 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002166 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002168 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002169 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2170 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002171 if (immis == null || immis.size() == 0) {
2172 return;
2173 }
2174
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002175 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176
satok688bd472012-02-09 20:09:17 +09002177 final List<ImeSubtypeListItem> imList =
2178 mImListManager.getSortedInputMethodAndSubtypeList(
2179 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002180
satokc3690562012-01-10 20:14:43 +09002181 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2182 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2183 if (currentSubtype != null) {
2184 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2185 lastInputMethodSubtypeId =
2186 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2187 }
2188 }
2189
Ken Wakasa761eb372011-03-04 19:06:18 +09002190 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002191 mIms = new InputMethodInfo[N];
2192 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002193 int checkedItem = 0;
2194 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002195 final ImeSubtypeListItem item = imList.get(i);
2196 mIms[i] = item.mImi;
2197 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002198 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002199 int subtypeId = mSubtypeIds[i];
2200 if ((subtypeId == NOT_A_SUBTYPE_ID)
2201 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2202 || (subtypeId == lastInputMethodSubtypeId)) {
2203 checkedItem = i;
2204 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
satokab751aa2010-09-14 19:17:36 +09002207
Ken Wakasa05dbb652011-08-22 15:22:43 +09002208 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002209 com.android.internal.R.styleable.DialogPreference,
2210 com.android.internal.R.attr.alertDialogStyle, 0);
2211 mDialogBuilder = new AlertDialog.Builder(context)
2212 .setTitle(com.android.internal.R.string.select_input_method)
2213 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002214 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002215 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002217 }
2218 })
2219 .setIcon(a.getDrawable(
2220 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2221 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002222
Ken Wakasa05dbb652011-08-22 15:22:43 +09002223 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2224 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2225 checkedItem);
2226
2227 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002228 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002229 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002230 public void onClick(DialogInterface dialog, int which) {
2231 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002232 if (mIms == null || mIms.length <= which
2233 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002234 return;
2235 }
2236 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002237 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002238 hideInputMethodMenu();
2239 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002240 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002241 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002242 subtypeId = NOT_A_SUBTYPE_ID;
2243 }
2244 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002245 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002248 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249
satokbc81b692011-08-26 16:22:22 +09002250 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002251 mDialogBuilder.setPositiveButton(
2252 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002253 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002254 @Override
satok7f35c8c2010-10-07 21:13:11 +09002255 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002256 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002257 }
2258 });
2259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002261 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 mSwitchingDialog.getWindow().setType(
2263 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002264 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 mSwitchingDialog.show();
2266 }
2267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002268
Ken Wakasa05dbb652011-08-22 15:22:43 +09002269 private static class ImeSubtypeListItem {
2270 public final CharSequence mImeName;
2271 public final CharSequence mSubtypeName;
2272 public final InputMethodInfo mImi;
2273 public final int mSubtypeId;
2274 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2275 InputMethodInfo imi, int subtypeId) {
2276 mImeName = imeName;
2277 mSubtypeName = subtypeName;
2278 mImi = imi;
2279 mSubtypeId = subtypeId;
2280 }
2281 }
2282
2283 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2284 private final LayoutInflater mInflater;
2285 private final int mTextViewResourceId;
2286 private final List<ImeSubtypeListItem> mItemsList;
2287 private final int mCheckedItem;
2288 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2289 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2290 super(context, textViewResourceId, itemsList);
2291 mTextViewResourceId = textViewResourceId;
2292 mItemsList = itemsList;
2293 mCheckedItem = checkedItem;
2294 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2295 }
2296
2297 @Override
2298 public View getView(int position, View convertView, ViewGroup parent) {
2299 final View view = convertView != null ? convertView
2300 : mInflater.inflate(mTextViewResourceId, null);
2301 if (position < 0 || position >= mItemsList.size()) return view;
2302 final ImeSubtypeListItem item = mItemsList.get(position);
2303 final CharSequence imeName = item.mImeName;
2304 final CharSequence subtypeName = item.mSubtypeName;
2305 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2306 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2307 if (TextUtils.isEmpty(subtypeName)) {
2308 firstTextView.setText(imeName);
2309 secondTextView.setVisibility(View.GONE);
2310 } else {
2311 firstTextView.setText(subtypeName);
2312 secondTextView.setText(imeName);
2313 secondTextView.setVisibility(View.VISIBLE);
2314 }
2315 final RadioButton radioButton =
2316 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2317 radioButton.setChecked(position == mCheckedItem);
2318 return view;
2319 }
2320 }
2321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002323 synchronized (mMethodMap) {
2324 hideInputMethodMenuLocked();
2325 }
2326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002327
The Android Open Source Project10592532009-03-18 17:39:46 -07002328 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002329 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330
The Android Open Source Project10592532009-03-18 17:39:46 -07002331 if (mSwitchingDialog != null) {
2332 mSwitchingDialog.dismiss();
2333 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002335
The Android Open Source Project10592532009-03-18 17:39:46 -07002336 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002337 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002341
satok42c5a162011-05-26 16:46:14 +09002342 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 public boolean setInputMethodEnabled(String id, boolean enabled) {
2344 synchronized (mMethodMap) {
2345 if (mContext.checkCallingOrSelfPermission(
2346 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2347 != PackageManager.PERMISSION_GRANTED) {
2348 throw new SecurityException(
2349 "Requires permission "
2350 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2351 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 long ident = Binder.clearCallingIdentity();
2354 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002355 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 } finally {
2357 Binder.restoreCallingIdentity(ident);
2358 }
2359 }
2360 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002361
2362 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2363 // Make sure this is a valid input method.
2364 InputMethodInfo imm = mMethodMap.get(id);
2365 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002366 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002367 }
2368
satokd87c2592010-09-29 11:52:06 +09002369 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2370 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002371
satokd87c2592010-09-29 11:52:06 +09002372 if (enabled) {
2373 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2374 if (pair.first.equals(id)) {
2375 // We are enabling this input method, but it is already enabled.
2376 // Nothing to do. The previous state was enabled.
2377 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002378 }
2379 }
satokd87c2592010-09-29 11:52:06 +09002380 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2381 // Previous state was disabled.
2382 return false;
2383 } else {
2384 StringBuilder builder = new StringBuilder();
2385 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2386 builder, enabledInputMethodsList, id)) {
2387 // Disabled input method is currently selected, switch to another one.
2388 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2389 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002390 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2391 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2392 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002393 }
2394 // Previous state was enabled.
2395 return true;
2396 } else {
2397 // We are disabling the input method but it is already disabled.
2398 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002399 return false;
2400 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002401 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002402 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002403
satok57ffc002011-01-25 00:11:47 +09002404 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2405 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002406 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002407 }
2408
satok723a27e2010-11-11 14:58:11 +09002409 private void saveCurrentInputMethodAndSubtypeToHistory() {
2410 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2411 if (mCurrentSubtype != null) {
2412 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2413 }
satok57ffc002011-01-25 00:11:47 +09002414 if (canAddToLastInputMethod(mCurrentSubtype)) {
2415 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2416 }
satokab751aa2010-09-14 19:17:36 +09002417 }
2418
satok723a27e2010-11-11 14:58:11 +09002419 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2420 boolean setSubtypeOnly) {
2421 // Update the history of InputMethod and Subtype
2422 saveCurrentInputMethodAndSubtypeToHistory();
2423
2424 // Set Subtype here
2425 if (imi == null || subtypeId < 0) {
2426 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002427 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002428 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002429 if (subtypeId < imi.getSubtypeCount()) {
2430 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2431 mSettings.putSelectedSubtype(subtype.hashCode());
2432 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002433 } else {
2434 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2435 mCurrentSubtype = null;
2436 }
satokab751aa2010-09-14 19:17:36 +09002437 }
satok723a27e2010-11-11 14:58:11 +09002438
2439 if (!setSubtypeOnly) {
2440 // Set InputMethod here
2441 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2442 }
2443 }
2444
2445 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2446 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2447 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2448 // newDefaultIme is empty when there is no candidate for the selected IME.
2449 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2450 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2451 if (subtypeHashCode != null) {
2452 try {
2453 lastSubtypeId = getSubtypeIdFromHashCode(
2454 imi, Integer.valueOf(subtypeHashCode));
2455 } catch (NumberFormatException e) {
2456 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2457 }
2458 }
2459 }
2460 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002461 }
2462
2463 private int getSelectedInputMethodSubtypeId(String id) {
2464 InputMethodInfo imi = mMethodMap.get(id);
2465 if (imi == null) {
2466 return NOT_A_SUBTYPE_ID;
2467 }
satokab751aa2010-09-14 19:17:36 +09002468 int subtypeId;
2469 try {
2470 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2471 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2472 } catch (SettingNotFoundException e) {
2473 return NOT_A_SUBTYPE_ID;
2474 }
satok723a27e2010-11-11 14:58:11 +09002475 return getSubtypeIdFromHashCode(imi, subtypeId);
2476 }
2477
2478 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002479 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002480 final int subtypeCount = imi.getSubtypeCount();
2481 for (int i = 0; i < subtypeCount; ++i) {
2482 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002483 if (subtypeHashCode == ims.hashCode()) {
2484 return i;
2485 }
satokab751aa2010-09-14 19:17:36 +09002486 }
2487 }
2488 return NOT_A_SUBTYPE_ID;
2489 }
2490
satoka86f5e42011-09-02 17:12:42 +09002491 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2492 Resources res, InputMethodInfo imi) {
2493 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002494 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002495 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002496 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002497 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002498 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002499 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002500 // scan overriding implicitly enabled subtypes.
2501 InputMethodSubtype subtype = subtypes.get(i);
2502 if (subtype.overridesImplicitlyEnabledSubtype()) {
2503 final String mode = subtype.getMode();
2504 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2505 applicableModeAndSubtypesMap.put(mode, subtype);
2506 }
2507 }
2508 }
2509 if (applicableModeAndSubtypesMap.size() > 0) {
2510 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2511 }
2512 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002513 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002514 final String locale = subtype.getLocale();
2515 final String mode = subtype.getMode();
2516 // When system locale starts with subtype's locale, that subtype will be applicable
2517 // for system locale
2518 // For instance, it's clearly applicable for cases like system locale = en_US and
2519 // subtype = en, but it is not necessarily considered applicable for cases like system
2520 // locale = en and subtype = en_US.
2521 // We just call systemLocale.startsWith(locale) in this function because there is no
2522 // need to find applicable subtypes aggressively unlike
2523 // findLastResortApplicableSubtypeLocked.
2524 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002525 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002526 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002527 if (applicableSubtype != null) {
2528 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2529 if (!systemLocale.equals(locale)) continue;
2530 }
satok3da92232011-01-11 22:46:30 +09002531 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002532 }
2533 }
2534 final InputMethodSubtype keyboardSubtype
2535 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2536 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2537 applicableModeAndSubtypesMap.values());
2538 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2539 for (int i = 0; i < N; ++i) {
2540 final InputMethodSubtype subtype = subtypes.get(i);
2541 final String mode = subtype.getMode();
2542 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2543 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2544 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002545 }
2546 }
2547 }
satokc3690562012-01-10 20:14:43 +09002548 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002549 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002550 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002551 if (lastResortKeyboardSubtype != null) {
2552 applicableSubtypes.add(lastResortKeyboardSubtype);
2553 }
2554 }
2555 return applicableSubtypes;
2556 }
2557
satok4e4569d2010-11-19 18:45:53 +09002558 /**
2559 * If there are no selected subtypes, tries finding the most applicable one according to the
2560 * given locale.
2561 * @param subtypes this function will search the most applicable subtype in subtypes
2562 * @param mode subtypes will be filtered by mode
2563 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002564 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2565 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002566 * @return the most applicable subtypeId
2567 */
satokdf31ae62011-01-15 06:19:44 +09002568 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2569 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002570 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002571 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002572 return null;
satok8fbb1e82010-11-02 23:15:58 +09002573 }
satok4e4569d2010-11-19 18:45:53 +09002574 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002575 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002576 }
satok8fbb1e82010-11-02 23:15:58 +09002577 final String language = locale.substring(0, 2);
2578 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002579 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002580 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002581 final int N = subtypes.size();
2582 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002583 InputMethodSubtype subtype = subtypes.get(i);
2584 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002585 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2586 // and all subtypes with all modes can be candidates.
2587 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002588 if (firstMatchedModeSubtype == null) {
2589 firstMatchedModeSubtype = subtype;
2590 }
satok9ef02832010-11-04 21:17:48 +09002591 if (locale.equals(subtypeLocale)) {
2592 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002593 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002594 break;
2595 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2596 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002597 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002598 partialMatchFound = true;
2599 }
satok8fbb1e82010-11-02 23:15:58 +09002600 }
2601 }
2602
satok7599a7f2010-12-22 13:45:23 +09002603 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2604 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002605 }
2606
satok8fbb1e82010-11-02 23:15:58 +09002607 // The first subtype applicable to the system locale will be defined as the most applicable
2608 // subtype.
2609 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002610 if (applicableSubtype != null) {
2611 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2612 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2613 }
satok8fbb1e82010-11-02 23:15:58 +09002614 }
satokcd7cd292010-11-20 15:46:23 +09002615 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002616 }
2617
satok4e4569d2010-11-19 18:45:53 +09002618 // If there are no selected shortcuts, tries finding the most applicable ones.
2619 private Pair<InputMethodInfo, InputMethodSubtype>
2620 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2621 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2622 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002623 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002624 boolean foundInSystemIME = false;
2625
2626 // Search applicable subtype for each InputMethodInfo
2627 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002628 final String imiId = imi.getId();
2629 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2630 continue;
2631 }
satokcd7cd292010-11-20 15:46:23 +09002632 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002633 final List<InputMethodSubtype> enabledSubtypes =
2634 getEnabledInputMethodSubtypeList(imi, true);
2635 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002636 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002637 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002638 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002639 }
satokdf31ae62011-01-15 06:19:44 +09002640 // 2. Search by the system locale from enabledSubtypes.
2641 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002642 if (subtype == null) {
2643 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002644 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002645 }
satoka86f5e42011-09-02 17:12:42 +09002646 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2647 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2648 final ArrayList<InputMethodSubtype> subtypesForSearch =
2649 overridingImplicitlyEnabledSubtypes.isEmpty()
2650 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002651 // 4. Search by the current subtype's locale from all subtypes.
2652 if (subtype == null && mCurrentSubtype != null) {
2653 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002654 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002655 }
2656 // 5. Search by the system locale from all subtypes.
2657 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002658 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002659 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002660 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002661 }
satokcd7cd292010-11-20 15:46:23 +09002662 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002663 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002664 // The current input method is the most applicable IME.
2665 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002666 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002667 break;
satok7599a7f2010-12-22 13:45:23 +09002668 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002669 // The system input method is 2nd applicable IME.
2670 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002671 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002672 if ((imi.getServiceInfo().applicationInfo.flags
2673 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2674 foundInSystemIME = true;
2675 }
satok4e4569d2010-11-19 18:45:53 +09002676 }
2677 }
2678 }
2679 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002680 if (mostApplicableIMI != null) {
2681 Slog.w(TAG, "Most applicable shortcut input method was:"
2682 + mostApplicableIMI.getId());
2683 if (mostApplicableSubtype != null) {
2684 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2685 + "," + mostApplicableSubtype.getMode() + ","
2686 + mostApplicableSubtype.getLocale());
2687 }
2688 }
satok4e4569d2010-11-19 18:45:53 +09002689 }
satokcd7cd292010-11-20 15:46:23 +09002690 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002691 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002692 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002693 } else {
2694 return null;
2695 }
2696 }
2697
satokab751aa2010-09-14 19:17:36 +09002698 /**
2699 * @return Return the current subtype of this input method.
2700 */
satok42c5a162011-05-26 16:46:14 +09002701 @Override
satokab751aa2010-09-14 19:17:36 +09002702 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002703 boolean subtypeIsSelected = false;
2704 try {
2705 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2706 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2707 } catch (SettingNotFoundException e) {
2708 }
satok3ef8b292010-11-23 06:06:29 +09002709 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002710 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002711 String lastInputMethodId = Settings.Secure.getString(
2712 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002713 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2714 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002715 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2716 if (imi != null) {
2717 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002718 // the most applicable subtype from explicitly or implicitly enabled
2719 // subtypes.
2720 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2721 getEnabledInputMethodSubtypeList(imi, true);
2722 // If there is only one explicitly or implicitly enabled subtype,
2723 // just returns it.
2724 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2725 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2726 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2727 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2728 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2729 SUBTYPE_MODE_KEYBOARD, null, true);
2730 if (mCurrentSubtype == null) {
2731 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2732 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2733 true);
2734 }
2735 }
satok4e4569d2010-11-19 18:45:53 +09002736 }
satokcd7cd292010-11-20 15:46:23 +09002737 } else {
satok3ef8b292010-11-23 06:06:29 +09002738 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002739 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002740 }
satok8fbb1e82010-11-02 23:15:58 +09002741 }
satok3ef8b292010-11-23 06:06:29 +09002742 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002743 }
satokab751aa2010-09-14 19:17:36 +09002744 }
2745
satokf3db1af2010-11-23 13:34:33 +09002746 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2747 InputMethodSubtype subtype) {
2748 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2749 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2750 } else {
2751 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2752 subtypes.add(subtype);
2753 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2754 }
2755 }
2756
satok4e4569d2010-11-19 18:45:53 +09002757 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002758 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002759 @Override
satok4e4569d2010-11-19 18:45:53 +09002760 public List getShortcutInputMethodsAndSubtypes() {
2761 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002762 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002763 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002764 // If there are no selected shortcut subtypes, the framework will try to find
2765 // the most applicable subtype from all subtypes whose mode is
2766 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002767 Pair<InputMethodInfo, InputMethodSubtype> info =
2768 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2769 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002770 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002771 ret.add(info.first);
2772 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002773 }
satok3da92232011-01-11 22:46:30 +09002774 return ret;
satokf3db1af2010-11-23 13:34:33 +09002775 }
satokf3db1af2010-11-23 13:34:33 +09002776 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2777 ret.add(imi);
2778 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2779 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002780 }
2781 }
satokf3db1af2010-11-23 13:34:33 +09002782 return ret;
satok4e4569d2010-11-19 18:45:53 +09002783 }
2784 }
2785
satok42c5a162011-05-26 16:46:14 +09002786 @Override
satokb66d2872010-11-10 01:04:04 +09002787 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2788 synchronized (mMethodMap) {
2789 if (subtype != null && mCurMethodId != null) {
2790 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2791 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2792 if (subtypeId != NOT_A_SUBTYPE_ID) {
2793 setInputMethodLocked(mCurMethodId, subtypeId);
2794 return true;
2795 }
2796 }
2797 return false;
2798 }
2799 }
2800
satok688bd472012-02-09 20:09:17 +09002801 private static class InputMethodAndSubtypeListManager {
2802 private final Context mContext;
2803 private final PackageManager mPm;
2804 private final InputMethodManagerService mImms;
2805 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
2806 mContext = context;
2807 mPm = context.getPackageManager();
2808 mImms = imms;
2809 }
2810
2811 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
2812 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2813 new Comparator<InputMethodInfo>() {
2814 @Override
2815 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2816 if (imi2 == null) return 0;
2817 if (imi1 == null) return 1;
2818 if (mPm == null) {
2819 return imi1.getId().compareTo(imi2.getId());
2820 }
2821 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
2822 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
2823 return imiId1.toString().compareTo(imiId2.toString());
2824 }
2825 });
2826
2827 public ImeSubtypeListItem getNextInputMethod(
2828 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
2829 if (imi == null) {
2830 return null;
2831 }
2832 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
2833 if (imList.size() <= 1) {
2834 return null;
2835 }
2836 final int N = imList.size();
2837 final int currentSubtypeId = subtype != null
2838 ? mImms.getSubtypeIdFromHashCode(imi, subtype.hashCode())
2839 : NOT_A_SUBTYPE_ID;
2840 for (int i = 0; i < N; ++i) {
2841 final ImeSubtypeListItem isli = imList.get(i);
2842 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
2843 if (!onlyCurrentIme) {
2844 return imList.get((i + 1) % N);
2845 }
2846 for (int j = 0; j < N - 1; ++j) {
2847 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
2848 if (candidate.mImi.equals(imi)) {
2849 return candidate;
2850 }
2851 }
2852 return null;
2853 }
2854 }
2855 return null;
2856 }
2857
2858 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
2859 return getSortedInputMethodAndSubtypeList(true, false, false);
2860 }
2861
2862 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
2863 boolean inputShown, boolean isScreenLocked) {
2864 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
2865 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2866 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
2867 if (immis == null || immis.size() == 0) {
2868 return Collections.emptyList();
2869 }
2870 mSortedImmis.clear();
2871 mSortedImmis.putAll(immis);
2872 for (InputMethodInfo imi : mSortedImmis.keySet()) {
2873 if (imi == null) continue;
2874 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
2875 HashSet<String> enabledSubtypeSet = new HashSet<String>();
2876 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2877 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
2878 }
2879 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
2880 final CharSequence imeLabel = imi.loadLabel(mPm);
2881 if (showSubtypes && enabledSubtypeSet.size() > 0) {
2882 final int subtypeCount = imi.getSubtypeCount();
2883 if (DEBUG) {
2884 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2885 }
2886 for (int j = 0; j < subtypeCount; ++j) {
2887 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2888 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2889 // We show all enabled IMEs and subtypes when an IME is shown.
2890 if (enabledSubtypeSet.contains(subtypeHashCode)
2891 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
2892 final CharSequence subtypeLabel =
2893 subtype.overridesImplicitlyEnabledSubtype() ? null
2894 : subtype.getDisplayName(mContext, imi.getPackageName(),
2895 imi.getServiceInfo().applicationInfo);
2896 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2897
2898 // Removing this subtype from enabledSubtypeSet because we no longer
2899 // need to add an entry of this subtype to imList to avoid duplicated
2900 // entries.
2901 enabledSubtypeSet.remove(subtypeHashCode);
2902 }
2903 }
2904 } else {
2905 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
2906 }
2907 }
2908 return imList;
2909 }
2910 }
2911
satokd87c2592010-09-29 11:52:06 +09002912 /**
2913 * Utility class for putting and getting settings for InputMethod
2914 * TODO: Move all putters and getters of settings to this class.
2915 */
2916 private static class InputMethodSettings {
2917 // The string for enabled input method is saved as follows:
2918 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2919 private static final char INPUT_METHOD_SEPARATER = ':';
2920 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002921 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002922 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2923
satok723a27e2010-11-11 14:58:11 +09002924 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002925 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2926
satokdf31ae62011-01-15 06:19:44 +09002927 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002928 private final ContentResolver mResolver;
2929 private final HashMap<String, InputMethodInfo> mMethodMap;
2930 private final ArrayList<InputMethodInfo> mMethodList;
2931
2932 private String mEnabledInputMethodsStrCache;
2933
2934 private static void buildEnabledInputMethodsSettingString(
2935 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2936 String id = pair.first;
2937 ArrayList<String> subtypes = pair.second;
2938 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002939 // Inputmethod and subtypes are saved in the settings as follows:
2940 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2941 for (String subtypeId: subtypes) {
2942 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002943 }
2944 }
2945
2946 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002947 Resources res, ContentResolver resolver,
2948 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2949 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002950 mResolver = resolver;
2951 mMethodMap = methodMap;
2952 mMethodList = methodList;
2953 }
2954
2955 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2956 return createEnabledInputMethodListLocked(
2957 getEnabledInputMethodsAndSubtypeListLocked());
2958 }
2959
satok7f35c8c2010-10-07 21:13:11 +09002960 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002961 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2962 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002963 getEnabledInputMethodsAndSubtypeListLocked());
2964 }
2965
satok67ddf9c2010-11-17 09:45:54 +09002966 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2967 InputMethodInfo imi) {
2968 List<Pair<String, ArrayList<String>>> imsList =
2969 getEnabledInputMethodsAndSubtypeListLocked();
2970 ArrayList<InputMethodSubtype> enabledSubtypes =
2971 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002972 if (imi != null) {
2973 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2974 InputMethodInfo info = mMethodMap.get(imsPair.first);
2975 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002976 final int subtypeCount = info.getSubtypeCount();
2977 for (int i = 0; i < subtypeCount; ++i) {
2978 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002979 for (String s: imsPair.second) {
2980 if (String.valueOf(ims.hashCode()).equals(s)) {
2981 enabledSubtypes.add(ims);
2982 }
satok67ddf9c2010-11-17 09:45:54 +09002983 }
2984 }
satok884ef9a2010-11-18 10:39:46 +09002985 break;
satok67ddf9c2010-11-17 09:45:54 +09002986 }
satok67ddf9c2010-11-17 09:45:54 +09002987 }
2988 }
2989 return enabledSubtypes;
2990 }
2991
satokd87c2592010-09-29 11:52:06 +09002992 // At the initial boot, the settings for input methods are not set,
2993 // so we need to enable IME in that case.
2994 public void enableAllIMEsIfThereIsNoEnabledIME() {
2995 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2996 StringBuilder sb = new StringBuilder();
2997 final int N = mMethodList.size();
2998 for (int i = 0; i < N; i++) {
2999 InputMethodInfo imi = mMethodList.get(i);
3000 Slog.i(TAG, "Adding: " + imi.getId());
3001 if (i > 0) sb.append(':');
3002 sb.append(imi.getId());
3003 }
3004 putEnabledInputMethodsStr(sb.toString());
3005 }
3006 }
3007
satokbb4aa062011-01-19 21:40:27 +09003008 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003009 ArrayList<Pair<String, ArrayList<String>>> imsList
3010 = new ArrayList<Pair<String, ArrayList<String>>>();
3011 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3012 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3013 return imsList;
3014 }
satok723a27e2010-11-11 14:58:11 +09003015 mInputMethodSplitter.setString(enabledInputMethodsStr);
3016 while (mInputMethodSplitter.hasNext()) {
3017 String nextImsStr = mInputMethodSplitter.next();
3018 mSubtypeSplitter.setString(nextImsStr);
3019 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003020 ArrayList<String> subtypeHashes = new ArrayList<String>();
3021 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003022 String imeId = mSubtypeSplitter.next();
3023 while (mSubtypeSplitter.hasNext()) {
3024 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003025 }
3026 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3027 }
3028 }
3029 return imsList;
3030 }
3031
3032 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3033 if (reloadInputMethodStr) {
3034 getEnabledInputMethodsStr();
3035 }
3036 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3037 // Add in the newly enabled input method.
3038 putEnabledInputMethodsStr(id);
3039 } else {
3040 putEnabledInputMethodsStr(
3041 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3042 }
3043 }
3044
3045 /**
3046 * Build and put a string of EnabledInputMethods with removing specified Id.
3047 * @return the specified id was removed or not.
3048 */
3049 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3050 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3051 boolean isRemoved = false;
3052 boolean needsAppendSeparator = false;
3053 for (Pair<String, ArrayList<String>> ims: imsList) {
3054 String curId = ims.first;
3055 if (curId.equals(id)) {
3056 // We are disabling this input method, and it is
3057 // currently enabled. Skip it to remove from the
3058 // new list.
3059 isRemoved = true;
3060 } else {
3061 if (needsAppendSeparator) {
3062 builder.append(INPUT_METHOD_SEPARATER);
3063 } else {
3064 needsAppendSeparator = true;
3065 }
3066 buildEnabledInputMethodsSettingString(builder, ims);
3067 }
3068 }
3069 if (isRemoved) {
3070 // Update the setting with the new list of input methods.
3071 putEnabledInputMethodsStr(builder.toString());
3072 }
3073 return isRemoved;
3074 }
3075
3076 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3077 List<Pair<String, ArrayList<String>>> imsList) {
3078 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3079 for (Pair<String, ArrayList<String>> ims: imsList) {
3080 InputMethodInfo info = mMethodMap.get(ims.first);
3081 if (info != null) {
3082 res.add(info);
3083 }
3084 }
3085 return res;
3086 }
3087
satok7f35c8c2010-10-07 21:13:11 +09003088 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003089 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003090 List<Pair<String, ArrayList<String>>> imsList) {
3091 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3092 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3093 for (Pair<String, ArrayList<String>> ims : imsList) {
3094 InputMethodInfo info = mMethodMap.get(ims.first);
3095 if (info != null) {
3096 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3097 }
3098 }
3099 return res;
3100 }
3101
satokd87c2592010-09-29 11:52:06 +09003102 private void putEnabledInputMethodsStr(String str) {
3103 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3104 mEnabledInputMethodsStrCache = str;
3105 }
3106
3107 private String getEnabledInputMethodsStr() {
3108 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3109 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003110 if (DEBUG) {
3111 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3112 }
satokd87c2592010-09-29 11:52:06 +09003113 return mEnabledInputMethodsStrCache;
3114 }
satok723a27e2010-11-11 14:58:11 +09003115
3116 private void saveSubtypeHistory(
3117 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3118 StringBuilder builder = new StringBuilder();
3119 boolean isImeAdded = false;
3120 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3121 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3122 newSubtypeId);
3123 isImeAdded = true;
3124 }
3125 for (Pair<String, String> ime: savedImes) {
3126 String imeId = ime.first;
3127 String subtypeId = ime.second;
3128 if (TextUtils.isEmpty(subtypeId)) {
3129 subtypeId = NOT_A_SUBTYPE_ID_STR;
3130 }
3131 if (isImeAdded) {
3132 builder.append(INPUT_METHOD_SEPARATER);
3133 } else {
3134 isImeAdded = true;
3135 }
3136 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3137 subtypeId);
3138 }
3139 // Remove the last INPUT_METHOD_SEPARATER
3140 putSubtypeHistoryStr(builder.toString());
3141 }
3142
3143 public void addSubtypeToHistory(String imeId, String subtypeId) {
3144 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3145 for (Pair<String, String> ime: subtypeHistory) {
3146 if (ime.first.equals(imeId)) {
3147 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003148 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003149 + ime.second);
3150 }
3151 // We should break here
3152 subtypeHistory.remove(ime);
3153 break;
3154 }
3155 }
satokbb4aa062011-01-19 21:40:27 +09003156 if (DEBUG) {
3157 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3158 }
satok723a27e2010-11-11 14:58:11 +09003159 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3160 }
3161
3162 private void putSubtypeHistoryStr(String str) {
3163 if (DEBUG) {
3164 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3165 }
3166 Settings.Secure.putString(
3167 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3168 }
3169
3170 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3171 // Gets the first one from the history
3172 return getLastSubtypeForInputMethodLockedInternal(null);
3173 }
3174
3175 public String getLastSubtypeForInputMethodLocked(String imeId) {
3176 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3177 if (ime != null) {
3178 return ime.second;
3179 } else {
3180 return null;
3181 }
3182 }
3183
3184 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3185 List<Pair<String, ArrayList<String>>> enabledImes =
3186 getEnabledInputMethodsAndSubtypeListLocked();
3187 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003188 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003189 final String imeInTheHistory = imeAndSubtype.first;
3190 // If imeId is empty, returns the first IME and subtype in the history
3191 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3192 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003193 final String subtypeHashCode =
3194 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3195 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003196 if (!TextUtils.isEmpty(subtypeHashCode)) {
3197 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003198 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003199 }
3200 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3201 }
3202 }
3203 }
3204 if (DEBUG) {
3205 Slog.d(TAG, "No enabled IME found in the history");
3206 }
3207 return null;
3208 }
3209
satokdf31ae62011-01-15 06:19:44 +09003210 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003211 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3212 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3213 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003214 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3215 if (explicitlyEnabledSubtypes.size() == 0) {
3216 // If there are no explicitly enabled subtypes, applicable subtypes are
3217 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003218 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003219 // If IME is enabled and no subtypes are enabled, applicable subtypes
3220 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003221 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003222 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003223 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003224 if (implicitlySelectedSubtypes != null) {
3225 final int N = implicitlySelectedSubtypes.size();
3226 for (int i = 0; i < N; ++i) {
3227 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3228 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3229 return subtypeHashCode;
3230 }
3231 }
3232 }
3233 }
3234 } else {
satokf6cafb62011-01-17 16:29:02 +09003235 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003236 if (s.equals(subtypeHashCode)) {
3237 // If both imeId and subtypeId are enabled, return subtypeId.
3238 return s;
3239 }
satok723a27e2010-11-11 14:58:11 +09003240 }
3241 }
3242 // If imeId was enabled but subtypeId was disabled.
3243 return NOT_A_SUBTYPE_ID_STR;
3244 }
3245 }
3246 // If both imeId and subtypeId are disabled, return null
3247 return null;
3248 }
3249
3250 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3251 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3252 final String subtypeHistoryStr = getSubtypeHistoryStr();
3253 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3254 return imsList;
3255 }
3256 mInputMethodSplitter.setString(subtypeHistoryStr);
3257 while (mInputMethodSplitter.hasNext()) {
3258 String nextImsStr = mInputMethodSplitter.next();
3259 mSubtypeSplitter.setString(nextImsStr);
3260 if (mSubtypeSplitter.hasNext()) {
3261 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3262 // The first element is ime id.
3263 String imeId = mSubtypeSplitter.next();
3264 while (mSubtypeSplitter.hasNext()) {
3265 subtypeId = mSubtypeSplitter.next();
3266 break;
3267 }
3268 imsList.add(new Pair<String, String>(imeId, subtypeId));
3269 }
3270 }
3271 return imsList;
3272 }
3273
3274 private String getSubtypeHistoryStr() {
3275 if (DEBUG) {
3276 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3277 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3278 }
3279 return Settings.Secure.getString(
3280 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3281 }
3282
3283 public void putSelectedInputMethod(String imeId) {
3284 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3285 }
3286
3287 public void putSelectedSubtype(int subtypeId) {
3288 Settings.Secure.putInt(
3289 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3290 }
satokd87c2592010-09-29 11:52:06 +09003291 }
3292
satoke7c6998e2011-06-03 17:57:59 +09003293 private static class InputMethodFileManager {
3294 private static final String SYSTEM_PATH = "system";
3295 private static final String INPUT_METHOD_PATH = "inputmethod";
3296 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3297 private static final String NODE_SUBTYPES = "subtypes";
3298 private static final String NODE_SUBTYPE = "subtype";
3299 private static final String NODE_IMI = "imi";
3300 private static final String ATTR_ID = "id";
3301 private static final String ATTR_LABEL = "label";
3302 private static final String ATTR_ICON = "icon";
3303 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3304 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3305 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3306 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3307 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3308 private final HashMap<String, InputMethodInfo> mMethodMap;
3309 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3310 new HashMap<String, List<InputMethodSubtype>>();
3311 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3312 if (methodMap == null) {
3313 throw new NullPointerException("methodMap is null");
3314 }
3315 mMethodMap = methodMap;
3316 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3317 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3318 if (!inputMethodDir.mkdirs()) {
3319 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3320 }
3321 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3322 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3323 if (!subtypeFile.exists()) {
3324 // If "subtypes.xml" doesn't exist, create a blank file.
3325 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3326 methodMap);
3327 } else {
3328 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3329 }
3330 }
3331
3332 private void deleteAllInputMethodSubtypes(String imiId) {
3333 synchronized (mMethodMap) {
3334 mSubtypesMap.remove(imiId);
3335 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3336 mMethodMap);
3337 }
3338 }
3339
3340 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003341 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003342 synchronized (mMethodMap) {
3343 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3344 final int N = additionalSubtypes.length;
3345 for (int i = 0; i < N; ++i) {
3346 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003347 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003348 subtypes.add(subtype);
3349 }
3350 }
satok4a28bde2011-06-29 21:03:40 +09003351 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003352 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3353 mMethodMap);
3354 }
3355 }
3356
3357 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3358 synchronized (mMethodMap) {
3359 return mSubtypesMap;
3360 }
3361 }
3362
3363 private static void writeAdditionalInputMethodSubtypes(
3364 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3365 HashMap<String, InputMethodInfo> methodMap) {
3366 // Safety net for the case that this function is called before methodMap is set.
3367 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3368 FileOutputStream fos = null;
3369 try {
3370 fos = subtypesFile.startWrite();
3371 final XmlSerializer out = new FastXmlSerializer();
3372 out.setOutput(fos, "utf-8");
3373 out.startDocument(null, true);
3374 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3375 out.startTag(null, NODE_SUBTYPES);
3376 for (String imiId : allSubtypes.keySet()) {
3377 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3378 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3379 continue;
3380 }
3381 out.startTag(null, NODE_IMI);
3382 out.attribute(null, ATTR_ID, imiId);
3383 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3384 final int N = subtypesList.size();
3385 for (int i = 0; i < N; ++i) {
3386 final InputMethodSubtype subtype = subtypesList.get(i);
3387 out.startTag(null, NODE_SUBTYPE);
3388 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3389 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3390 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3391 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3392 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3393 out.attribute(null, ATTR_IS_AUXILIARY,
3394 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3395 out.endTag(null, NODE_SUBTYPE);
3396 }
3397 out.endTag(null, NODE_IMI);
3398 }
3399 out.endTag(null, NODE_SUBTYPES);
3400 out.endDocument();
3401 subtypesFile.finishWrite(fos);
3402 } catch (java.io.IOException e) {
3403 Slog.w(TAG, "Error writing subtypes", e);
3404 if (fos != null) {
3405 subtypesFile.failWrite(fos);
3406 }
3407 }
3408 }
3409
3410 private static void readAdditionalInputMethodSubtypes(
3411 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3412 if (allSubtypes == null || subtypesFile == null) return;
3413 allSubtypes.clear();
3414 FileInputStream fis = null;
3415 try {
3416 fis = subtypesFile.openRead();
3417 final XmlPullParser parser = Xml.newPullParser();
3418 parser.setInput(fis, null);
3419 int type = parser.getEventType();
3420 // Skip parsing until START_TAG
3421 while ((type = parser.next()) != XmlPullParser.START_TAG
3422 && type != XmlPullParser.END_DOCUMENT) {}
3423 String firstNodeName = parser.getName();
3424 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3425 throw new XmlPullParserException("Xml doesn't start with subtypes");
3426 }
3427 final int depth =parser.getDepth();
3428 String currentImiId = null;
3429 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3430 while (((type = parser.next()) != XmlPullParser.END_TAG
3431 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3432 if (type != XmlPullParser.START_TAG)
3433 continue;
3434 final String nodeName = parser.getName();
3435 if (NODE_IMI.equals(nodeName)) {
3436 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3437 if (TextUtils.isEmpty(currentImiId)) {
3438 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3439 continue;
3440 }
3441 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3442 allSubtypes.put(currentImiId, tempSubtypesArray);
3443 } else if (NODE_SUBTYPE.equals(nodeName)) {
3444 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3445 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3446 continue;
3447 }
3448 final int icon = Integer.valueOf(
3449 parser.getAttributeValue(null, ATTR_ICON));
3450 final int label = Integer.valueOf(
3451 parser.getAttributeValue(null, ATTR_LABEL));
3452 final String imeSubtypeLocale =
3453 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3454 final String imeSubtypeMode =
3455 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3456 final String imeSubtypeExtraValue =
3457 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003458 final boolean isAuxiliary = "1".equals(String.valueOf(
3459 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003460 final InputMethodSubtype subtype =
3461 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3462 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3463 tempSubtypesArray.add(subtype);
3464 }
3465 }
3466 } catch (XmlPullParserException e) {
3467 Slog.w(TAG, "Error reading subtypes: " + e);
3468 return;
3469 } catch (java.io.IOException e) {
3470 Slog.w(TAG, "Error reading subtypes: " + e);
3471 return;
3472 } catch (NumberFormatException e) {
3473 Slog.w(TAG, "Error reading subtypes: " + e);
3474 return;
3475 } finally {
3476 if (fis != null) {
3477 try {
3478 fis.close();
3479 } catch (java.io.IOException e1) {
3480 Slog.w(TAG, "Failed to close.");
3481 }
3482 }
3483 }
3484 }
3485 }
3486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 @Override
3490 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3491 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3492 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3495 + Binder.getCallingPid()
3496 + ", uid=" + Binder.getCallingUid());
3497 return;
3498 }
3499
3500 IInputMethod method;
3501 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 synchronized (mMethodMap) {
3506 p.println("Current Input Method Manager state:");
3507 int N = mMethodList.size();
3508 p.println(" Input Methods:");
3509 for (int i=0; i<N; i++) {
3510 InputMethodInfo info = mMethodList.get(i);
3511 p.println(" InputMethod #" + i + ":");
3512 info.dump(p, " ");
3513 }
3514 p.println(" Clients:");
3515 for (ClientState ci : mClients.values()) {
3516 p.println(" Client " + ci + ":");
3517 p.println(" client=" + ci.client);
3518 p.println(" inputContext=" + ci.inputContext);
3519 p.println(" sessionRequested=" + ci.sessionRequested);
3520 p.println(" curSession=" + ci.curSession);
3521 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003522 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003524 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3525 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3527 + " mBoundToMethod=" + mBoundToMethod);
3528 p.println(" mCurToken=" + mCurToken);
3529 p.println(" mCurIntent=" + mCurIntent);
3530 method = mCurMethod;
3531 p.println(" mCurMethod=" + mCurMethod);
3532 p.println(" mEnabledSession=" + mEnabledSession);
3533 p.println(" mShowRequested=" + mShowRequested
3534 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3535 + " mShowForced=" + mShowForced
3536 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003537 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003539
Jeff Brownb88102f2010-09-08 11:49:43 -07003540 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 pw.flush();
3543 try {
3544 client.client.asBinder().dump(fd, args);
3545 } catch (RemoteException e) {
3546 p.println("Input method client dead: " + e);
3547 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003548 } else {
3549 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003551
Jeff Brownb88102f2010-09-08 11:49:43 -07003552 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 pw.flush();
3555 try {
3556 method.asBinder().dump(fd, args);
3557 } catch (RemoteException e) {
3558 p.println("Input method service dead: " + e);
3559 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003560 } else {
3561 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563 }
3564}