blob: 8037d7a41682ae216ed7df4485d5a1bd68f0675b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090020import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090022import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.view.IInputContext;
24import com.android.internal.view.IInputMethod;
25import com.android.internal.view.IInputMethodCallback;
26import com.android.internal.view.IInputMethodClient;
27import com.android.internal.view.IInputMethodManager;
28import com.android.internal.view.IInputMethodSession;
29import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
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;
51import android.content.pm.ResolveInfo;
52import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070053import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.res.Resources;
55import android.content.res.TypedArray;
56import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090057import android.graphics.BitmapFactory;
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;
83import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090084import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.inputmethod.InputBinding;
86import android.view.inputmethod.InputMethod;
87import android.view.inputmethod.InputMethodInfo;
88import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090089import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
satoke7c6998e2011-06-03 17:57:59 +090091import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090093import java.io.FileInputStream;
94import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.IOException;
96import java.io.PrintWriter;
97import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +090098import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +090099import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900101import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900103import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900104import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106/**
107 * This class provides a system service that manages input methods.
108 */
109public class InputMethodManagerService extends IInputMethodManager.Stub
110 implements ServiceConnection, Handler.Callback {
111 static final boolean DEBUG = false;
112 static final String TAG = "InputManagerService";
113
114 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900115 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900116 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900117 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 static final int MSG_UNBIND_INPUT = 1000;
120 static final int MSG_BIND_INPUT = 1010;
121 static final int MSG_SHOW_SOFT_INPUT = 1020;
122 static final int MSG_HIDE_SOFT_INPUT = 1030;
123 static final int MSG_ATTACH_TOKEN = 1040;
124 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 static final int MSG_START_INPUT = 2000;
127 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 static final int MSG_UNBIND_METHOD = 3000;
130 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
satokf9f01002011-05-19 21:31:50 +0900134 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
135
satokab751aa2010-09-14 19:17:36 +0900136 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900137 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900138 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
139 private static final String SUBTYPE_MODE_VOICE = "voice";
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800142 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900144 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700146 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 final IWindowManager mIWindowManager;
148 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900149 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 // All known input methods. mMethodMap also serves as the global
154 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900155 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
156 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900157 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
158 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
satok7cfc0ed2011-06-20 21:29:36 +0900160 // Ongoing notification
161 private final NotificationManager mNotificationManager;
162 private final Notification mImeSwitcherNotification;
163 private final PendingIntent mImeSwitchPendingIntent;
164 private final boolean mShowOngoingImeSwitcherForPhones;
165 private boolean mNotificationShown;
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 class SessionState {
168 final ClientState client;
169 final IInputMethod method;
170 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 @Override
173 public String toString() {
174 return "SessionState{uid " + client.uid + " pid " + client.pid
175 + " method " + Integer.toHexString(
176 System.identityHashCode(method))
177 + " session " + Integer.toHexString(
178 System.identityHashCode(session))
179 + "}";
180 }
181
182 SessionState(ClientState _client, IInputMethod _method,
183 IInputMethodSession _session) {
184 client = _client;
185 method = _method;
186 session = _session;
187 }
188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 class ClientState {
191 final IInputMethodClient client;
192 final IInputContext inputContext;
193 final int uid;
194 final int pid;
195 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 boolean sessionRequested;
198 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 @Override
201 public String toString() {
202 return "ClientState{" + Integer.toHexString(
203 System.identityHashCode(this)) + " uid " + uid
204 + " pid " + pid + "}";
205 }
206
207 ClientState(IInputMethodClient _client, IInputContext _inputContext,
208 int _uid, int _pid) {
209 client = _client;
210 inputContext = _inputContext;
211 uid = _uid;
212 pid = _pid;
213 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
214 }
215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 final HashMap<IBinder, ClientState> mClients
218 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700221 * Set once the system is ready to run third party code.
222 */
223 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * Id of the currently selected input method.
227 */
228 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * The current binding sequence number, incremented every time there is
232 * a new bind performed.
233 */
234 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The client that is currently bound to an input method.
238 */
239 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700242 * The last window token that gained focus.
243 */
244 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * The input context last provided by the current client.
248 */
249 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * The attributes last provided by the current client.
253 */
254 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * The input method ID of the input method service that we are currently
258 * connected to or in the process of connecting to.
259 */
260 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
satokab751aa2010-09-14 19:17:36 +0900263 * The current subtype of the current input method.
264 */
265 private InputMethodSubtype mCurrentSubtype;
266
satok4e4569d2010-11-19 18:45:53 +0900267 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900268 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
269 mShortcutInputMethodsAndSubtypes =
270 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900271
272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * Set to true if our ServiceConnection is currently actively bound to
274 * a service (whether or not we have gotten its IBinder back yet).
275 */
276 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Set if the client has asked for the input method to be shown.
280 */
281 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Set if we were explicitly told to show the input method.
285 */
286 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Set if we were forced to be shown.
290 */
291 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Set if we last told the input method to show itself.
295 */
296 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * The Intent used to connect to the current input method.
300 */
301 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * The token we have made for the currently active input method, to
305 * identify it in the future.
306 */
307 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * If non-null, this is the input method service we are currently connected
311 * to.
312 */
313 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * Time that we last initiated a bind to the input method, to determine
317 * if we should try to disconnect and reconnect to it.
318 */
319 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Have we called mCurMethod.bindInput()?
323 */
324 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Currently enabled session. Only touched by service thread, not
328 * protected by a lock.
329 */
330 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * True if the screen is on. The value is true initially.
334 */
335 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
Joe Onorato857fd9b2011-01-27 15:08:35 -0800337 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
338 int mImeWindowVis;
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 AlertDialog.Builder mDialogBuilder;
341 AlertDialog mSwitchingDialog;
342 InputMethodInfo[] mIms;
343 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900344 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 class SettingsObserver extends ContentObserver {
347 SettingsObserver(Handler handler) {
348 super(handler);
349 ContentResolver resolver = mContext.getContentResolver();
350 resolver.registerContentObserver(Settings.Secure.getUriFor(
351 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900352 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900353 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
354 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900355 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 @Override public void onChange(boolean selfChange) {
359 synchronized (mMethodMap) {
360 updateFromSettingsLocked();
361 }
362 }
363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
366 @Override
367 public void onReceive(Context context, Intent intent) {
368 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
369 mScreenOn = true;
370 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
371 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700372 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
373 hideInputMethodMenu();
374 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800376 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378
379 // Inform the current client of the change in active status
380 try {
381 if (mCurClient != null && mCurClient.client != null) {
382 mCurClient.client.setActive(mScreenOn);
383 }
384 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800385 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 + mCurClient.pid + " uid " + mCurClient.uid);
387 }
388 }
389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800391 class MyPackageMonitor extends PackageMonitor {
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800394 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
398 final int N = mMethodList.size();
399 if (curInputMethodId != null) {
400 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 InputMethodInfo imi = mMethodList.get(i);
402 if (imi.getId().equals(curInputMethodId)) {
403 for (String pkg : packages) {
404 if (imi.getPackageName().equals(pkg)) {
405 if (!doit) {
406 return true;
407 }
satok723a27e2010-11-11 14:58:11 +0900408 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800409 chooseNewDefaultIMELocked();
410 return true;
411 }
412 }
413 }
414 }
415 }
416 }
417 return false;
418 }
419
420 @Override
421 public void onSomePackagesChanged() {
422 synchronized (mMethodMap) {
423 InputMethodInfo curIm = null;
424 String curInputMethodId = Settings.Secure.getString(mContext
425 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
426 final int N = mMethodList.size();
427 if (curInputMethodId != null) {
428 for (int i=0; i<N; i++) {
429 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900430 final String imiId = imi.getId();
431 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 curIm = imi;
433 }
satoke7c6998e2011-06-03 17:57:59 +0900434
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800435 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900436 if (isPackageModified(imi.getPackageName())) {
437 mFileManager.deleteAllInputMethodSubtypes(imiId);
438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 if (change == PACKAGE_TEMPORARY_CHANGE
440 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800441 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 + imi.getComponent());
443 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 buildInputMethodListLocked(mMethodList, mMethodMap);
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800452 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 int change = isPackageDisappearing(curIm.getPackageName());
454 if (change == PACKAGE_TEMPORARY_CHANGE
455 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800456 ServiceInfo si = null;
457 try {
458 si = mContext.getPackageManager().getServiceInfo(
459 curIm.getComponent(), 0);
460 } catch (PackageManager.NameNotFoundException ex) {
461 }
462 if (si == null) {
463 // Uh oh, current input method is no longer around!
464 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800465 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800466 mImeWindowVis = 0;
467 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
468 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800470 changed = true;
471 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800472 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900473 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 }
475 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800476 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800477 }
satokab751aa2010-09-14 19:17:36 +0900478
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800479 if (curIm == null) {
480 // We currently don't have a default input method... is
481 // one now available?
482 changed = chooseNewDefaultIMELocked();
483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800484
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800485 if (changed) {
486 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
489 }
490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 class MethodCallback extends IInputMethodCallback.Stub {
493 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 MethodCallback(IInputMethod method) {
496 mMethod = method;
497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
satoke7c6998e2011-06-03 17:57:59 +0900499 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 public void finishedEvent(int seq, boolean handled) throws RemoteException {
501 }
502
satoke7c6998e2011-06-03 17:57:59 +0900503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public void sessionCreated(IInputMethodSession session) throws RemoteException {
505 onSessionCreated(mMethod, session);
506 }
507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800508
Joe Onorato089de882010-04-12 08:18:45 -0700509 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800511 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 mHandler = new Handler(this);
513 mIWindowManager = IWindowManager.Stub.asInterface(
514 ServiceManager.getService(Context.WINDOW_SERVICE));
515 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900516 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 public void executeMessage(Message msg) {
518 handleMessage(msg);
519 }
520 });
satok7cfc0ed2011-06-20 21:29:36 +0900521
522 mNotificationManager = (NotificationManager)
523 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
524 mImeSwitcherNotification = new Notification();
525 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
526 mImeSwitcherNotification.when = 0;
527 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
528 mImeSwitcherNotification.tickerText = null;
529 mImeSwitcherNotification.defaults = 0; // please be quiet
530 mImeSwitcherNotification.sound = null;
531 mImeSwitcherNotification.vibrate = null;
532 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
533 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
534 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
535 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
536 mImeSwitchPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
537 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
538 com.android.internal.R.bool.show_ongoing_ime_switcher);
539
satoke7c6998e2011-06-03 17:57:59 +0900540 synchronized (mMethodMap) {
541 mFileManager = new InputMethodFileManager(mMethodMap);
542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800544 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 IntentFilter screenOnOffFilt = new IntentFilter();
547 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
548 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700549 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800551
satok913a8922010-08-26 21:53:41 +0900552 mStatusBar = statusBar;
553 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900554 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900555
satokd87c2592010-09-29 11:52:06 +0900556 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900557 mSettings = new InputMethodSettings(
558 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900560 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
satokd87c2592010-09-29 11:52:06 +0900562 if (TextUtils.isEmpty(Settings.Secure.getString(
563 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900565 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
567 try {
satokd87c2592010-09-29 11:52:06 +0900568 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 imi.getPackageName(), 0).getResources();
570 if (res.getBoolean(imi.getIsDefaultResourceId())) {
571 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800572 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 } catch (PackageManager.NameNotFoundException ex) {
575 } catch (Resources.NotFoundException ex) {
576 }
577 }
578 }
satokd87c2592010-09-29 11:52:06 +0900579 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800581 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900584 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 mSettingsObserver = new SettingsObserver(mHandler);
589 updateFromSettingsLocked();
590 }
591
592 @Override
593 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
594 throws RemoteException {
595 try {
596 return super.onTransact(code, data, reply, flags);
597 } catch (RuntimeException e) {
598 // The input method manager only throws security exceptions, so let's
599 // log all others.
600 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800601 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603 throw e;
604 }
605 }
606
607 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700608 synchronized (mMethodMap) {
609 if (!mSystemReady) {
610 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700611 try {
612 startInputInnerLocked();
613 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800614 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700615 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700616 }
617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800619
satoke7c6998e2011-06-03 17:57:59 +0900620 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public List<InputMethodInfo> getInputMethodList() {
622 synchronized (mMethodMap) {
623 return new ArrayList<InputMethodInfo>(mMethodList);
624 }
625 }
626
satoke7c6998e2011-06-03 17:57:59 +0900627 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 public List<InputMethodInfo> getEnabledInputMethodList() {
629 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900630 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632 }
633
satokbb4aa062011-01-19 21:40:27 +0900634 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
635 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
636 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
637 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
638 for (InputMethodInfo imi: getEnabledInputMethodList()) {
639 enabledInputMethodAndSubtypes.put(
640 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
641 }
642 return enabledInputMethodAndSubtypes;
643 }
644
645 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
646 boolean allowsImplicitlySelectedSubtypes) {
647 if (imi == null && mCurMethodId != null) {
648 imi = mMethodMap.get(mCurMethodId);
649 }
satok7265d9b2011-02-14 15:47:30 +0900650 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900651 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900652 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
653 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900654 }
satok7265d9b2011-02-14 15:47:30 +0900655 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900656 }
657
satoke7c6998e2011-06-03 17:57:59 +0900658 @Override
satok16331c82010-12-20 23:48:46 +0900659 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
660 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900661 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900662 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900663 }
664 }
665
satoke7c6998e2011-06-03 17:57:59 +0900666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 public void addClient(IInputMethodClient client,
668 IInputContext inputContext, int uid, int pid) {
669 synchronized (mMethodMap) {
670 mClients.put(client.asBinder(), new ClientState(client,
671 inputContext, uid, pid));
672 }
673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800674
satoke7c6998e2011-06-03 17:57:59 +0900675 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 public void removeClient(IInputMethodClient client) {
677 synchronized (mMethodMap) {
678 mClients.remove(client.asBinder());
679 }
680 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 void executeOrSendMessage(IInterface target, Message msg) {
683 if (target.asBinder() instanceof Binder) {
684 mCaller.sendMessage(msg);
685 } else {
686 handleMessage(msg);
687 msg.recycle();
688 }
689 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800690
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700691 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800693 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 + mCurClient.client.asBinder());
695 if (mBoundToMethod) {
696 mBoundToMethod = false;
697 if (mCurMethod != null) {
698 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
699 MSG_UNBIND_INPUT, mCurMethod));
700 }
701 }
702 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
703 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
704 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 // Call setActive(false) on the old client
707 try {
708 mCurClient.client.setActive(false);
709 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + mCurClient.pid + " uid " + mCurClient.uid);
712 }
713 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
The Android Open Source Project10592532009-03-18 17:39:46 -0700715 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 private int getImeShowFlags() {
720 int flags = 0;
721 if (mShowForced) {
722 flags |= InputMethod.SHOW_FORCED
723 | InputMethod.SHOW_EXPLICIT;
724 } else if (mShowExplicitlyRequested) {
725 flags |= InputMethod.SHOW_EXPLICIT;
726 }
727 return flags;
728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 private int getAppShowFlags() {
731 int flags = 0;
732 if (mShowForced) {
733 flags |= InputMethodManager.SHOW_FORCED;
734 } else if (!mShowExplicitlyRequested) {
735 flags |= InputMethodManager.SHOW_IMPLICIT;
736 }
737 return flags;
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
741 if (!mBoundToMethod) {
742 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
743 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
744 mBoundToMethod = true;
745 }
746 final SessionState session = mCurClient.curSession;
747 if (initial) {
748 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
749 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
750 } else {
751 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
752 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
753 }
754 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800755 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800756 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758 return needResult
759 ? new InputBindResult(session.session, mCurId, mCurSeq)
760 : null;
761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 InputBindResult startInputLocked(IInputMethodClient client,
764 IInputContext inputContext, EditorInfo attribute,
765 boolean initial, boolean needResult) {
766 // If no method is currently selected, do nothing.
767 if (mCurMethodId == null) {
768 return mNoBinding;
769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 ClientState cs = mClients.get(client.asBinder());
772 if (cs == null) {
773 throw new IllegalArgumentException("unknown client "
774 + client.asBinder());
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 try {
778 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
779 // Check with the window manager to make sure this client actually
780 // has a window with focus. If not, reject. This is thread safe
781 // because if the focus changes some time before or after, the
782 // next client receiving focus that has any interest in input will
783 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800784 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
786 return null;
787 }
788 } catch (RemoteException e) {
789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 if (mCurClient != cs) {
792 // If the client is changing, we need to switch over to the new
793 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700794 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800795 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 + cs.client.asBinder());
797
798 // If the screen is on, inform the new client it is active
799 if (mScreenOn) {
800 try {
801 cs.client.setActive(mScreenOn);
802 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800803 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 + cs.pid + " uid " + cs.uid);
805 }
806 }
807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 // Bump up the sequence for this client and attach it.
810 mCurSeq++;
811 if (mCurSeq <= 0) mCurSeq = 1;
812 mCurClient = cs;
813 mCurInputContext = inputContext;
814 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 // Check if the input method is changing.
817 if (mCurId != null && mCurId.equals(mCurMethodId)) {
818 if (cs.curSession != null) {
819 // Fast case: if we are already connected to the input method,
820 // then just return it.
821 return attachNewInputLocked(initial, needResult);
822 }
823 if (mHaveConnection) {
824 if (mCurMethod != null) {
825 if (!cs.sessionRequested) {
826 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
829 MSG_CREATE_SESSION, mCurMethod,
830 new MethodCallback(mCurMethod)));
831 }
832 // Return to client, and we will get back with it when
833 // we have had a session made for it.
834 return new InputBindResult(null, mCurId, mCurSeq);
835 } else if (SystemClock.uptimeMillis()
836 < (mLastBindTime+TIME_TO_RECONNECT)) {
837 // In this case we have connected to the service, but
838 // don't yet have its interface. If it hasn't been too
839 // long since we did the connection, we'll return to
840 // the client and wait to get the service interface so
841 // we can report back. If it has been too long, we want
842 // to fall through so we can try a disconnect/reconnect
843 // to see if we can get back in touch with the service.
844 return new InputBindResult(null, mCurId, mCurSeq);
845 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800846 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
847 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 }
850 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700852 return startInputInnerLocked();
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700855 InputBindResult startInputInnerLocked() {
856 if (mCurMethodId == null) {
857 return mNoBinding;
858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700860 if (!mSystemReady) {
861 // If the system is not yet ready, we shouldn't be running third
862 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700863 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 InputMethodInfo info = mMethodMap.get(mCurMethodId);
867 if (info == null) {
868 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700871 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
874 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700875 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
876 com.android.internal.R.string.input_method_binding_label);
877 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
878 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
880 mLastBindTime = SystemClock.uptimeMillis();
881 mHaveConnection = true;
882 mCurId = info.getId();
883 mCurToken = new Binder();
884 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800885 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 mIWindowManager.addWindowToken(mCurToken,
887 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
888 } catch (RemoteException e) {
889 }
890 return new InputBindResult(null, mCurId, mCurSeq);
891 } else {
892 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800893 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 + mCurIntent);
895 }
896 return null;
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
satoke7c6998e2011-06-03 17:57:59 +0900899 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 public InputBindResult startInput(IInputMethodClient client,
901 IInputContext inputContext, EditorInfo attribute,
902 boolean initial, boolean needResult) {
903 synchronized (mMethodMap) {
904 final long ident = Binder.clearCallingIdentity();
905 try {
906 return startInputLocked(client, inputContext, attribute,
907 initial, needResult);
908 } finally {
909 Binder.restoreCallingIdentity(ident);
910 }
911 }
912 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
satoke7c6998e2011-06-03 17:57:59 +0900914 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 public void finishInput(IInputMethodClient client) {
916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
satoke7c6998e2011-06-03 17:57:59 +0900918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public void onServiceConnected(ComponentName name, IBinder service) {
920 synchronized (mMethodMap) {
921 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
922 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700923 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800924 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700925 unbindCurrentMethodLocked(false);
926 return;
927 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800928 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700929 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
930 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800932 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700933 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 MSG_CREATE_SESSION, mCurMethod,
936 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 }
938 }
939 }
940 }
941
942 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
943 synchronized (mMethodMap) {
944 if (mCurMethod != null && method != null
945 && mCurMethod.asBinder() == method.asBinder()) {
946 if (mCurClient != null) {
947 mCurClient.curSession = new SessionState(mCurClient,
948 method, session);
949 mCurClient.sessionRequested = false;
950 InputBindResult res = attachNewInputLocked(true, true);
951 if (res.method != null) {
952 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
953 MSG_BIND_METHOD, mCurClient.client, res));
954 }
955 }
956 }
957 }
958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800959
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700960 void unbindCurrentMethodLocked(boolean reportToClient) {
961 if (mHaveConnection) {
962 mContext.unbindService(this);
963 mHaveConnection = false;
964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700966 if (mCurToken != null) {
967 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800968 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700969 mIWindowManager.removeWindowToken(mCurToken);
970 } catch (RemoteException e) {
971 }
972 mCurToken = null;
973 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800974
The Android Open Source Project10592532009-03-18 17:39:46 -0700975 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700978 if (reportToClient && mCurClient != null) {
979 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
980 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
981 }
982 }
983
Devin Taylor0c33ed22010-02-23 13:26:46 -0600984 private void finishSession(SessionState sessionState) {
985 if (sessionState != null && sessionState.session != null) {
986 try {
987 sessionState.session.finishSession();
988 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700989 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600990 }
991 }
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700994 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (mCurMethod != null) {
996 for (ClientState cs : mClients.values()) {
997 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600998 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 cs.curSession = null;
1000 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001001
1002 finishSession(mEnabledSession);
1003 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 mCurMethod = null;
1005 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001006 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public void onServiceDisconnected(ComponentName name) {
1011 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001012 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 + " mCurIntent=" + mCurIntent);
1014 if (mCurMethod != null && mCurIntent != null
1015 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001016 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 // We consider this to be a new bind attempt, since the system
1018 // should now try to restart the service for us.
1019 mLastBindTime = SystemClock.uptimeMillis();
1020 mShowRequested = mInputShown;
1021 mInputShown = false;
1022 if (mCurClient != null) {
1023 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1024 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1025 }
1026 }
1027 }
1028 }
1029
satokf9f01002011-05-19 21:31:50 +09001030 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001032 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 long ident = Binder.clearCallingIdentity();
1034 try {
1035 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001036 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 return;
1038 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 synchronized (mMethodMap) {
1041 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001042 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001043 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001046 mStatusBar.setIcon("ime", packageName, iconId, 0);
1047 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
1050 } finally {
1051 Binder.restoreCallingIdentity(ident);
1052 }
1053 }
1054
satok7cfc0ed2011-06-20 21:29:36 +09001055 private boolean needsToShowImeSwitchOngoingNotification() {
1056 if (!mShowOngoingImeSwitcherForPhones) return false;
1057 synchronized (mMethodMap) {
1058 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1059 final int N = imis.size();
1060 int count = 0;
1061 for(int i = 0; i < N; ++i) {
1062 final InputMethodInfo imi = imis.get(i);
1063 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1064 imi, true);
1065 final int subtypeCount = subtypes.size();
1066 if (subtypeCount == 0) {
1067 ++count;
1068 } else {
1069 for (int j = 0; j < subtypeCount; ++j) {
1070 if (!subtypes.get(j).isAuxiliary()) {
1071 ++count;
1072 }
1073 }
1074 }
1075 if (count > 1) return true;
1076 }
1077 }
1078 return false;
1079 }
1080
satokf9f01002011-05-19 21:31:50 +09001081 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001082 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001083 int uid = Binder.getCallingUid();
1084 long ident = Binder.clearCallingIdentity();
1085 try {
1086 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001087 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001088 return;
1089 }
1090
1091 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001092 mImeWindowVis = vis;
1093 mBackDisposition = backDisposition;
1094 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001095 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1096 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1097 final PackageManager pm = mContext.getPackageManager();
1098 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1099 final CharSequence title = mRes.getText(
1100 com.android.internal.R.string.select_input_method);
1101 mImeSwitcherNotification.setLatestEventInfo(
1102 mContext, title, label, mImeSwitchPendingIntent);
1103 mNotificationManager.notify(
1104 com.android.internal.R.string.select_input_method,
1105 mImeSwitcherNotification);
1106 mNotificationShown = true;
1107 } else {
1108 if (mNotificationShown) {
1109 mNotificationManager.cancel(
1110 com.android.internal.R.string.select_input_method);
1111 mNotificationShown = false;
1112 }
1113 }
satok06487a52010-10-29 11:37:18 +09001114 }
1115 } finally {
1116 Binder.restoreCallingIdentity(ident);
1117 }
1118 }
1119
satoke7c6998e2011-06-03 17:57:59 +09001120 @Override
satokf9f01002011-05-19 21:31:50 +09001121 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1122 synchronized (mMethodMap) {
1123 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1124 for (int i = 0; i < spans.length; ++i) {
1125 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001126 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001127 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001128 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001129 }
1130 }
1131 }
1132 }
1133
satoke7c6998e2011-06-03 17:57:59 +09001134 @Override
satokf9f01002011-05-19 21:31:50 +09001135 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1136 synchronized (mMethodMap) {
1137 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1138 // TODO: Do not send the intent if the process of the targetImi is already dead.
1139 if (targetImi != null) {
1140 final String[] suggestions = span.getSuggestions();
1141 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001142 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001143 final Intent intent = new Intent();
1144 // Ensures that only a class in the original IME package will receive the
1145 // notification.
satok42c5a162011-05-26 16:46:14 +09001146 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001147 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1148 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1149 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1150 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1151 mContext.sendBroadcast(intent);
1152 return true;
1153 }
1154 }
1155 return false;
1156 }
1157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001159 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1160 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1161 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1162 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001164 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001165 // There is no input method selected, try to choose new applicable input method.
1166 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1167 id = Settings.Secure.getString(mContext.getContentResolver(),
1168 Settings.Secure.DEFAULT_INPUT_METHOD);
1169 }
1170 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 try {
satokab751aa2010-09-14 19:17:36 +09001172 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001174 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001175 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001176 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
satokf3db1af2010-11-23 13:34:33 +09001178 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001179 } else {
1180 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001181 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001182 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
satokab751aa2010-09-14 19:17:36 +09001186 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 InputMethodInfo info = mMethodMap.get(id);
1188 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001189 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001193 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001194 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1195 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001196 }
1197 if (subtype != mCurrentSubtype) {
1198 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001199 if (subtype != null) {
1200 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1201 }
satokcd7cd292010-11-20 15:46:23 +09001202 if (mCurMethod != null) {
1203 try {
satoke40dea02011-01-30 01:14:02 +09001204 final Configuration conf = mRes.getConfiguration();
1205 final boolean haveHardKeyboard = conf.keyboard
1206 != Configuration.KEYBOARD_NOKEYS;
1207 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001208 && conf.hardKeyboardHidden
1209 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001210 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1211 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1212 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001213 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1214 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001215 // If subtype is null, try to find the most applicable one from
1216 // getCurrentInputMethodSubtype.
1217 if (subtype == null) {
1218 subtype = getCurrentInputMethodSubtype();
1219 }
1220 mCurMethod.changeInputMethodSubtype(subtype);
1221 } catch (RemoteException e) {
1222 return;
satokab751aa2010-09-14 19:17:36 +09001223 }
1224 }
1225 }
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 return;
1228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 final long ident = Binder.clearCallingIdentity();
1231 try {
satokab751aa2010-09-14 19:17:36 +09001232 // Set a subtype to this input method.
1233 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001234 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1235 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1236 // because mCurMethodId is stored as a history in
1237 // setSelectedInputMethodAndSubtypeLocked().
1238 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239
1240 if (ActivityManagerNative.isSystemReady()) {
1241 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001242 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 intent.putExtra("input_method_id", id);
1244 mContext.sendBroadcast(intent);
1245 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001246 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 } finally {
1248 Binder.restoreCallingIdentity(ident);
1249 }
1250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
satok42c5a162011-05-26 16:46:14 +09001252 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001253 public boolean showSoftInput(IInputMethodClient client, int flags,
1254 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001255 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 long ident = Binder.clearCallingIdentity();
1257 try {
1258 synchronized (mMethodMap) {
1259 if (mCurClient == null || client == null
1260 || mCurClient.client.asBinder() != client.asBinder()) {
1261 try {
1262 // We need to check if this is the current client with
1263 // focus in the window manager, to allow this call to
1264 // be made before input is started in it.
1265 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001266 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001267 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001270 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 }
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Joe Onorato8a9b2202010-02-26 18:56:32 -08001274 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001275 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 } finally {
1278 Binder.restoreCallingIdentity(ident);
1279 }
1280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001281
The Android Open Source Project4df24232009-03-05 14:34:35 -08001282 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 mShowRequested = true;
1284 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1285 mShowExplicitlyRequested = true;
1286 }
1287 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1288 mShowExplicitlyRequested = true;
1289 mShowForced = true;
1290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
Dianne Hackborncc278702009-09-02 23:07:23 -07001292 if (!mSystemReady) {
1293 return false;
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
The Android Open Source Project4df24232009-03-05 14:34:35 -08001296 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001298 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1299 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1300 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001302 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 } else if (mHaveConnection && SystemClock.uptimeMillis()
1304 < (mLastBindTime+TIME_TO_RECONNECT)) {
1305 // The client has asked to have the input method shown, but
1306 // we have been sitting here too long with a connection to the
1307 // service and no interface received, so let's disconnect/connect
1308 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001309 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 SystemClock.uptimeMillis()-mLastBindTime,1);
1311 mContext.unbindService(this);
1312 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
The Android Open Source Project4df24232009-03-05 14:34:35 -08001315 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
satok42c5a162011-05-26 16:46:14 +09001318 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 public boolean hideSoftInput(IInputMethodClient client, int flags,
1320 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001321 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 long ident = Binder.clearCallingIdentity();
1323 try {
1324 synchronized (mMethodMap) {
1325 if (mCurClient == null || client == null
1326 || mCurClient.client.asBinder() != client.asBinder()) {
1327 try {
1328 // We need to check if this is the current client with
1329 // focus in the window manager, to allow this call to
1330 // be made before input is started in it.
1331 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001332 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1333 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001334 mImeWindowVis = 0;
1335 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1336 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001340 mImeWindowVis = 0;
1341 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001342 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Joe Onorato8a9b2202010-02-26 18:56:32 -08001346 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001347 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
1349 } finally {
1350 Binder.restoreCallingIdentity(ident);
1351 }
1352 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001353
The Android Open Source Project4df24232009-03-05 14:34:35 -08001354 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1356 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001357 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001362 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001364 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001366 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001368 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1369 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1370 res = true;
1371 } else {
1372 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374 mInputShown = false;
1375 mShowRequested = false;
1376 mShowExplicitlyRequested = false;
1377 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001378 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
satok42c5a162011-05-26 16:46:14 +09001381 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001382 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1384 boolean first, int windowFlags) {
1385 long ident = Binder.clearCallingIdentity();
1386 try {
1387 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001388 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 + " viewHasFocus=" + viewHasFocus
1390 + " isTextEditor=" + isTextEditor
1391 + " softInputMode=#" + Integer.toHexString(softInputMode)
1392 + " first=" + first + " flags=#"
1393 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 if (mCurClient == null || client == null
1396 || mCurClient.client.asBinder() != client.asBinder()) {
1397 try {
1398 // We need to check if this is the current client with
1399 // focus in the window manager, to allow this call to
1400 // be made before input is started in it.
1401 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001402 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 return;
1404 }
1405 } catch (RemoteException e) {
1406 }
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001409 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001410 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001411 return;
1412 }
1413 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001415 // Should we auto-show the IME even if the caller has not
1416 // specified what should be done with it?
1417 // We only do this automatically if the window can resize
1418 // to accommodate the IME (so what the user sees will give
1419 // them good context without input information being obscured
1420 // by the IME) or if running on a large screen where there
1421 // is more room for the target window + IME.
1422 final boolean doAutoShow =
1423 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1424 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1425 || mRes.getConfiguration().isLayoutSizeAtLeast(
1426 Configuration.SCREENLAYOUT_SIZE_LARGE);
1427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1429 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001430 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1432 // There is no focus view, and this window will
1433 // be behind any soft input window, so hide the
1434 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001435 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001436 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001438 } else if (isTextEditor && doAutoShow && (softInputMode &
1439 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 // There is a focus view, and we are navigating forward
1441 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001442 // We only do this automatically if the window an resize
1443 // to accomodate the IME (so what the user sees will give
1444 // them good context without input information being obscured
1445 // by the IME) or if running on a large screen where there
1446 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001447 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001448 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450 break;
1451 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1452 // Do nothing.
1453 break;
1454 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1455 if ((softInputMode &
1456 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001457 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001458 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
1460 break;
1461 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001462 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001463 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 break;
1465 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1466 if ((softInputMode &
1467 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001468 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001469 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471 break;
1472 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001473 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001474 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 break;
1476 }
1477 }
1478 } finally {
1479 Binder.restoreCallingIdentity(ident);
1480 }
1481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001482
satok42c5a162011-05-26 16:46:14 +09001483 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1485 synchronized (mMethodMap) {
1486 if (mCurClient == null || client == null
1487 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001488 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001489 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491
satok440aab52010-11-25 09:43:11 +09001492 // Always call subtype picker, because subtype picker is a superset of input method
1493 // picker.
satokab751aa2010-09-14 19:17:36 +09001494 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1495 }
1496 }
1497
satok42c5a162011-05-26 16:46:14 +09001498 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001500 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1501 }
1502
satok42c5a162011-05-26 16:46:14 +09001503 @Override
satok28203512010-11-24 11:06:49 +09001504 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1505 synchronized (mMethodMap) {
1506 if (subtype != null) {
1507 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1508 mMethodMap.get(id), subtype.hashCode()));
1509 } else {
1510 setInputMethod(token, id);
1511 }
1512 }
satokab751aa2010-09-14 19:17:36 +09001513 }
1514
satok42c5a162011-05-26 16:46:14 +09001515 @Override
satokb416a712010-11-25 20:42:14 +09001516 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001517 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001518 synchronized (mMethodMap) {
1519 if (mCurClient == null || client == null
1520 || mCurClient.client.asBinder() != client.asBinder()) {
1521 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1522 }
satok7fee71f2010-12-17 18:54:26 +09001523 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1524 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001525 }
1526 }
1527
satok4fc87d62011-05-20 16:13:43 +09001528 @Override
satok735cf382010-11-11 20:40:09 +09001529 public boolean switchToLastInputMethod(IBinder token) {
1530 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001531 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001532 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001533 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001534 lastImi = mMethodMap.get(lastIme.first);
1535 } else {
1536 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001537 }
satok4fc87d62011-05-20 16:13:43 +09001538 String targetLastImiId = null;
1539 int subtypeId = NOT_A_SUBTYPE_ID;
1540 if (lastIme != null && lastImi != null) {
1541 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1542 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1543 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1544 : mCurrentSubtype.hashCode();
1545 // If the last IME is the same as the current IME and the last subtype is not
1546 // defined, there is no need to switch to the last IME.
1547 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1548 targetLastImiId = lastIme.first;
1549 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1550 }
1551 }
1552
1553 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1554 // This is a safety net. If the currentSubtype can't be added to the history
1555 // and the framework couldn't find the last ime, we will make the last ime be
1556 // the most applicable enabled keyboard subtype of the system imes.
1557 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1558 if (enabled != null) {
1559 final int N = enabled.size();
1560 final String locale = mCurrentSubtype == null
1561 ? mRes.getConfiguration().locale.toString()
1562 : mCurrentSubtype.getLocale();
1563 for (int i = 0; i < N; ++i) {
1564 final InputMethodInfo imi = enabled.get(i);
1565 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1566 InputMethodSubtype keyboardSubtype =
1567 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1568 SUBTYPE_MODE_KEYBOARD, locale, true);
1569 if (keyboardSubtype != null) {
1570 targetLastImiId = imi.getId();
1571 subtypeId = getSubtypeIdFromHashCode(
1572 imi, keyboardSubtype.hashCode());
1573 if(keyboardSubtype.getLocale().equals(locale)) {
1574 break;
1575 }
1576 }
1577 }
1578 }
1579 }
1580 }
1581
1582 if (!TextUtils.isEmpty(targetLastImiId)) {
1583 if (DEBUG) {
1584 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1585 + ", from: " + mCurMethodId + ", " + subtypeId);
1586 }
1587 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1588 return true;
1589 } else {
1590 return false;
1591 }
satok735cf382010-11-11 20:40:09 +09001592 }
1593 }
1594
satoke7c6998e2011-06-03 17:57:59 +09001595 @Override
satok68f1b782011-04-11 14:26:04 +09001596 public InputMethodSubtype getLastInputMethodSubtype() {
1597 synchronized (mMethodMap) {
1598 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1599 // TODO: Handle the case of the last IME with no subtypes
1600 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1601 || TextUtils.isEmpty(lastIme.second)) return null;
1602 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1603 if (lastImi == null) return null;
1604 try {
1605 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1606 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1607 lastImi, lastSubtypeHash));
1608 } catch (NumberFormatException e) {
1609 return null;
1610 }
1611 }
1612 }
1613
satoke7c6998e2011-06-03 17:57:59 +09001614 @Override
1615 public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) {
1616 if (token == null || mCurToken != token) {
1617 return false;
1618 }
1619 if (subtypes == null || subtypes.length == 0) return false;
1620 synchronized (mMethodMap) {
1621 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1622 if (imi == null) return false;
1623 final int N = subtypes.length;
1624 mFileManager.addInputMethodSubtypes(mCurMethodId, subtypes);
1625 buildInputMethodListLocked(mMethodList, mMethodMap);
1626 return true;
1627 }
1628 }
1629
satok28203512010-11-24 11:06:49 +09001630 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 synchronized (mMethodMap) {
1632 if (token == null) {
1633 if (mContext.checkCallingOrSelfPermission(
1634 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1635 != PackageManager.PERMISSION_GRANTED) {
1636 throw new SecurityException(
1637 "Using null token requires permission "
1638 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1639 }
1640 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001641 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1642 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 return;
1644 }
1645
1646 long ident = Binder.clearCallingIdentity();
1647 try {
satokab751aa2010-09-14 19:17:36 +09001648 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 } finally {
1650 Binder.restoreCallingIdentity(ident);
1651 }
1652 }
1653 }
1654
satok42c5a162011-05-26 16:46:14 +09001655 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 public void hideMySoftInput(IBinder token, int flags) {
1657 synchronized (mMethodMap) {
1658 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001659 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1660 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 return;
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 long ident = Binder.clearCallingIdentity();
1664 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001665 hideCurrentInputLocked(flags, null);
1666 } finally {
1667 Binder.restoreCallingIdentity(ident);
1668 }
1669 }
1670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
satok42c5a162011-05-26 16:46:14 +09001672 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001673 public void showMySoftInput(IBinder token, int flags) {
1674 synchronized (mMethodMap) {
1675 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001676 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1677 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001678 return;
1679 }
1680 long ident = Binder.clearCallingIdentity();
1681 try {
1682 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 } finally {
1684 Binder.restoreCallingIdentity(ident);
1685 }
1686 }
1687 }
1688
1689 void setEnabledSessionInMainThread(SessionState session) {
1690 if (mEnabledSession != session) {
1691 if (mEnabledSession != null) {
1692 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001693 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 mEnabledSession.method.setSessionEnabled(
1695 mEnabledSession.session, false);
1696 } catch (RemoteException e) {
1697 }
1698 }
1699 mEnabledSession = session;
1700 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001701 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 session.method.setSessionEnabled(
1703 session.session, true);
1704 } catch (RemoteException e) {
1705 }
1706 }
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
satok42c5a162011-05-26 16:46:14 +09001709 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 public boolean handleMessage(Message msg) {
1711 HandlerCaller.SomeArgs args;
1712 switch (msg.what) {
1713 case MSG_SHOW_IM_PICKER:
1714 showInputMethodMenu();
1715 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001716
satokab751aa2010-09-14 19:17:36 +09001717 case MSG_SHOW_IM_SUBTYPE_PICKER:
1718 showInputMethodSubtypeMenu();
1719 return true;
1720
satok47a44912010-10-06 16:03:58 +09001721 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001722 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001723 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001724 return true;
1725
1726 case MSG_SHOW_IM_CONFIG:
1727 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001728 return true;
1729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 case MSG_UNBIND_INPUT:
1733 try {
1734 ((IInputMethod)msg.obj).unbindInput();
1735 } catch (RemoteException e) {
1736 // There is nothing interesting about the method dying.
1737 }
1738 return true;
1739 case MSG_BIND_INPUT:
1740 args = (HandlerCaller.SomeArgs)msg.obj;
1741 try {
1742 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1743 } catch (RemoteException e) {
1744 }
1745 return true;
1746 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001747 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001749 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1750 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 } catch (RemoteException e) {
1752 }
1753 return true;
1754 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 ((IInputMethod)args.arg1).hideSoftInput(0,
1758 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 } catch (RemoteException e) {
1760 }
1761 return true;
1762 case MSG_ATTACH_TOKEN:
1763 args = (HandlerCaller.SomeArgs)msg.obj;
1764 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001765 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1767 } catch (RemoteException e) {
1768 }
1769 return true;
1770 case MSG_CREATE_SESSION:
1771 args = (HandlerCaller.SomeArgs)msg.obj;
1772 try {
1773 ((IInputMethod)args.arg1).createSession(
1774 (IInputMethodCallback)args.arg2);
1775 } catch (RemoteException e) {
1776 }
1777 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 case MSG_START_INPUT:
1781 args = (HandlerCaller.SomeArgs)msg.obj;
1782 try {
1783 SessionState session = (SessionState)args.arg1;
1784 setEnabledSessionInMainThread(session);
1785 session.method.startInput((IInputContext)args.arg2,
1786 (EditorInfo)args.arg3);
1787 } catch (RemoteException e) {
1788 }
1789 return true;
1790 case MSG_RESTART_INPUT:
1791 args = (HandlerCaller.SomeArgs)msg.obj;
1792 try {
1793 SessionState session = (SessionState)args.arg1;
1794 setEnabledSessionInMainThread(session);
1795 session.method.restartInput((IInputContext)args.arg2,
1796 (EditorInfo)args.arg3);
1797 } catch (RemoteException e) {
1798 }
1799 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 case MSG_UNBIND_METHOD:
1804 try {
1805 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1806 } catch (RemoteException e) {
1807 // There is nothing interesting about the last client dying.
1808 }
1809 return true;
1810 case MSG_BIND_METHOD:
1811 args = (HandlerCaller.SomeArgs)msg.obj;
1812 try {
1813 ((IInputMethodClient)args.arg1).onBindMethod(
1814 (InputBindResult)args.arg2);
1815 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001816 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 return true;
1819 }
1820 return false;
1821 }
1822
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001823 private boolean isSystemIme(InputMethodInfo inputMethod) {
1824 return (inputMethod.getServiceInfo().applicationInfo.flags
1825 & ApplicationInfo.FLAG_SYSTEM) != 0;
1826 }
1827
Ken Wakasa586f0512011-01-20 22:31:01 +09001828 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1829 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1830 final int subtypeCount = imi.getSubtypeCount();
1831 for (int i = 0; i < subtypeCount; ++i) {
1832 subtypes.add(imi.getSubtypeAt(i));
1833 }
1834 return subtypes;
1835 }
1836
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001837 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001838 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001839 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001840 // We'd prefer to fall back on a system IME, since that is safer.
1841 int i=enabled.size();
1842 while (i > 0) {
1843 i--;
1844 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1845 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1846 break;
1847 }
1848 }
satokab751aa2010-09-14 19:17:36 +09001849 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001850 if (DEBUG) {
1851 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1852 }
satok723a27e2010-11-11 14:58:11 +09001853 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001854 return true;
1855 }
1856
1857 return false;
1858 }
1859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1861 HashMap<String, InputMethodInfo> map) {
1862 list.clear();
1863 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001866 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001867 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1868 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1869 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1870 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871
1872 List<ResolveInfo> services = pm.queryIntentServices(
1873 new Intent(InputMethod.SERVICE_INTERFACE),
1874 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001875
satoke7c6998e2011-06-03 17:57:59 +09001876 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1877 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 for (int i = 0; i < services.size(); ++i) {
1879 ResolveInfo ri = services.get(i);
1880 ServiceInfo si = ri.serviceInfo;
1881 ComponentName compName = new ComponentName(si.packageName, si.name);
1882 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1883 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001884 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 + ": it does not require the permission "
1886 + android.Manifest.permission.BIND_INPUT_METHOD);
1887 continue;
1888 }
1889
Joe Onorato8a9b2202010-02-26 18:56:32 -08001890 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891
1892 try {
satoke7c6998e2011-06-03 17:57:59 +09001893 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001895 final String id = p.getId();
1896 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897
Amith Yamasanie861ec12010-03-24 21:39:27 -07001898 // System IMEs are enabled by default, unless there's a hard keyboard
1899 // and the system IME was explicitly disabled
1900 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1901 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001902 }
1903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001905 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001909 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001911 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001914
1915 String defaultIme = Settings.Secure.getString(mContext
1916 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001917 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001918 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001919 updateFromSettingsLocked();
1920 }
1921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001925
satokab751aa2010-09-14 19:17:36 +09001926 private void showInputMethodMenu() {
1927 showInputMethodMenuInternal(false);
1928 }
1929
1930 private void showInputMethodSubtypeMenu() {
1931 showInputMethodMenuInternal(true);
1932 }
1933
satok217f5482010-12-15 05:19:19 +09001934 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001935 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001936 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001937 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1938 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001939 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001940 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001941 }
satok217f5482010-12-15 05:19:19 +09001942 mContext.startActivity(intent);
1943 }
1944
1945 private void showConfigureInputMethods() {
1946 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1947 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1948 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1949 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001950 mContext.startActivity(intent);
1951 }
1952
satokab751aa2010-09-14 19:17:36 +09001953 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001954 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 String lastInputMethodId = Settings.Secure.getString(context
1961 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001962 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001963 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001965 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001966 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1967 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001968 if (immis == null || immis.size() == 0) {
1969 return;
1970 }
1971
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001972 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973
Ken Wakasa761eb372011-03-04 19:06:18 +09001974 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1975 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1976 new Comparator<InputMethodInfo>() {
1977 @Override
1978 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1979 if (imi2 == null) return 0;
1980 if (imi1 == null) return 1;
1981 if (pm == null) {
1982 return imi1.getId().compareTo(imi2.getId());
1983 }
1984 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
1985 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
1986 return imiId1.toString().compareTo(imiId2.toString());
1987 }
1988 });
satok913a8922010-08-26 21:53:41 +09001989
Ken Wakasa761eb372011-03-04 19:06:18 +09001990 sortedImmis.putAll(immis);
1991
1992 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
1993 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
1994
1995 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09001996 if (imi == null) continue;
1997 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09001998 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09001999 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2000 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002001 }
satokbb4aa062011-01-19 21:40:27 +09002002 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002003 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002004 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002005 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09002006 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09002007 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok9b415792011-05-30 12:37:52 +09002008 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))
2009 && !subtype.isAuxiliary()) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002010 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09002011 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09002012 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09002013 if (nameResId != 0) {
satok4f313532011-06-01 16:13:45 +09002014 title = TextUtils.concat(subtype.getDisplayName(context,
2015 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09002016 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09002017 } else {
2018 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09002019 // TODO: Use more friendly Title and UI
2020 title = label + "," + (mode == null ? "" : mode) + ","
2021 + (language == null ? "" : language);
2022 }
Ken Wakasa761eb372011-03-04 19:06:18 +09002023 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2024 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09002025 }
satokab751aa2010-09-14 19:17:36 +09002026 }
2027 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002028 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2029 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002030 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002031 }
satok913a8922010-08-26 21:53:41 +09002032
Ken Wakasa761eb372011-03-04 19:06:18 +09002033 final int N = imList.size();
2034 mItems = new CharSequence[N];
2035 for (int i = 0; i < N; ++i) {
2036 mItems[i] = imList.get(i).first;
2037 }
satokab751aa2010-09-14 19:17:36 +09002038 mIms = new InputMethodInfo[N];
2039 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002040 int checkedItem = 0;
2041 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002042 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002043 mIms[i] = value.first;
2044 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002045 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002046 int subtypeId = mSubtypeIds[i];
2047 if ((subtypeId == NOT_A_SUBTYPE_ID)
2048 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2049 || (subtypeId == lastInputMethodSubtypeId)) {
2050 checkedItem = i;
2051 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
satokab751aa2010-09-14 19:17:36 +09002054
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002055 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002056 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002057 public void onClick(DialogInterface dialog, int which) {
2058 hideInputMethodMenu();
2059 }
2060 };
satokd87c2592010-09-29 11:52:06 +09002061
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002062 TypedArray a = context.obtainStyledAttributes(null,
2063 com.android.internal.R.styleable.DialogPreference,
2064 com.android.internal.R.attr.alertDialogStyle, 0);
2065 mDialogBuilder = new AlertDialog.Builder(context)
2066 .setTitle(com.android.internal.R.string.select_input_method)
2067 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002068 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002069 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002071 }
2072 })
2073 .setIcon(a.getDrawable(
2074 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2075 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002076
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002077 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2078 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002079 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002080 public void onClick(DialogInterface dialog, int which) {
2081 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002082 if (mIms == null || mIms.length <= which
2083 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002084 return;
2085 }
2086 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002087 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002088 hideInputMethodMenu();
2089 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002090 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002091 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002092 subtypeId = NOT_A_SUBTYPE_ID;
2093 }
2094 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002095 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002098 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099
satok7f35c8c2010-10-07 21:13:11 +09002100 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09002101 mDialogBuilder.setPositiveButton(
2102 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002103 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002104 @Override
satok7f35c8c2010-10-07 21:13:11 +09002105 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002106 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002107 }
2108 });
2109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002111 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 mSwitchingDialog.getWindow().setType(
2113 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002114 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 mSwitchingDialog.show();
2116 }
2117 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002120 synchronized (mMethodMap) {
2121 hideInputMethodMenuLocked();
2122 }
2123 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002124
The Android Open Source Project10592532009-03-18 17:39:46 -07002125 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002126 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127
The Android Open Source Project10592532009-03-18 17:39:46 -07002128 if (mSwitchingDialog != null) {
2129 mSwitchingDialog.dismiss();
2130 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002132
The Android Open Source Project10592532009-03-18 17:39:46 -07002133 mDialogBuilder = null;
2134 mItems = null;
2135 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
satok42c5a162011-05-26 16:46:14 +09002140 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 public boolean setInputMethodEnabled(String id, boolean enabled) {
2142 synchronized (mMethodMap) {
2143 if (mContext.checkCallingOrSelfPermission(
2144 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2145 != PackageManager.PERMISSION_GRANTED) {
2146 throw new SecurityException(
2147 "Requires permission "
2148 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2149 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 long ident = Binder.clearCallingIdentity();
2152 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002153 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 } finally {
2155 Binder.restoreCallingIdentity(ident);
2156 }
2157 }
2158 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002159
2160 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2161 // Make sure this is a valid input method.
2162 InputMethodInfo imm = mMethodMap.get(id);
2163 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002164 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002165 }
2166
satokd87c2592010-09-29 11:52:06 +09002167 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2168 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002169
satokd87c2592010-09-29 11:52:06 +09002170 if (enabled) {
2171 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2172 if (pair.first.equals(id)) {
2173 // We are enabling this input method, but it is already enabled.
2174 // Nothing to do. The previous state was enabled.
2175 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002176 }
2177 }
satokd87c2592010-09-29 11:52:06 +09002178 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2179 // Previous state was disabled.
2180 return false;
2181 } else {
2182 StringBuilder builder = new StringBuilder();
2183 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2184 builder, enabledInputMethodsList, id)) {
2185 // Disabled input method is currently selected, switch to another one.
2186 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2187 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002188 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2189 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2190 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002191 }
2192 // Previous state was enabled.
2193 return true;
2194 } else {
2195 // We are disabling the input method but it is already disabled.
2196 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002197 return false;
2198 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002199 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002200 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002201
satok57ffc002011-01-25 00:11:47 +09002202 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2203 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002204 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002205 }
2206
satok723a27e2010-11-11 14:58:11 +09002207 private void saveCurrentInputMethodAndSubtypeToHistory() {
2208 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2209 if (mCurrentSubtype != null) {
2210 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2211 }
satok57ffc002011-01-25 00:11:47 +09002212 if (canAddToLastInputMethod(mCurrentSubtype)) {
2213 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2214 }
satokab751aa2010-09-14 19:17:36 +09002215 }
2216
satok723a27e2010-11-11 14:58:11 +09002217 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2218 boolean setSubtypeOnly) {
2219 // Update the history of InputMethod and Subtype
2220 saveCurrentInputMethodAndSubtypeToHistory();
2221
2222 // Set Subtype here
2223 if (imi == null || subtypeId < 0) {
2224 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002225 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002226 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002227 if (subtypeId < imi.getSubtypeCount()) {
2228 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2229 mSettings.putSelectedSubtype(subtype.hashCode());
2230 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002231 } else {
2232 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2233 mCurrentSubtype = null;
2234 }
satokab751aa2010-09-14 19:17:36 +09002235 }
satok723a27e2010-11-11 14:58:11 +09002236
2237 if (!setSubtypeOnly) {
2238 // Set InputMethod here
2239 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2240 }
2241 }
2242
2243 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2244 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2245 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2246 // newDefaultIme is empty when there is no candidate for the selected IME.
2247 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2248 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2249 if (subtypeHashCode != null) {
2250 try {
2251 lastSubtypeId = getSubtypeIdFromHashCode(
2252 imi, Integer.valueOf(subtypeHashCode));
2253 } catch (NumberFormatException e) {
2254 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2255 }
2256 }
2257 }
2258 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002259 }
2260
2261 private int getSelectedInputMethodSubtypeId(String id) {
2262 InputMethodInfo imi = mMethodMap.get(id);
2263 if (imi == null) {
2264 return NOT_A_SUBTYPE_ID;
2265 }
satokab751aa2010-09-14 19:17:36 +09002266 int subtypeId;
2267 try {
2268 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2269 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2270 } catch (SettingNotFoundException e) {
2271 return NOT_A_SUBTYPE_ID;
2272 }
satok723a27e2010-11-11 14:58:11 +09002273 return getSubtypeIdFromHashCode(imi, subtypeId);
2274 }
2275
2276 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002277 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002278 final int subtypeCount = imi.getSubtypeCount();
2279 for (int i = 0; i < subtypeCount; ++i) {
2280 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002281 if (subtypeHashCode == ims.hashCode()) {
2282 return i;
2283 }
satokab751aa2010-09-14 19:17:36 +09002284 }
2285 }
2286 return NOT_A_SUBTYPE_ID;
2287 }
2288
satokdf31ae62011-01-15 06:19:44 +09002289 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2290 Resources res, List<InputMethodSubtype> subtypes) {
2291 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002292 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2293 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2294 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002295 final int N = subtypes.size();
2296 boolean containsKeyboardSubtype = false;
2297 for (int i = 0; i < N; ++i) {
2298 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002299 final String locale = subtype.getLocale();
2300 final String mode = subtype.getMode();
2301 // When system locale starts with subtype's locale, that subtype will be applicable
2302 // for system locale
2303 // For instance, it's clearly applicable for cases like system locale = en_US and
2304 // subtype = en, but it is not necessarily considered applicable for cases like system
2305 // locale = en and subtype = en_US.
2306 // We just call systemLocale.startsWith(locale) in this function because there is no
2307 // need to find applicable subtypes aggressively unlike
2308 // findLastResortApplicableSubtypeLocked.
2309 if (systemLocale.startsWith(locale)) {
2310 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2311 // If more applicable subtypes are contained, skip.
2312 if (applicableSubtype != null
2313 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2314 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002315 if (!containsKeyboardSubtype
2316 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2317 containsKeyboardSubtype = true;
2318 }
2319 }
2320 }
satok3da92232011-01-11 22:46:30 +09002321 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2322 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002323 if (!containsKeyboardSubtype) {
2324 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002325 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002326 if (lastResortKeyboardSubtype != null) {
2327 applicableSubtypes.add(lastResortKeyboardSubtype);
2328 }
2329 }
2330 return applicableSubtypes;
2331 }
2332
satok4e4569d2010-11-19 18:45:53 +09002333 /**
2334 * If there are no selected subtypes, tries finding the most applicable one according to the
2335 * given locale.
2336 * @param subtypes this function will search the most applicable subtype in subtypes
2337 * @param mode subtypes will be filtered by mode
2338 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002339 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2340 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002341 * @return the most applicable subtypeId
2342 */
satokdf31ae62011-01-15 06:19:44 +09002343 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2344 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002345 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002346 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002347 return null;
satok8fbb1e82010-11-02 23:15:58 +09002348 }
satok4e4569d2010-11-19 18:45:53 +09002349 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002350 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002351 }
satok8fbb1e82010-11-02 23:15:58 +09002352 final String language = locale.substring(0, 2);
2353 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002354 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002355 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002356 final int N = subtypes.size();
2357 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002358 InputMethodSubtype subtype = subtypes.get(i);
2359 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002360 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2361 // and all subtypes with all modes can be candidates.
2362 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002363 if (firstMatchedModeSubtype == null) {
2364 firstMatchedModeSubtype = subtype;
2365 }
satok9ef02832010-11-04 21:17:48 +09002366 if (locale.equals(subtypeLocale)) {
2367 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002368 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002369 break;
2370 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2371 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002372 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002373 partialMatchFound = true;
2374 }
satok8fbb1e82010-11-02 23:15:58 +09002375 }
2376 }
2377
satok7599a7f2010-12-22 13:45:23 +09002378 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2379 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002380 }
2381
satok8fbb1e82010-11-02 23:15:58 +09002382 // The first subtype applicable to the system locale will be defined as the most applicable
2383 // subtype.
2384 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002385 if (applicableSubtype != null) {
2386 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2387 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2388 }
satok8fbb1e82010-11-02 23:15:58 +09002389 }
satokcd7cd292010-11-20 15:46:23 +09002390 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002391 }
2392
satok4e4569d2010-11-19 18:45:53 +09002393 // If there are no selected shortcuts, tries finding the most applicable ones.
2394 private Pair<InputMethodInfo, InputMethodSubtype>
2395 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2396 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2397 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002398 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002399 boolean foundInSystemIME = false;
2400
2401 // Search applicable subtype for each InputMethodInfo
2402 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002403 final String imiId = imi.getId();
2404 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2405 continue;
2406 }
satokcd7cd292010-11-20 15:46:23 +09002407 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002408 final List<InputMethodSubtype> enabledSubtypes =
2409 getEnabledInputMethodSubtypeList(imi, true);
2410 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002411 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002412 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002413 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002414 }
satokdf31ae62011-01-15 06:19:44 +09002415 // 2. Search by the system locale from enabledSubtypes.
2416 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002417 if (subtype == null) {
2418 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002419 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002420 }
satok7599a7f2010-12-22 13:45:23 +09002421 // 4. Search by the current subtype's locale from all subtypes.
2422 if (subtype == null && mCurrentSubtype != null) {
2423 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002424 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002425 }
2426 // 5. Search by the system locale from all subtypes.
2427 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002428 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002429 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002430 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002431 }
satokcd7cd292010-11-20 15:46:23 +09002432 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002433 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002434 // The current input method is the most applicable IME.
2435 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002436 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002437 break;
satok7599a7f2010-12-22 13:45:23 +09002438 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002439 // The system input method is 2nd applicable IME.
2440 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002441 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002442 if ((imi.getServiceInfo().applicationInfo.flags
2443 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2444 foundInSystemIME = true;
2445 }
satok4e4569d2010-11-19 18:45:53 +09002446 }
2447 }
2448 }
2449 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002450 if (mostApplicableIMI != null) {
2451 Slog.w(TAG, "Most applicable shortcut input method was:"
2452 + mostApplicableIMI.getId());
2453 if (mostApplicableSubtype != null) {
2454 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2455 + "," + mostApplicableSubtype.getMode() + ","
2456 + mostApplicableSubtype.getLocale());
2457 }
2458 }
satok4e4569d2010-11-19 18:45:53 +09002459 }
satokcd7cd292010-11-20 15:46:23 +09002460 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002461 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002462 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002463 } else {
2464 return null;
2465 }
2466 }
2467
satokab751aa2010-09-14 19:17:36 +09002468 /**
2469 * @return Return the current subtype of this input method.
2470 */
satok42c5a162011-05-26 16:46:14 +09002471 @Override
satokab751aa2010-09-14 19:17:36 +09002472 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002473 boolean subtypeIsSelected = false;
2474 try {
2475 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2476 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2477 } catch (SettingNotFoundException e) {
2478 }
satok3ef8b292010-11-23 06:06:29 +09002479 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002480 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002481 String lastInputMethodId = Settings.Secure.getString(
2482 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002483 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2484 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002485 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2486 if (imi != null) {
2487 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002488 // the most applicable subtype from explicitly or implicitly enabled
2489 // subtypes.
2490 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2491 getEnabledInputMethodSubtypeList(imi, true);
2492 // If there is only one explicitly or implicitly enabled subtype,
2493 // just returns it.
2494 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2495 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2496 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2497 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2498 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2499 SUBTYPE_MODE_KEYBOARD, null, true);
2500 if (mCurrentSubtype == null) {
2501 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2502 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2503 true);
2504 }
2505 }
satok4e4569d2010-11-19 18:45:53 +09002506 }
satokcd7cd292010-11-20 15:46:23 +09002507 } else {
satok3ef8b292010-11-23 06:06:29 +09002508 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002509 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002510 }
satok8fbb1e82010-11-02 23:15:58 +09002511 }
satok3ef8b292010-11-23 06:06:29 +09002512 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002513 }
satokab751aa2010-09-14 19:17:36 +09002514 }
2515
satokf3db1af2010-11-23 13:34:33 +09002516 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2517 InputMethodSubtype subtype) {
2518 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2519 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2520 } else {
2521 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2522 subtypes.add(subtype);
2523 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2524 }
2525 }
2526
satok4e4569d2010-11-19 18:45:53 +09002527 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002528 @Override
satok4e4569d2010-11-19 18:45:53 +09002529 public List getShortcutInputMethodsAndSubtypes() {
2530 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002531 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002532 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002533 // If there are no selected shortcut subtypes, the framework will try to find
2534 // the most applicable subtype from all subtypes whose mode is
2535 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002536 Pair<InputMethodInfo, InputMethodSubtype> info =
2537 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2538 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002539 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002540 ret.add(info.first);
2541 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002542 }
satok3da92232011-01-11 22:46:30 +09002543 return ret;
satokf3db1af2010-11-23 13:34:33 +09002544 }
satokf3db1af2010-11-23 13:34:33 +09002545 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2546 ret.add(imi);
2547 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2548 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002549 }
2550 }
satokf3db1af2010-11-23 13:34:33 +09002551 return ret;
satok4e4569d2010-11-19 18:45:53 +09002552 }
2553 }
2554
satok42c5a162011-05-26 16:46:14 +09002555 @Override
satokb66d2872010-11-10 01:04:04 +09002556 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2557 synchronized (mMethodMap) {
2558 if (subtype != null && mCurMethodId != null) {
2559 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2560 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2561 if (subtypeId != NOT_A_SUBTYPE_ID) {
2562 setInputMethodLocked(mCurMethodId, subtypeId);
2563 return true;
2564 }
2565 }
2566 return false;
2567 }
2568 }
2569
satokd87c2592010-09-29 11:52:06 +09002570 /**
2571 * Utility class for putting and getting settings for InputMethod
2572 * TODO: Move all putters and getters of settings to this class.
2573 */
2574 private static class InputMethodSettings {
2575 // The string for enabled input method is saved as follows:
2576 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2577 private static final char INPUT_METHOD_SEPARATER = ':';
2578 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002579 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002580 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2581
satok723a27e2010-11-11 14:58:11 +09002582 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002583 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2584
satokdf31ae62011-01-15 06:19:44 +09002585 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002586 private final ContentResolver mResolver;
2587 private final HashMap<String, InputMethodInfo> mMethodMap;
2588 private final ArrayList<InputMethodInfo> mMethodList;
2589
2590 private String mEnabledInputMethodsStrCache;
2591
2592 private static void buildEnabledInputMethodsSettingString(
2593 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2594 String id = pair.first;
2595 ArrayList<String> subtypes = pair.second;
2596 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002597 // Inputmethod and subtypes are saved in the settings as follows:
2598 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2599 for (String subtypeId: subtypes) {
2600 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002601 }
2602 }
2603
2604 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002605 Resources res, ContentResolver resolver,
2606 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2607 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002608 mResolver = resolver;
2609 mMethodMap = methodMap;
2610 mMethodList = methodList;
2611 }
2612
2613 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2614 return createEnabledInputMethodListLocked(
2615 getEnabledInputMethodsAndSubtypeListLocked());
2616 }
2617
satok7f35c8c2010-10-07 21:13:11 +09002618 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002619 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2620 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002621 getEnabledInputMethodsAndSubtypeListLocked());
2622 }
2623
satok67ddf9c2010-11-17 09:45:54 +09002624 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2625 InputMethodInfo imi) {
2626 List<Pair<String, ArrayList<String>>> imsList =
2627 getEnabledInputMethodsAndSubtypeListLocked();
2628 ArrayList<InputMethodSubtype> enabledSubtypes =
2629 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002630 if (imi != null) {
2631 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2632 InputMethodInfo info = mMethodMap.get(imsPair.first);
2633 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002634 final int subtypeCount = info.getSubtypeCount();
2635 for (int i = 0; i < subtypeCount; ++i) {
2636 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002637 for (String s: imsPair.second) {
2638 if (String.valueOf(ims.hashCode()).equals(s)) {
2639 enabledSubtypes.add(ims);
2640 }
satok67ddf9c2010-11-17 09:45:54 +09002641 }
2642 }
satok884ef9a2010-11-18 10:39:46 +09002643 break;
satok67ddf9c2010-11-17 09:45:54 +09002644 }
satok67ddf9c2010-11-17 09:45:54 +09002645 }
2646 }
2647 return enabledSubtypes;
2648 }
2649
satokd87c2592010-09-29 11:52:06 +09002650 // At the initial boot, the settings for input methods are not set,
2651 // so we need to enable IME in that case.
2652 public void enableAllIMEsIfThereIsNoEnabledIME() {
2653 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2654 StringBuilder sb = new StringBuilder();
2655 final int N = mMethodList.size();
2656 for (int i = 0; i < N; i++) {
2657 InputMethodInfo imi = mMethodList.get(i);
2658 Slog.i(TAG, "Adding: " + imi.getId());
2659 if (i > 0) sb.append(':');
2660 sb.append(imi.getId());
2661 }
2662 putEnabledInputMethodsStr(sb.toString());
2663 }
2664 }
2665
satokbb4aa062011-01-19 21:40:27 +09002666 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002667 ArrayList<Pair<String, ArrayList<String>>> imsList
2668 = new ArrayList<Pair<String, ArrayList<String>>>();
2669 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2670 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2671 return imsList;
2672 }
satok723a27e2010-11-11 14:58:11 +09002673 mInputMethodSplitter.setString(enabledInputMethodsStr);
2674 while (mInputMethodSplitter.hasNext()) {
2675 String nextImsStr = mInputMethodSplitter.next();
2676 mSubtypeSplitter.setString(nextImsStr);
2677 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002678 ArrayList<String> subtypeHashes = new ArrayList<String>();
2679 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002680 String imeId = mSubtypeSplitter.next();
2681 while (mSubtypeSplitter.hasNext()) {
2682 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002683 }
2684 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2685 }
2686 }
2687 return imsList;
2688 }
2689
2690 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2691 if (reloadInputMethodStr) {
2692 getEnabledInputMethodsStr();
2693 }
2694 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2695 // Add in the newly enabled input method.
2696 putEnabledInputMethodsStr(id);
2697 } else {
2698 putEnabledInputMethodsStr(
2699 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2700 }
2701 }
2702
2703 /**
2704 * Build and put a string of EnabledInputMethods with removing specified Id.
2705 * @return the specified id was removed or not.
2706 */
2707 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2708 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2709 boolean isRemoved = false;
2710 boolean needsAppendSeparator = false;
2711 for (Pair<String, ArrayList<String>> ims: imsList) {
2712 String curId = ims.first;
2713 if (curId.equals(id)) {
2714 // We are disabling this input method, and it is
2715 // currently enabled. Skip it to remove from the
2716 // new list.
2717 isRemoved = true;
2718 } else {
2719 if (needsAppendSeparator) {
2720 builder.append(INPUT_METHOD_SEPARATER);
2721 } else {
2722 needsAppendSeparator = true;
2723 }
2724 buildEnabledInputMethodsSettingString(builder, ims);
2725 }
2726 }
2727 if (isRemoved) {
2728 // Update the setting with the new list of input methods.
2729 putEnabledInputMethodsStr(builder.toString());
2730 }
2731 return isRemoved;
2732 }
2733
2734 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2735 List<Pair<String, ArrayList<String>>> imsList) {
2736 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2737 for (Pair<String, ArrayList<String>> ims: imsList) {
2738 InputMethodInfo info = mMethodMap.get(ims.first);
2739 if (info != null) {
2740 res.add(info);
2741 }
2742 }
2743 return res;
2744 }
2745
satok7f35c8c2010-10-07 21:13:11 +09002746 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002747 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002748 List<Pair<String, ArrayList<String>>> imsList) {
2749 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2750 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2751 for (Pair<String, ArrayList<String>> ims : imsList) {
2752 InputMethodInfo info = mMethodMap.get(ims.first);
2753 if (info != null) {
2754 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2755 }
2756 }
2757 return res;
2758 }
2759
satokd87c2592010-09-29 11:52:06 +09002760 private void putEnabledInputMethodsStr(String str) {
2761 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2762 mEnabledInputMethodsStrCache = str;
2763 }
2764
2765 private String getEnabledInputMethodsStr() {
2766 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2767 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002768 if (DEBUG) {
2769 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2770 }
satokd87c2592010-09-29 11:52:06 +09002771 return mEnabledInputMethodsStrCache;
2772 }
satok723a27e2010-11-11 14:58:11 +09002773
2774 private void saveSubtypeHistory(
2775 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2776 StringBuilder builder = new StringBuilder();
2777 boolean isImeAdded = false;
2778 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2779 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2780 newSubtypeId);
2781 isImeAdded = true;
2782 }
2783 for (Pair<String, String> ime: savedImes) {
2784 String imeId = ime.first;
2785 String subtypeId = ime.second;
2786 if (TextUtils.isEmpty(subtypeId)) {
2787 subtypeId = NOT_A_SUBTYPE_ID_STR;
2788 }
2789 if (isImeAdded) {
2790 builder.append(INPUT_METHOD_SEPARATER);
2791 } else {
2792 isImeAdded = true;
2793 }
2794 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2795 subtypeId);
2796 }
2797 // Remove the last INPUT_METHOD_SEPARATER
2798 putSubtypeHistoryStr(builder.toString());
2799 }
2800
2801 public void addSubtypeToHistory(String imeId, String subtypeId) {
2802 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2803 for (Pair<String, String> ime: subtypeHistory) {
2804 if (ime.first.equals(imeId)) {
2805 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002806 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002807 + ime.second);
2808 }
2809 // We should break here
2810 subtypeHistory.remove(ime);
2811 break;
2812 }
2813 }
satokbb4aa062011-01-19 21:40:27 +09002814 if (DEBUG) {
2815 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2816 }
satok723a27e2010-11-11 14:58:11 +09002817 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2818 }
2819
2820 private void putSubtypeHistoryStr(String str) {
2821 if (DEBUG) {
2822 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2823 }
2824 Settings.Secure.putString(
2825 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2826 }
2827
2828 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2829 // Gets the first one from the history
2830 return getLastSubtypeForInputMethodLockedInternal(null);
2831 }
2832
2833 public String getLastSubtypeForInputMethodLocked(String imeId) {
2834 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2835 if (ime != null) {
2836 return ime.second;
2837 } else {
2838 return null;
2839 }
2840 }
2841
2842 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2843 List<Pair<String, ArrayList<String>>> enabledImes =
2844 getEnabledInputMethodsAndSubtypeListLocked();
2845 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002846 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002847 final String imeInTheHistory = imeAndSubtype.first;
2848 // If imeId is empty, returns the first IME and subtype in the history
2849 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2850 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002851 final String subtypeHashCode =
2852 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2853 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002854 if (!TextUtils.isEmpty(subtypeHashCode)) {
2855 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002856 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002857 }
2858 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2859 }
2860 }
2861 }
2862 if (DEBUG) {
2863 Slog.d(TAG, "No enabled IME found in the history");
2864 }
2865 return null;
2866 }
2867
satokdf31ae62011-01-15 06:19:44 +09002868 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002869 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2870 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2871 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002872 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2873 if (explicitlyEnabledSubtypes.size() == 0) {
2874 // If there are no explicitly enabled subtypes, applicable subtypes are
2875 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002876 InputMethodInfo ime = mMethodMap.get(imeId);
2877 // If IME is enabled and no subtypes are enabled, applicable subtypes
2878 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002879 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002880 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002881 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002882 if (implicitlySelectedSubtypes != null) {
2883 final int N = implicitlySelectedSubtypes.size();
2884 for (int i = 0; i < N; ++i) {
2885 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2886 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2887 return subtypeHashCode;
2888 }
2889 }
2890 }
2891 }
2892 } else {
satokf6cafb62011-01-17 16:29:02 +09002893 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002894 if (s.equals(subtypeHashCode)) {
2895 // If both imeId and subtypeId are enabled, return subtypeId.
2896 return s;
2897 }
satok723a27e2010-11-11 14:58:11 +09002898 }
2899 }
2900 // If imeId was enabled but subtypeId was disabled.
2901 return NOT_A_SUBTYPE_ID_STR;
2902 }
2903 }
2904 // If both imeId and subtypeId are disabled, return null
2905 return null;
2906 }
2907
2908 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2909 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2910 final String subtypeHistoryStr = getSubtypeHistoryStr();
2911 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2912 return imsList;
2913 }
2914 mInputMethodSplitter.setString(subtypeHistoryStr);
2915 while (mInputMethodSplitter.hasNext()) {
2916 String nextImsStr = mInputMethodSplitter.next();
2917 mSubtypeSplitter.setString(nextImsStr);
2918 if (mSubtypeSplitter.hasNext()) {
2919 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2920 // The first element is ime id.
2921 String imeId = mSubtypeSplitter.next();
2922 while (mSubtypeSplitter.hasNext()) {
2923 subtypeId = mSubtypeSplitter.next();
2924 break;
2925 }
2926 imsList.add(new Pair<String, String>(imeId, subtypeId));
2927 }
2928 }
2929 return imsList;
2930 }
2931
2932 private String getSubtypeHistoryStr() {
2933 if (DEBUG) {
2934 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2935 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2936 }
2937 return Settings.Secure.getString(
2938 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2939 }
2940
2941 public void putSelectedInputMethod(String imeId) {
2942 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2943 }
2944
2945 public void putSelectedSubtype(int subtypeId) {
2946 Settings.Secure.putInt(
2947 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2948 }
satokd87c2592010-09-29 11:52:06 +09002949 }
2950
satoke7c6998e2011-06-03 17:57:59 +09002951 private static class InputMethodFileManager {
2952 private static final String SYSTEM_PATH = "system";
2953 private static final String INPUT_METHOD_PATH = "inputmethod";
2954 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2955 private static final String NODE_SUBTYPES = "subtypes";
2956 private static final String NODE_SUBTYPE = "subtype";
2957 private static final String NODE_IMI = "imi";
2958 private static final String ATTR_ID = "id";
2959 private static final String ATTR_LABEL = "label";
2960 private static final String ATTR_ICON = "icon";
2961 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2962 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2963 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2964 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2965 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2966 private final HashMap<String, InputMethodInfo> mMethodMap;
2967 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2968 new HashMap<String, List<InputMethodSubtype>>();
2969 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2970 if (methodMap == null) {
2971 throw new NullPointerException("methodMap is null");
2972 }
2973 mMethodMap = methodMap;
2974 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
2975 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
2976 if (!inputMethodDir.mkdirs()) {
2977 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
2978 }
2979 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
2980 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
2981 if (!subtypeFile.exists()) {
2982 // If "subtypes.xml" doesn't exist, create a blank file.
2983 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
2984 methodMap);
2985 } else {
2986 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
2987 }
2988 }
2989
2990 private void deleteAllInputMethodSubtypes(String imiId) {
2991 synchronized (mMethodMap) {
2992 mSubtypesMap.remove(imiId);
2993 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
2994 mMethodMap);
2995 }
2996 }
2997
2998 public void addInputMethodSubtypes(
2999 String imiId, InputMethodSubtype[] additionalSubtypes) {
3000 synchronized (mMethodMap) {
3001 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3002 final int N = additionalSubtypes.length;
3003 for (int i = 0; i < N; ++i) {
3004 final InputMethodSubtype subtype = additionalSubtypes[i];
3005 if (!subtypes.contains(subtype)) {
3006 subtypes.add(subtype);
3007 }
3008 }
3009 mSubtypesMap.put(imiId, subtypes);
3010 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3011 mMethodMap);
3012 }
3013 }
3014
3015 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3016 synchronized (mMethodMap) {
3017 return mSubtypesMap;
3018 }
3019 }
3020
3021 private static void writeAdditionalInputMethodSubtypes(
3022 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3023 HashMap<String, InputMethodInfo> methodMap) {
3024 // Safety net for the case that this function is called before methodMap is set.
3025 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3026 FileOutputStream fos = null;
3027 try {
3028 fos = subtypesFile.startWrite();
3029 final XmlSerializer out = new FastXmlSerializer();
3030 out.setOutput(fos, "utf-8");
3031 out.startDocument(null, true);
3032 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3033 out.startTag(null, NODE_SUBTYPES);
3034 for (String imiId : allSubtypes.keySet()) {
3035 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3036 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3037 continue;
3038 }
3039 out.startTag(null, NODE_IMI);
3040 out.attribute(null, ATTR_ID, imiId);
3041 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3042 final int N = subtypesList.size();
3043 for (int i = 0; i < N; ++i) {
3044 final InputMethodSubtype subtype = subtypesList.get(i);
3045 out.startTag(null, NODE_SUBTYPE);
3046 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3047 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3048 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3049 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3050 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3051 out.attribute(null, ATTR_IS_AUXILIARY,
3052 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3053 out.endTag(null, NODE_SUBTYPE);
3054 }
3055 out.endTag(null, NODE_IMI);
3056 }
3057 out.endTag(null, NODE_SUBTYPES);
3058 out.endDocument();
3059 subtypesFile.finishWrite(fos);
3060 } catch (java.io.IOException e) {
3061 Slog.w(TAG, "Error writing subtypes", e);
3062 if (fos != null) {
3063 subtypesFile.failWrite(fos);
3064 }
3065 }
3066 }
3067
3068 private static void readAdditionalInputMethodSubtypes(
3069 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3070 if (allSubtypes == null || subtypesFile == null) return;
3071 allSubtypes.clear();
3072 FileInputStream fis = null;
3073 try {
3074 fis = subtypesFile.openRead();
3075 final XmlPullParser parser = Xml.newPullParser();
3076 parser.setInput(fis, null);
3077 int type = parser.getEventType();
3078 // Skip parsing until START_TAG
3079 while ((type = parser.next()) != XmlPullParser.START_TAG
3080 && type != XmlPullParser.END_DOCUMENT) {}
3081 String firstNodeName = parser.getName();
3082 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3083 throw new XmlPullParserException("Xml doesn't start with subtypes");
3084 }
3085 final int depth =parser.getDepth();
3086 String currentImiId = null;
3087 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3088 while (((type = parser.next()) != XmlPullParser.END_TAG
3089 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3090 if (type != XmlPullParser.START_TAG)
3091 continue;
3092 final String nodeName = parser.getName();
3093 if (NODE_IMI.equals(nodeName)) {
3094 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3095 if (TextUtils.isEmpty(currentImiId)) {
3096 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3097 continue;
3098 }
3099 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3100 allSubtypes.put(currentImiId, tempSubtypesArray);
3101 } else if (NODE_SUBTYPE.equals(nodeName)) {
3102 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3103 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3104 continue;
3105 }
3106 final int icon = Integer.valueOf(
3107 parser.getAttributeValue(null, ATTR_ICON));
3108 final int label = Integer.valueOf(
3109 parser.getAttributeValue(null, ATTR_LABEL));
3110 final String imeSubtypeLocale =
3111 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3112 final String imeSubtypeMode =
3113 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3114 final String imeSubtypeExtraValue =
3115 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
3116 final boolean isAuxiliary =
3117 Boolean.valueOf(parser.getAttributeValue(null, ATTR_IS_AUXILIARY));
3118 final InputMethodSubtype subtype =
3119 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3120 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3121 tempSubtypesArray.add(subtype);
3122 }
3123 }
3124 } catch (XmlPullParserException e) {
3125 Slog.w(TAG, "Error reading subtypes: " + e);
3126 return;
3127 } catch (java.io.IOException e) {
3128 Slog.w(TAG, "Error reading subtypes: " + e);
3129 return;
3130 } catch (NumberFormatException e) {
3131 Slog.w(TAG, "Error reading subtypes: " + e);
3132 return;
3133 } finally {
3134 if (fis != null) {
3135 try {
3136 fis.close();
3137 } catch (java.io.IOException e1) {
3138 Slog.w(TAG, "Failed to close.");
3139 }
3140 }
3141 }
3142 }
3143 }
3144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 @Override
3148 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3149 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3150 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3153 + Binder.getCallingPid()
3154 + ", uid=" + Binder.getCallingUid());
3155 return;
3156 }
3157
3158 IInputMethod method;
3159 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 synchronized (mMethodMap) {
3164 p.println("Current Input Method Manager state:");
3165 int N = mMethodList.size();
3166 p.println(" Input Methods:");
3167 for (int i=0; i<N; i++) {
3168 InputMethodInfo info = mMethodList.get(i);
3169 p.println(" InputMethod #" + i + ":");
3170 info.dump(p, " ");
3171 }
3172 p.println(" Clients:");
3173 for (ClientState ci : mClients.values()) {
3174 p.println(" Client " + ci + ":");
3175 p.println(" client=" + ci.client);
3176 p.println(" inputContext=" + ci.inputContext);
3177 p.println(" sessionRequested=" + ci.sessionRequested);
3178 p.println(" curSession=" + ci.curSession);
3179 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003180 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003182 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3183 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3185 + " mBoundToMethod=" + mBoundToMethod);
3186 p.println(" mCurToken=" + mCurToken);
3187 p.println(" mCurIntent=" + mCurIntent);
3188 method = mCurMethod;
3189 p.println(" mCurMethod=" + mCurMethod);
3190 p.println(" mEnabledSession=" + mEnabledSession);
3191 p.println(" mShowRequested=" + mShowRequested
3192 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3193 + " mShowForced=" + mShowForced
3194 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003195 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003197
Jeff Brownb88102f2010-09-08 11:49:43 -07003198 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 pw.flush();
3201 try {
3202 client.client.asBinder().dump(fd, args);
3203 } catch (RemoteException e) {
3204 p.println("Input method client dead: " + e);
3205 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003206 } else {
3207 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003209
Jeff Brownb88102f2010-09-08 11:49:43 -07003210 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 pw.flush();
3213 try {
3214 method.asBinder().dump(fd, args);
3215 } catch (RemoteException e) {
3216 p.println("Input method service dead: " + e);
3217 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003218 } else {
3219 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 }
3221 }
3222}