blob: b03bd4d28df055830cf1be760e1b657b1dee3ba1 [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";
satokb6359412011-06-28 17:47:41 +0900140 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800143 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900145 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700147 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final IWindowManager mIWindowManager;
149 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900150 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 // All known input methods. mMethodMap also serves as the global
155 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900156 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
157 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900158 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
159 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
satok7cfc0ed2011-06-20 21:29:36 +0900161 // Ongoing notification
162 private final NotificationManager mNotificationManager;
163 private final Notification mImeSwitcherNotification;
164 private final PendingIntent mImeSwitchPendingIntent;
165 private final boolean mShowOngoingImeSwitcherForPhones;
166 private boolean mNotificationShown;
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 class SessionState {
169 final ClientState client;
170 final IInputMethod method;
171 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 @Override
174 public String toString() {
175 return "SessionState{uid " + client.uid + " pid " + client.pid
176 + " method " + Integer.toHexString(
177 System.identityHashCode(method))
178 + " session " + Integer.toHexString(
179 System.identityHashCode(session))
180 + "}";
181 }
182
183 SessionState(ClientState _client, IInputMethod _method,
184 IInputMethodSession _session) {
185 client = _client;
186 method = _method;
187 session = _session;
188 }
189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 class ClientState {
192 final IInputMethodClient client;
193 final IInputContext inputContext;
194 final int uid;
195 final int pid;
196 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 boolean sessionRequested;
199 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 @Override
202 public String toString() {
203 return "ClientState{" + Integer.toHexString(
204 System.identityHashCode(this)) + " uid " + uid
205 + " pid " + pid + "}";
206 }
207
208 ClientState(IInputMethodClient _client, IInputContext _inputContext,
209 int _uid, int _pid) {
210 client = _client;
211 inputContext = _inputContext;
212 uid = _uid;
213 pid = _pid;
214 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
215 }
216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 final HashMap<IBinder, ClientState> mClients
219 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700222 * Set once the system is ready to run third party code.
223 */
224 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * Id of the currently selected input method.
228 */
229 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * The current binding sequence number, incremented every time there is
233 * a new bind performed.
234 */
235 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
238 * The client that is currently bound to an input method.
239 */
240 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700243 * The last window token that gained focus.
244 */
245 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * The input context last provided by the current client.
249 */
250 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * The attributes last provided by the current client.
254 */
255 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The input method ID of the input method service that we are currently
259 * connected to or in the process of connecting to.
260 */
261 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
satokab751aa2010-09-14 19:17:36 +0900264 * The current subtype of the current input method.
265 */
266 private InputMethodSubtype mCurrentSubtype;
267
satok4e4569d2010-11-19 18:45:53 +0900268 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900269 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
270 mShortcutInputMethodsAndSubtypes =
271 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900272
273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * Set to true if our ServiceConnection is currently actively bound to
275 * a service (whether or not we have gotten its IBinder back yet).
276 */
277 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Set if the client has asked for the input method to be shown.
281 */
282 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Set if we were explicitly told to show the input method.
286 */
287 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Set if we were forced to be shown.
291 */
292 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if we last told the input method to show itself.
296 */
297 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * The Intent used to connect to the current input method.
301 */
302 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * The token we have made for the currently active input method, to
306 * identify it in the future.
307 */
308 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * If non-null, this is the input method service we are currently connected
312 * to.
313 */
314 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * Time that we last initiated a bind to the input method, to determine
318 * if we should try to disconnect and reconnect to it.
319 */
320 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Have we called mCurMethod.bindInput()?
324 */
325 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Currently enabled session. Only touched by service thread, not
329 * protected by a lock.
330 */
331 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * True if the screen is on. The value is true initially.
335 */
336 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
Joe Onorato857fd9b2011-01-27 15:08:35 -0800338 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
339 int mImeWindowVis;
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 AlertDialog.Builder mDialogBuilder;
342 AlertDialog mSwitchingDialog;
343 InputMethodInfo[] mIms;
344 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900345 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 class SettingsObserver extends ContentObserver {
348 SettingsObserver(Handler handler) {
349 super(handler);
350 ContentResolver resolver = mContext.getContentResolver();
351 resolver.registerContentObserver(Settings.Secure.getUriFor(
352 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900353 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900354 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900356 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 @Override public void onChange(boolean selfChange) {
360 synchronized (mMethodMap) {
361 updateFromSettingsLocked();
362 }
363 }
364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
367 @Override
368 public void onReceive(Context context, Intent intent) {
369 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
370 mScreenOn = true;
371 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
372 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700373 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
374 hideInputMethodMenu();
375 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800377 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 // Inform the current client of the change in active status
381 try {
382 if (mCurClient != null && mCurClient.client != null) {
383 mCurClient.client.setActive(mScreenOn);
384 }
385 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800386 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 + mCurClient.pid + " uid " + mCurClient.uid);
388 }
389 }
390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800392 class MyPackageMonitor extends PackageMonitor {
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800395 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800397 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
399 final int N = mMethodList.size();
400 if (curInputMethodId != null) {
401 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800402 InputMethodInfo imi = mMethodList.get(i);
403 if (imi.getId().equals(curInputMethodId)) {
404 for (String pkg : packages) {
405 if (imi.getPackageName().equals(pkg)) {
406 if (!doit) {
407 return true;
408 }
satok723a27e2010-11-11 14:58:11 +0900409 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 chooseNewDefaultIMELocked();
411 return true;
412 }
413 }
414 }
415 }
416 }
417 }
418 return false;
419 }
420
421 @Override
422 public void onSomePackagesChanged() {
423 synchronized (mMethodMap) {
424 InputMethodInfo curIm = null;
425 String curInputMethodId = Settings.Secure.getString(mContext
426 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
427 final int N = mMethodList.size();
428 if (curInputMethodId != null) {
429 for (int i=0; i<N; i++) {
430 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900431 final String imiId = imi.getId();
432 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 curIm = imi;
434 }
satoke7c6998e2011-06-03 17:57:59 +0900435
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900437 if (isPackageModified(imi.getPackageName())) {
438 mFileManager.deleteAllInputMethodSubtypes(imiId);
439 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 if (change == PACKAGE_TEMPORARY_CHANGE
441 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800442 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 + imi.getComponent());
444 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446 }
447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800449 buildInputMethodListLocked(mMethodList, mMethodMap);
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800453 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 int change = isPackageDisappearing(curIm.getPackageName());
455 if (change == PACKAGE_TEMPORARY_CHANGE
456 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 ServiceInfo si = null;
458 try {
459 si = mContext.getPackageManager().getServiceInfo(
460 curIm.getComponent(), 0);
461 } catch (PackageManager.NameNotFoundException ex) {
462 }
463 if (si == null) {
464 // Uh oh, current input method is no longer around!
465 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800466 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800467 mImeWindowVis = 0;
468 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
469 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800471 changed = true;
472 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900474 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 }
476 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800477 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 }
satokab751aa2010-09-14 19:17:36 +0900479
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 if (curIm == null) {
481 // We currently don't have a default input method... is
482 // one now available?
483 changed = chooseNewDefaultIMELocked();
484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 if (changed) {
487 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490 }
491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 class MethodCallback extends IInputMethodCallback.Stub {
494 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 MethodCallback(IInputMethod method) {
497 mMethod = method;
498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
satoke7c6998e2011-06-03 17:57:59 +0900500 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 public void finishedEvent(int seq, boolean handled) throws RemoteException {
502 }
503
satoke7c6998e2011-06-03 17:57:59 +0900504 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public void sessionCreated(IInputMethodSession session) throws RemoteException {
506 onSessionCreated(mMethod, session);
507 }
508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Joe Onorato089de882010-04-12 08:18:45 -0700510 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800512 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 mHandler = new Handler(this);
514 mIWindowManager = IWindowManager.Stub.asInterface(
515 ServiceManager.getService(Context.WINDOW_SERVICE));
516 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900517 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public void executeMessage(Message msg) {
519 handleMessage(msg);
520 }
521 });
satok7cfc0ed2011-06-20 21:29:36 +0900522
523 mNotificationManager = (NotificationManager)
524 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
525 mImeSwitcherNotification = new Notification();
526 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
527 mImeSwitcherNotification.when = 0;
528 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
529 mImeSwitcherNotification.tickerText = null;
530 mImeSwitcherNotification.defaults = 0; // please be quiet
531 mImeSwitcherNotification.sound = null;
532 mImeSwitcherNotification.vibrate = null;
533 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
534 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
535 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
536 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
537 mImeSwitchPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
538 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
539 com.android.internal.R.bool.show_ongoing_ime_switcher);
540
satoke7c6998e2011-06-03 17:57:59 +0900541 synchronized (mMethodMap) {
542 mFileManager = new InputMethodFileManager(mMethodMap);
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 IntentFilter screenOnOffFilt = new IntentFilter();
548 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
549 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700550 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800552
satok913a8922010-08-26 21:53:41 +0900553 mStatusBar = statusBar;
554 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900555 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900556
satokd87c2592010-09-29 11:52:06 +0900557 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900558 mSettings = new InputMethodSettings(
559 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900561 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
satokd87c2592010-09-29 11:52:06 +0900563 if (TextUtils.isEmpty(Settings.Secure.getString(
564 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900566 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
568 try {
satokd87c2592010-09-29 11:52:06 +0900569 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 imi.getPackageName(), 0).getResources();
571 if (res.getBoolean(imi.getIsDefaultResourceId())) {
572 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800573 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575 } catch (PackageManager.NameNotFoundException ex) {
576 } catch (Resources.NotFoundException ex) {
577 }
578 }
579 }
satokd87c2592010-09-29 11:52:06 +0900580 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800582 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900585 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 mSettingsObserver = new SettingsObserver(mHandler);
590 updateFromSettingsLocked();
591 }
592
593 @Override
594 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
595 throws RemoteException {
596 try {
597 return super.onTransact(code, data, reply, flags);
598 } catch (RuntimeException e) {
599 // The input method manager only throws security exceptions, so let's
600 // log all others.
601 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800602 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604 throw e;
605 }
606 }
607
608 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700609 synchronized (mMethodMap) {
610 if (!mSystemReady) {
611 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700612 try {
613 startInputInnerLocked();
614 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800615 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700616 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700617 }
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
satoke7c6998e2011-06-03 17:57:59 +0900621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public List<InputMethodInfo> getInputMethodList() {
623 synchronized (mMethodMap) {
624 return new ArrayList<InputMethodInfo>(mMethodList);
625 }
626 }
627
satoke7c6998e2011-06-03 17:57:59 +0900628 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public List<InputMethodInfo> getEnabledInputMethodList() {
630 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900631 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 }
634
satokbb4aa062011-01-19 21:40:27 +0900635 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
636 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
637 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
638 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
639 for (InputMethodInfo imi: getEnabledInputMethodList()) {
640 enabledInputMethodAndSubtypes.put(
641 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
642 }
643 return enabledInputMethodAndSubtypes;
644 }
645
646 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
647 boolean allowsImplicitlySelectedSubtypes) {
648 if (imi == null && mCurMethodId != null) {
649 imi = mMethodMap.get(mCurMethodId);
650 }
satok7265d9b2011-02-14 15:47:30 +0900651 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900652 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900653 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
654 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900655 }
satok7265d9b2011-02-14 15:47:30 +0900656 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900657 }
658
satoke7c6998e2011-06-03 17:57:59 +0900659 @Override
satok16331c82010-12-20 23:48:46 +0900660 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
661 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900662 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900663 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900664 }
665 }
666
satoke7c6998e2011-06-03 17:57:59 +0900667 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 public void addClient(IInputMethodClient client,
669 IInputContext inputContext, int uid, int pid) {
670 synchronized (mMethodMap) {
671 mClients.put(client.asBinder(), new ClientState(client,
672 inputContext, uid, pid));
673 }
674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
satoke7c6998e2011-06-03 17:57:59 +0900676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public void removeClient(IInputMethodClient client) {
678 synchronized (mMethodMap) {
679 mClients.remove(client.asBinder());
680 }
681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 void executeOrSendMessage(IInterface target, Message msg) {
684 if (target.asBinder() instanceof Binder) {
685 mCaller.sendMessage(msg);
686 } else {
687 handleMessage(msg);
688 msg.recycle();
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700692 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800694 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 + mCurClient.client.asBinder());
696 if (mBoundToMethod) {
697 mBoundToMethod = false;
698 if (mCurMethod != null) {
699 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
700 MSG_UNBIND_INPUT, mCurMethod));
701 }
702 }
703 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
704 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
705 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 // Call setActive(false) on the old client
708 try {
709 mCurClient.client.setActive(false);
710 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + mCurClient.pid + " uid " + mCurClient.uid);
713 }
714 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project10592532009-03-18 17:39:46 -0700716 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 private int getImeShowFlags() {
721 int flags = 0;
722 if (mShowForced) {
723 flags |= InputMethod.SHOW_FORCED
724 | InputMethod.SHOW_EXPLICIT;
725 } else if (mShowExplicitlyRequested) {
726 flags |= InputMethod.SHOW_EXPLICIT;
727 }
728 return flags;
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 private int getAppShowFlags() {
732 int flags = 0;
733 if (mShowForced) {
734 flags |= InputMethodManager.SHOW_FORCED;
735 } else if (!mShowExplicitlyRequested) {
736 flags |= InputMethodManager.SHOW_IMPLICIT;
737 }
738 return flags;
739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
742 if (!mBoundToMethod) {
743 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
744 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
745 mBoundToMethod = true;
746 }
747 final SessionState session = mCurClient.curSession;
748 if (initial) {
749 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
750 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
751 } else {
752 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
753 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
754 }
755 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800756 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800757 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 return needResult
760 ? new InputBindResult(session.session, mCurId, mCurSeq)
761 : null;
762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 InputBindResult startInputLocked(IInputMethodClient client,
765 IInputContext inputContext, EditorInfo attribute,
766 boolean initial, boolean needResult) {
767 // If no method is currently selected, do nothing.
768 if (mCurMethodId == null) {
769 return mNoBinding;
770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 ClientState cs = mClients.get(client.asBinder());
773 if (cs == null) {
774 throw new IllegalArgumentException("unknown client "
775 + client.asBinder());
776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 try {
779 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
780 // Check with the window manager to make sure this client actually
781 // has a window with focus. If not, reject. This is thread safe
782 // because if the focus changes some time before or after, the
783 // next client receiving focus that has any interest in input will
784 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800785 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
787 return null;
788 }
789 } catch (RemoteException e) {
790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (mCurClient != cs) {
793 // If the client is changing, we need to switch over to the new
794 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700795 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800796 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 + cs.client.asBinder());
798
799 // If the screen is on, inform the new client it is active
800 if (mScreenOn) {
801 try {
802 cs.client.setActive(mScreenOn);
803 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800804 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 + cs.pid + " uid " + cs.uid);
806 }
807 }
808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 // Bump up the sequence for this client and attach it.
811 mCurSeq++;
812 if (mCurSeq <= 0) mCurSeq = 1;
813 mCurClient = cs;
814 mCurInputContext = inputContext;
815 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 // Check if the input method is changing.
818 if (mCurId != null && mCurId.equals(mCurMethodId)) {
819 if (cs.curSession != null) {
820 // Fast case: if we are already connected to the input method,
821 // then just return it.
822 return attachNewInputLocked(initial, needResult);
823 }
824 if (mHaveConnection) {
825 if (mCurMethod != null) {
826 if (!cs.sessionRequested) {
827 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
830 MSG_CREATE_SESSION, mCurMethod,
831 new MethodCallback(mCurMethod)));
832 }
833 // Return to client, and we will get back with it when
834 // we have had a session made for it.
835 return new InputBindResult(null, mCurId, mCurSeq);
836 } else if (SystemClock.uptimeMillis()
837 < (mLastBindTime+TIME_TO_RECONNECT)) {
838 // In this case we have connected to the service, but
839 // don't yet have its interface. If it hasn't been too
840 // long since we did the connection, we'll return to
841 // the client and wait to get the service interface so
842 // we can report back. If it has been too long, we want
843 // to fall through so we can try a disconnect/reconnect
844 // to see if we can get back in touch with the service.
845 return new InputBindResult(null, mCurId, mCurSeq);
846 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800847 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
848 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 }
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700853 return startInputInnerLocked();
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700856 InputBindResult startInputInnerLocked() {
857 if (mCurMethodId == null) {
858 return mNoBinding;
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 if (!mSystemReady) {
862 // If the system is not yet ready, we shouldn't be running third
863 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 InputMethodInfo info = mMethodMap.get(mCurMethodId);
868 if (info == null) {
869 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
875 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700876 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
877 com.android.internal.R.string.input_method_binding_label);
878 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
879 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
881 mLastBindTime = SystemClock.uptimeMillis();
882 mHaveConnection = true;
883 mCurId = info.getId();
884 mCurToken = new Binder();
885 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800886 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 mIWindowManager.addWindowToken(mCurToken,
888 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
889 } catch (RemoteException e) {
890 }
891 return new InputBindResult(null, mCurId, mCurSeq);
892 } else {
893 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 + mCurIntent);
896 }
897 return null;
898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
satoke7c6998e2011-06-03 17:57:59 +0900900 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public InputBindResult startInput(IInputMethodClient client,
902 IInputContext inputContext, EditorInfo attribute,
903 boolean initial, boolean needResult) {
904 synchronized (mMethodMap) {
905 final long ident = Binder.clearCallingIdentity();
906 try {
907 return startInputLocked(client, inputContext, attribute,
908 initial, needResult);
909 } finally {
910 Binder.restoreCallingIdentity(ident);
911 }
912 }
913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800914
satoke7c6998e2011-06-03 17:57:59 +0900915 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public void finishInput(IInputMethodClient client) {
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
satoke7c6998e2011-06-03 17:57:59 +0900919 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void onServiceConnected(ComponentName name, IBinder service) {
921 synchronized (mMethodMap) {
922 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
923 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700924 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800925 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700926 unbindCurrentMethodLocked(false);
927 return;
928 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700930 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
931 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800933 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700934 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700936 MSG_CREATE_SESSION, mCurMethod,
937 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939 }
940 }
941 }
942
943 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
944 synchronized (mMethodMap) {
945 if (mCurMethod != null && method != null
946 && mCurMethod.asBinder() == method.asBinder()) {
947 if (mCurClient != null) {
948 mCurClient.curSession = new SessionState(mCurClient,
949 method, session);
950 mCurClient.sessionRequested = false;
951 InputBindResult res = attachNewInputLocked(true, true);
952 if (res.method != null) {
953 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
954 MSG_BIND_METHOD, mCurClient.client, res));
955 }
956 }
957 }
958 }
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700961 void unbindCurrentMethodLocked(boolean reportToClient) {
962 if (mHaveConnection) {
963 mContext.unbindService(this);
964 mHaveConnection = false;
965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 if (mCurToken != null) {
968 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800969 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 mIWindowManager.removeWindowToken(mCurToken);
971 } catch (RemoteException e) {
972 }
973 mCurToken = null;
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Project10592532009-03-18 17:39:46 -0700976 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 if (reportToClient && mCurClient != null) {
980 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
981 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
982 }
983 }
984
Devin Taylor0c33ed22010-02-23 13:26:46 -0600985 private void finishSession(SessionState sessionState) {
986 if (sessionState != null && sessionState.session != null) {
987 try {
988 sessionState.session.finishSession();
989 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700990 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600991 }
992 }
993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800994
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700995 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (mCurMethod != null) {
997 for (ClientState cs : mClients.values()) {
998 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600999 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 cs.curSession = null;
1001 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001002
1003 finishSession(mEnabledSession);
1004 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 mCurMethod = null;
1006 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001007 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
satoke7c6998e2011-06-03 17:57:59 +09001010 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 public void onServiceDisconnected(ComponentName name) {
1012 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001013 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 + " mCurIntent=" + mCurIntent);
1015 if (mCurMethod != null && mCurIntent != null
1016 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001017 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 // We consider this to be a new bind attempt, since the system
1019 // should now try to restart the service for us.
1020 mLastBindTime = SystemClock.uptimeMillis();
1021 mShowRequested = mInputShown;
1022 mInputShown = false;
1023 if (mCurClient != null) {
1024 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1025 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1026 }
1027 }
1028 }
1029 }
1030
satokf9f01002011-05-19 21:31:50 +09001031 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001033 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 long ident = Binder.clearCallingIdentity();
1035 try {
1036 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001037 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 return;
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 synchronized (mMethodMap) {
1042 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001043 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001044 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001046 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001047 mStatusBar.setIcon("ime", packageName, iconId, 0);
1048 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 }
1051 } finally {
1052 Binder.restoreCallingIdentity(ident);
1053 }
1054 }
1055
satok7cfc0ed2011-06-20 21:29:36 +09001056 private boolean needsToShowImeSwitchOngoingNotification() {
1057 if (!mShowOngoingImeSwitcherForPhones) return false;
1058 synchronized (mMethodMap) {
1059 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1060 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001061 if (N > 2) return true;
1062 if (N < 1) return false;
1063 int nonAuxCount = 0;
1064 int auxCount = 0;
1065 InputMethodSubtype nonAuxSubtype = null;
1066 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001067 for(int i = 0; i < N; ++i) {
1068 final InputMethodInfo imi = imis.get(i);
1069 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1070 imi, true);
1071 final int subtypeCount = subtypes.size();
1072 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001073 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001074 } else {
1075 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001076 final InputMethodSubtype subtype = subtypes.get(j);
1077 if (!subtype.isAuxiliary()) {
1078 ++nonAuxCount;
1079 nonAuxSubtype = subtype;
1080 } else {
1081 ++auxCount;
1082 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001083 }
1084 }
1085 }
satok7cfc0ed2011-06-20 21:29:36 +09001086 }
satokb6359412011-06-28 17:47:41 +09001087 if (nonAuxCount > 1 || auxCount > 1) {
1088 return true;
1089 } else if (nonAuxCount == 1 && auxCount == 1) {
1090 if (nonAuxSubtype != null && auxSubtype != null
1091 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1092 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1093 return false;
1094 }
1095 return true;
1096 }
1097 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001098 }
satok7cfc0ed2011-06-20 21:29:36 +09001099 }
1100
satokf9f01002011-05-19 21:31:50 +09001101 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001102 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001103 int uid = Binder.getCallingUid();
1104 long ident = Binder.clearCallingIdentity();
1105 try {
1106 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001107 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001108 return;
1109 }
1110
1111 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001112 mImeWindowVis = vis;
1113 mBackDisposition = backDisposition;
1114 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001115 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1116 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1117 final PackageManager pm = mContext.getPackageManager();
1118 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1119 final CharSequence title = mRes.getText(
1120 com.android.internal.R.string.select_input_method);
1121 mImeSwitcherNotification.setLatestEventInfo(
1122 mContext, title, label, mImeSwitchPendingIntent);
1123 mNotificationManager.notify(
1124 com.android.internal.R.string.select_input_method,
1125 mImeSwitcherNotification);
1126 mNotificationShown = true;
1127 } else {
1128 if (mNotificationShown) {
1129 mNotificationManager.cancel(
1130 com.android.internal.R.string.select_input_method);
1131 mNotificationShown = false;
1132 }
1133 }
satok06487a52010-10-29 11:37:18 +09001134 }
1135 } finally {
1136 Binder.restoreCallingIdentity(ident);
1137 }
1138 }
1139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
satokf9f01002011-05-19 21:31:50 +09001141 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1142 synchronized (mMethodMap) {
1143 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1144 for (int i = 0; i < spans.length; ++i) {
1145 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001146 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001147 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001148 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001149 }
1150 }
1151 }
1152 }
1153
satoke7c6998e2011-06-03 17:57:59 +09001154 @Override
satokf9f01002011-05-19 21:31:50 +09001155 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1156 synchronized (mMethodMap) {
1157 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1158 // TODO: Do not send the intent if the process of the targetImi is already dead.
1159 if (targetImi != null) {
1160 final String[] suggestions = span.getSuggestions();
1161 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001162 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001163 final Intent intent = new Intent();
1164 // Ensures that only a class in the original IME package will receive the
1165 // notification.
satok42c5a162011-05-26 16:46:14 +09001166 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001167 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1168 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1169 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1170 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1171 mContext.sendBroadcast(intent);
1172 return true;
1173 }
1174 }
1175 return false;
1176 }
1177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001179 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1180 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1181 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1182 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001184 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001185 // There is no input method selected, try to choose new applicable input method.
1186 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1187 id = Settings.Secure.getString(mContext.getContentResolver(),
1188 Settings.Secure.DEFAULT_INPUT_METHOD);
1189 }
1190 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 try {
satokab751aa2010-09-14 19:17:36 +09001192 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001194 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001195 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001196 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
satokf3db1af2010-11-23 13:34:33 +09001198 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001199 } else {
1200 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001201 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001202 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
satokab751aa2010-09-14 19:17:36 +09001206 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 InputMethodInfo info = mMethodMap.get(id);
1208 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001209 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001213 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001214 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1215 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001216 }
1217 if (subtype != mCurrentSubtype) {
1218 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001219 if (subtype != null) {
1220 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1221 }
satokcd7cd292010-11-20 15:46:23 +09001222 if (mCurMethod != null) {
1223 try {
satoke40dea02011-01-30 01:14:02 +09001224 final Configuration conf = mRes.getConfiguration();
1225 final boolean haveHardKeyboard = conf.keyboard
1226 != Configuration.KEYBOARD_NOKEYS;
1227 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001228 && conf.hardKeyboardHidden
1229 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001230 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1231 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1232 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001233 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1234 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001235 // If subtype is null, try to find the most applicable one from
1236 // getCurrentInputMethodSubtype.
1237 if (subtype == null) {
1238 subtype = getCurrentInputMethodSubtype();
1239 }
1240 mCurMethod.changeInputMethodSubtype(subtype);
1241 } catch (RemoteException e) {
1242 return;
satokab751aa2010-09-14 19:17:36 +09001243 }
1244 }
1245 }
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 return;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 final long ident = Binder.clearCallingIdentity();
1251 try {
satokab751aa2010-09-14 19:17:36 +09001252 // Set a subtype to this input method.
1253 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001254 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1255 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1256 // because mCurMethodId is stored as a history in
1257 // setSelectedInputMethodAndSubtypeLocked().
1258 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259
1260 if (ActivityManagerNative.isSystemReady()) {
1261 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001262 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 intent.putExtra("input_method_id", id);
1264 mContext.sendBroadcast(intent);
1265 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001266 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 } finally {
1268 Binder.restoreCallingIdentity(ident);
1269 }
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
satok42c5a162011-05-26 16:46:14 +09001272 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001273 public boolean showSoftInput(IInputMethodClient client, int flags,
1274 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001275 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 long ident = Binder.clearCallingIdentity();
1277 try {
1278 synchronized (mMethodMap) {
1279 if (mCurClient == null || client == null
1280 || mCurClient.client.asBinder() != client.asBinder()) {
1281 try {
1282 // We need to check if this is the current client with
1283 // focus in the window manager, to allow this call to
1284 // be made before input is started in it.
1285 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001286 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001290 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Joe Onorato8a9b2202010-02-26 18:56:32 -08001294 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001295 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297 } finally {
1298 Binder.restoreCallingIdentity(ident);
1299 }
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Project4df24232009-03-05 14:34:35 -08001302 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 mShowRequested = true;
1304 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1305 mShowExplicitlyRequested = true;
1306 }
1307 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1308 mShowExplicitlyRequested = true;
1309 mShowForced = true;
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborncc278702009-09-02 23:07:23 -07001312 if (!mSystemReady) {
1313 return false;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Project4df24232009-03-05 14:34:35 -08001316 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001318 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1319 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1320 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001322 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 } else if (mHaveConnection && SystemClock.uptimeMillis()
1324 < (mLastBindTime+TIME_TO_RECONNECT)) {
1325 // The client has asked to have the input method shown, but
1326 // we have been sitting here too long with a connection to the
1327 // service and no interface received, so let's disconnect/connect
1328 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 SystemClock.uptimeMillis()-mLastBindTime,1);
1331 mContext.unbindService(this);
1332 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Project4df24232009-03-05 14:34:35 -08001335 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
satok42c5a162011-05-26 16:46:14 +09001338 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001339 public boolean hideSoftInput(IInputMethodClient client, int flags,
1340 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001341 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 long ident = Binder.clearCallingIdentity();
1343 try {
1344 synchronized (mMethodMap) {
1345 if (mCurClient == null || client == null
1346 || mCurClient.client.asBinder() != client.asBinder()) {
1347 try {
1348 // We need to check if this is the current client with
1349 // focus in the window manager, to allow this call to
1350 // be made before input is started in it.
1351 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001352 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1353 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001354 mImeWindowVis = 0;
1355 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1356 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001360 mImeWindowVis = 0;
1361 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001362 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Joe Onorato8a9b2202010-02-26 18:56:32 -08001366 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001367 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 } finally {
1370 Binder.restoreCallingIdentity(ident);
1371 }
1372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
The Android Open Source Project4df24232009-03-05 14:34:35 -08001374 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1376 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001384 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1389 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1390 res = true;
1391 } else {
1392 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 mInputShown = false;
1395 mShowRequested = false;
1396 mShowExplicitlyRequested = false;
1397 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
satok42c5a162011-05-26 16:46:14 +09001401 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001402 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1404 boolean first, int windowFlags) {
1405 long ident = Binder.clearCallingIdentity();
1406 try {
1407 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 + " viewHasFocus=" + viewHasFocus
1410 + " isTextEditor=" + isTextEditor
1411 + " softInputMode=#" + Integer.toHexString(softInputMode)
1412 + " first=" + first + " flags=#"
1413 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 if (mCurClient == null || client == null
1416 || mCurClient.client.asBinder() != client.asBinder()) {
1417 try {
1418 // We need to check if this is the current client with
1419 // focus in the window manager, to allow this call to
1420 // be made before input is started in it.
1421 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001422 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 return;
1424 }
1425 } catch (RemoteException e) {
1426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001429 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001430 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001431 return;
1432 }
1433 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001435 // Should we auto-show the IME even if the caller has not
1436 // specified what should be done with it?
1437 // We only do this automatically if the window can resize
1438 // to accommodate the IME (so what the user sees will give
1439 // them good context without input information being obscured
1440 // by the IME) or if running on a large screen where there
1441 // is more room for the target window + IME.
1442 final boolean doAutoShow =
1443 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1444 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1445 || mRes.getConfiguration().isLayoutSizeAtLeast(
1446 Configuration.SCREENLAYOUT_SIZE_LARGE);
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1449 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001450 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1452 // There is no focus view, and this window will
1453 // be behind any soft input window, so hide the
1454 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001456 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001458 } else if (isTextEditor && doAutoShow && (softInputMode &
1459 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 // There is a focus view, and we are navigating forward
1461 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001462 // We only do this automatically if the window an resize
1463 // to accomodate the IME (so what the user sees will give
1464 // them good context without input information being obscured
1465 // by the IME) or if running on a large screen where there
1466 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
1470 break;
1471 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1472 // Do nothing.
1473 break;
1474 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1475 if ((softInputMode &
1476 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001477 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001478 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480 break;
1481 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001483 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 break;
1485 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1486 if ((softInputMode &
1487 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001489 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 break;
1492 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001493 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001494 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 break;
1496 }
1497 }
1498 } finally {
1499 Binder.restoreCallingIdentity(ident);
1500 }
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
satok42c5a162011-05-26 16:46:14 +09001503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1505 synchronized (mMethodMap) {
1506 if (mCurClient == null || client == null
1507 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001508 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001509 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511
satok440aab52010-11-25 09:43:11 +09001512 // Always call subtype picker, because subtype picker is a superset of input method
1513 // picker.
satokab751aa2010-09-14 19:17:36 +09001514 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1515 }
1516 }
1517
satok42c5a162011-05-26 16:46:14 +09001518 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001520 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1521 }
1522
satok42c5a162011-05-26 16:46:14 +09001523 @Override
satok28203512010-11-24 11:06:49 +09001524 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1525 synchronized (mMethodMap) {
1526 if (subtype != null) {
1527 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1528 mMethodMap.get(id), subtype.hashCode()));
1529 } else {
1530 setInputMethod(token, id);
1531 }
1532 }
satokab751aa2010-09-14 19:17:36 +09001533 }
1534
satok42c5a162011-05-26 16:46:14 +09001535 @Override
satokb416a712010-11-25 20:42:14 +09001536 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001537 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001538 synchronized (mMethodMap) {
1539 if (mCurClient == null || client == null
1540 || mCurClient.client.asBinder() != client.asBinder()) {
1541 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1542 }
satok7fee71f2010-12-17 18:54:26 +09001543 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1544 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001545 }
1546 }
1547
satok4fc87d62011-05-20 16:13:43 +09001548 @Override
satok735cf382010-11-11 20:40:09 +09001549 public boolean switchToLastInputMethod(IBinder token) {
1550 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001551 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001552 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001553 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001554 lastImi = mMethodMap.get(lastIme.first);
1555 } else {
1556 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001557 }
satok4fc87d62011-05-20 16:13:43 +09001558 String targetLastImiId = null;
1559 int subtypeId = NOT_A_SUBTYPE_ID;
1560 if (lastIme != null && lastImi != null) {
1561 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1562 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1563 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1564 : mCurrentSubtype.hashCode();
1565 // If the last IME is the same as the current IME and the last subtype is not
1566 // defined, there is no need to switch to the last IME.
1567 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1568 targetLastImiId = lastIme.first;
1569 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1570 }
1571 }
1572
1573 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1574 // This is a safety net. If the currentSubtype can't be added to the history
1575 // and the framework couldn't find the last ime, we will make the last ime be
1576 // the most applicable enabled keyboard subtype of the system imes.
1577 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1578 if (enabled != null) {
1579 final int N = enabled.size();
1580 final String locale = mCurrentSubtype == null
1581 ? mRes.getConfiguration().locale.toString()
1582 : mCurrentSubtype.getLocale();
1583 for (int i = 0; i < N; ++i) {
1584 final InputMethodInfo imi = enabled.get(i);
1585 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1586 InputMethodSubtype keyboardSubtype =
1587 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1588 SUBTYPE_MODE_KEYBOARD, locale, true);
1589 if (keyboardSubtype != null) {
1590 targetLastImiId = imi.getId();
1591 subtypeId = getSubtypeIdFromHashCode(
1592 imi, keyboardSubtype.hashCode());
1593 if(keyboardSubtype.getLocale().equals(locale)) {
1594 break;
1595 }
1596 }
1597 }
1598 }
1599 }
1600 }
1601
1602 if (!TextUtils.isEmpty(targetLastImiId)) {
1603 if (DEBUG) {
1604 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1605 + ", from: " + mCurMethodId + ", " + subtypeId);
1606 }
1607 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1608 return true;
1609 } else {
1610 return false;
1611 }
satok735cf382010-11-11 20:40:09 +09001612 }
1613 }
1614
satoke7c6998e2011-06-03 17:57:59 +09001615 @Override
satok68f1b782011-04-11 14:26:04 +09001616 public InputMethodSubtype getLastInputMethodSubtype() {
1617 synchronized (mMethodMap) {
1618 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1619 // TODO: Handle the case of the last IME with no subtypes
1620 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1621 || TextUtils.isEmpty(lastIme.second)) return null;
1622 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1623 if (lastImi == null) return null;
1624 try {
1625 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1626 return lastImi.getSubtypeAt(getSubtypeIdFromHashCode(
1627 lastImi, lastSubtypeHash));
1628 } catch (NumberFormatException e) {
1629 return null;
1630 }
1631 }
1632 }
1633
satoke7c6998e2011-06-03 17:57:59 +09001634 @Override
1635 public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) {
1636 if (token == null || mCurToken != token) {
1637 return false;
1638 }
1639 if (subtypes == null || subtypes.length == 0) return false;
1640 synchronized (mMethodMap) {
1641 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1642 if (imi == null) return false;
1643 final int N = subtypes.length;
1644 mFileManager.addInputMethodSubtypes(mCurMethodId, subtypes);
1645 buildInputMethodListLocked(mMethodList, mMethodMap);
1646 return true;
1647 }
1648 }
1649
satok28203512010-11-24 11:06:49 +09001650 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 synchronized (mMethodMap) {
1652 if (token == null) {
1653 if (mContext.checkCallingOrSelfPermission(
1654 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1655 != PackageManager.PERMISSION_GRANTED) {
1656 throw new SecurityException(
1657 "Using null token requires permission "
1658 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1659 }
1660 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001661 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1662 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 return;
1664 }
1665
1666 long ident = Binder.clearCallingIdentity();
1667 try {
satokab751aa2010-09-14 19:17:36 +09001668 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 } finally {
1670 Binder.restoreCallingIdentity(ident);
1671 }
1672 }
1673 }
1674
satok42c5a162011-05-26 16:46:14 +09001675 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 public void hideMySoftInput(IBinder token, int flags) {
1677 synchronized (mMethodMap) {
1678 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001679 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1680 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return;
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 long ident = Binder.clearCallingIdentity();
1684 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001685 hideCurrentInputLocked(flags, null);
1686 } finally {
1687 Binder.restoreCallingIdentity(ident);
1688 }
1689 }
1690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001691
satok42c5a162011-05-26 16:46:14 +09001692 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001693 public void showMySoftInput(IBinder token, int flags) {
1694 synchronized (mMethodMap) {
1695 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001696 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1697 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 return;
1699 }
1700 long ident = Binder.clearCallingIdentity();
1701 try {
1702 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 } finally {
1704 Binder.restoreCallingIdentity(ident);
1705 }
1706 }
1707 }
1708
1709 void setEnabledSessionInMainThread(SessionState session) {
1710 if (mEnabledSession != session) {
1711 if (mEnabledSession != null) {
1712 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001713 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 mEnabledSession.method.setSessionEnabled(
1715 mEnabledSession.session, false);
1716 } catch (RemoteException e) {
1717 }
1718 }
1719 mEnabledSession = session;
1720 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001721 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 session.method.setSessionEnabled(
1723 session.session, true);
1724 } catch (RemoteException e) {
1725 }
1726 }
1727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
satok42c5a162011-05-26 16:46:14 +09001729 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 public boolean handleMessage(Message msg) {
1731 HandlerCaller.SomeArgs args;
1732 switch (msg.what) {
1733 case MSG_SHOW_IM_PICKER:
1734 showInputMethodMenu();
1735 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
satokab751aa2010-09-14 19:17:36 +09001737 case MSG_SHOW_IM_SUBTYPE_PICKER:
1738 showInputMethodSubtypeMenu();
1739 return true;
1740
satok47a44912010-10-06 16:03:58 +09001741 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001742 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001743 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001744 return true;
1745
1746 case MSG_SHOW_IM_CONFIG:
1747 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001748 return true;
1749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 case MSG_UNBIND_INPUT:
1753 try {
1754 ((IInputMethod)msg.obj).unbindInput();
1755 } catch (RemoteException e) {
1756 // There is nothing interesting about the method dying.
1757 }
1758 return true;
1759 case MSG_BIND_INPUT:
1760 args = (HandlerCaller.SomeArgs)msg.obj;
1761 try {
1762 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1763 } catch (RemoteException e) {
1764 }
1765 return true;
1766 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001767 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1770 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 } catch (RemoteException e) {
1772 }
1773 return true;
1774 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001775 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001777 ((IInputMethod)args.arg1).hideSoftInput(0,
1778 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 } catch (RemoteException e) {
1780 }
1781 return true;
1782 case MSG_ATTACH_TOKEN:
1783 args = (HandlerCaller.SomeArgs)msg.obj;
1784 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1787 } catch (RemoteException e) {
1788 }
1789 return true;
1790 case MSG_CREATE_SESSION:
1791 args = (HandlerCaller.SomeArgs)msg.obj;
1792 try {
1793 ((IInputMethod)args.arg1).createSession(
1794 (IInputMethodCallback)args.arg2);
1795 } catch (RemoteException e) {
1796 }
1797 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 case MSG_START_INPUT:
1801 args = (HandlerCaller.SomeArgs)msg.obj;
1802 try {
1803 SessionState session = (SessionState)args.arg1;
1804 setEnabledSessionInMainThread(session);
1805 session.method.startInput((IInputContext)args.arg2,
1806 (EditorInfo)args.arg3);
1807 } catch (RemoteException e) {
1808 }
1809 return true;
1810 case MSG_RESTART_INPUT:
1811 args = (HandlerCaller.SomeArgs)msg.obj;
1812 try {
1813 SessionState session = (SessionState)args.arg1;
1814 setEnabledSessionInMainThread(session);
1815 session.method.restartInput((IInputContext)args.arg2,
1816 (EditorInfo)args.arg3);
1817 } catch (RemoteException e) {
1818 }
1819 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 case MSG_UNBIND_METHOD:
1824 try {
1825 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1826 } catch (RemoteException e) {
1827 // There is nothing interesting about the last client dying.
1828 }
1829 return true;
1830 case MSG_BIND_METHOD:
1831 args = (HandlerCaller.SomeArgs)msg.obj;
1832 try {
1833 ((IInputMethodClient)args.arg1).onBindMethod(
1834 (InputBindResult)args.arg2);
1835 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001836 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838 return true;
1839 }
1840 return false;
1841 }
1842
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001843 private boolean isSystemIme(InputMethodInfo inputMethod) {
1844 return (inputMethod.getServiceInfo().applicationInfo.flags
1845 & ApplicationInfo.FLAG_SYSTEM) != 0;
1846 }
1847
Ken Wakasa586f0512011-01-20 22:31:01 +09001848 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1849 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1850 final int subtypeCount = imi.getSubtypeCount();
1851 for (int i = 0; i < subtypeCount; ++i) {
1852 subtypes.add(imi.getSubtypeAt(i));
1853 }
1854 return subtypes;
1855 }
1856
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001857 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001858 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001859 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001860 // We'd prefer to fall back on a system IME, since that is safer.
1861 int i=enabled.size();
1862 while (i > 0) {
1863 i--;
1864 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1865 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1866 break;
1867 }
1868 }
satokab751aa2010-09-14 19:17:36 +09001869 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001870 if (DEBUG) {
1871 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1872 }
satok723a27e2010-11-11 14:58:11 +09001873 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001874 return true;
1875 }
1876
1877 return false;
1878 }
1879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1881 HashMap<String, InputMethodInfo> map) {
1882 list.clear();
1883 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001886 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001887 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1888 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1889 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1890 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891
1892 List<ResolveInfo> services = pm.queryIntentServices(
1893 new Intent(InputMethod.SERVICE_INTERFACE),
1894 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001895
satoke7c6998e2011-06-03 17:57:59 +09001896 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1897 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 for (int i = 0; i < services.size(); ++i) {
1899 ResolveInfo ri = services.get(i);
1900 ServiceInfo si = ri.serviceInfo;
1901 ComponentName compName = new ComponentName(si.packageName, si.name);
1902 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1903 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001904 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 + ": it does not require the permission "
1906 + android.Manifest.permission.BIND_INPUT_METHOD);
1907 continue;
1908 }
1909
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911
1912 try {
satoke7c6998e2011-06-03 17:57:59 +09001913 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001915 final String id = p.getId();
1916 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917
Amith Yamasanie861ec12010-03-24 21:39:27 -07001918 // System IMEs are enabled by default, unless there's a hard keyboard
1919 // and the system IME was explicitly disabled
1920 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1921 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001922 }
1923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001931 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001934
1935 String defaultIme = Settings.Secure.getString(mContext
1936 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001937 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001938 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001939 updateFromSettingsLocked();
1940 }
1941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001945
satokab751aa2010-09-14 19:17:36 +09001946 private void showInputMethodMenu() {
1947 showInputMethodMenuInternal(false);
1948 }
1949
1950 private void showInputMethodSubtypeMenu() {
1951 showInputMethodMenuInternal(true);
1952 }
1953
satok217f5482010-12-15 05:19:19 +09001954 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001955 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001956 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001957 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1958 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001959 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001960 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001961 }
satok217f5482010-12-15 05:19:19 +09001962 mContext.startActivity(intent);
1963 }
1964
1965 private void showConfigureInputMethods() {
1966 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1967 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1968 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1969 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001970 mContext.startActivity(intent);
1971 }
1972
satokab751aa2010-09-14 19:17:36 +09001973 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001974 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 String lastInputMethodId = Settings.Secure.getString(context
1981 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001982 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001983 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001984
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001985 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001986 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1987 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001988 if (immis == null || immis.size() == 0) {
1989 return;
1990 }
1991
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001992 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993
Ken Wakasa761eb372011-03-04 19:06:18 +09001994 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1995 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1996 new Comparator<InputMethodInfo>() {
1997 @Override
1998 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
1999 if (imi2 == null) return 0;
2000 if (imi1 == null) return 1;
2001 if (pm == null) {
2002 return imi1.getId().compareTo(imi2.getId());
2003 }
2004 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2005 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2006 return imiId1.toString().compareTo(imiId2.toString());
2007 }
2008 });
satok913a8922010-08-26 21:53:41 +09002009
Ken Wakasa761eb372011-03-04 19:06:18 +09002010 sortedImmis.putAll(immis);
2011
2012 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2013 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2014
2015 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002016 if (imi == null) continue;
2017 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002018 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002019 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2020 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002021 }
satokbb4aa062011-01-19 21:40:27 +09002022 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002023 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002024 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002025 final int subtypeCount = imi.getSubtypeCount();
Ken Wakasa586f0512011-01-20 22:31:01 +09002026 for (int j = 0; j < subtypeCount; ++j) {
satokbb4aa062011-01-19 21:40:27 +09002027 InputMethodSubtype subtype = imi.getSubtypeAt(j);
satok9b415792011-05-30 12:37:52 +09002028 if (enabledSubtypeSet.contains(String.valueOf(subtype.hashCode()))
2029 && !subtype.isAuxiliary()) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002030 final CharSequence title;
satok7f35c8c2010-10-07 21:13:11 +09002031 int nameResId = subtype.getNameResId();
satok9ef02832010-11-04 21:17:48 +09002032 String mode = subtype.getMode();
satok7f35c8c2010-10-07 21:13:11 +09002033 if (nameResId != 0) {
satok4f313532011-06-01 16:13:45 +09002034 title = TextUtils.concat(subtype.getDisplayName(context,
2035 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
Ken Wakasa761eb372011-03-04 19:06:18 +09002036 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
satok7f35c8c2010-10-07 21:13:11 +09002037 } else {
2038 CharSequence language = subtype.getLocale();
satok7f35c8c2010-10-07 21:13:11 +09002039 // TODO: Use more friendly Title and UI
2040 title = label + "," + (mode == null ? "" : mode) + ","
2041 + (language == null ? "" : language);
2042 }
Ken Wakasa761eb372011-03-04 19:06:18 +09002043 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2044 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satokab751aa2010-09-14 19:17:36 +09002045 }
satokab751aa2010-09-14 19:17:36 +09002046 }
2047 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002048 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2049 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002050 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002051 }
satok913a8922010-08-26 21:53:41 +09002052
Ken Wakasa761eb372011-03-04 19:06:18 +09002053 final int N = imList.size();
2054 mItems = new CharSequence[N];
2055 for (int i = 0; i < N; ++i) {
2056 mItems[i] = imList.get(i).first;
2057 }
satokab751aa2010-09-14 19:17:36 +09002058 mIms = new InputMethodInfo[N];
2059 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002060 int checkedItem = 0;
2061 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002062 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002063 mIms[i] = value.first;
2064 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002065 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002066 int subtypeId = mSubtypeIds[i];
2067 if ((subtypeId == NOT_A_SUBTYPE_ID)
2068 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2069 || (subtypeId == lastInputMethodSubtypeId)) {
2070 checkedItem = i;
2071 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
satokab751aa2010-09-14 19:17:36 +09002074
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002075 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002076 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002077 public void onClick(DialogInterface dialog, int which) {
2078 hideInputMethodMenu();
2079 }
2080 };
satokd87c2592010-09-29 11:52:06 +09002081
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002082 TypedArray a = context.obtainStyledAttributes(null,
2083 com.android.internal.R.styleable.DialogPreference,
2084 com.android.internal.R.attr.alertDialogStyle, 0);
2085 mDialogBuilder = new AlertDialog.Builder(context)
2086 .setTitle(com.android.internal.R.string.select_input_method)
2087 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002088 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002089 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002091 }
2092 })
2093 .setIcon(a.getDrawable(
2094 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2095 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002096
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002097 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2098 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002099 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002100 public void onClick(DialogInterface dialog, int which) {
2101 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002102 if (mIms == null || mIms.length <= which
2103 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002104 return;
2105 }
2106 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002107 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002108 hideInputMethodMenu();
2109 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002110 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002111 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002112 subtypeId = NOT_A_SUBTYPE_ID;
2113 }
2114 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002115 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002118 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119
satok7f35c8c2010-10-07 21:13:11 +09002120 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09002121 mDialogBuilder.setPositiveButton(
2122 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002123 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002124 @Override
satok7f35c8c2010-10-07 21:13:11 +09002125 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002126 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002127 }
2128 });
2129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002131 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 mSwitchingDialog.getWindow().setType(
2133 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002134 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 mSwitchingDialog.show();
2136 }
2137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002140 synchronized (mMethodMap) {
2141 hideInputMethodMenuLocked();
2142 }
2143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144
The Android Open Source Project10592532009-03-18 17:39:46 -07002145 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002146 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147
The Android Open Source Project10592532009-03-18 17:39:46 -07002148 if (mSwitchingDialog != null) {
2149 mSwitchingDialog.dismiss();
2150 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002152
The Android Open Source Project10592532009-03-18 17:39:46 -07002153 mDialogBuilder = null;
2154 mItems = null;
2155 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
satok42c5a162011-05-26 16:46:14 +09002160 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 public boolean setInputMethodEnabled(String id, boolean enabled) {
2162 synchronized (mMethodMap) {
2163 if (mContext.checkCallingOrSelfPermission(
2164 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2165 != PackageManager.PERMISSION_GRANTED) {
2166 throw new SecurityException(
2167 "Requires permission "
2168 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2169 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 long ident = Binder.clearCallingIdentity();
2172 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002173 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 } finally {
2175 Binder.restoreCallingIdentity(ident);
2176 }
2177 }
2178 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002179
2180 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2181 // Make sure this is a valid input method.
2182 InputMethodInfo imm = mMethodMap.get(id);
2183 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002184 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002185 }
2186
satokd87c2592010-09-29 11:52:06 +09002187 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2188 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002189
satokd87c2592010-09-29 11:52:06 +09002190 if (enabled) {
2191 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2192 if (pair.first.equals(id)) {
2193 // We are enabling this input method, but it is already enabled.
2194 // Nothing to do. The previous state was enabled.
2195 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002196 }
2197 }
satokd87c2592010-09-29 11:52:06 +09002198 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2199 // Previous state was disabled.
2200 return false;
2201 } else {
2202 StringBuilder builder = new StringBuilder();
2203 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2204 builder, enabledInputMethodsList, id)) {
2205 // Disabled input method is currently selected, switch to another one.
2206 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2207 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002208 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2209 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2210 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002211 }
2212 // Previous state was enabled.
2213 return true;
2214 } else {
2215 // We are disabling the input method but it is already disabled.
2216 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002217 return false;
2218 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002219 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002220 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002221
satok57ffc002011-01-25 00:11:47 +09002222 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2223 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002224 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002225 }
2226
satok723a27e2010-11-11 14:58:11 +09002227 private void saveCurrentInputMethodAndSubtypeToHistory() {
2228 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2229 if (mCurrentSubtype != null) {
2230 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2231 }
satok57ffc002011-01-25 00:11:47 +09002232 if (canAddToLastInputMethod(mCurrentSubtype)) {
2233 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2234 }
satokab751aa2010-09-14 19:17:36 +09002235 }
2236
satok723a27e2010-11-11 14:58:11 +09002237 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2238 boolean setSubtypeOnly) {
2239 // Update the history of InputMethod and Subtype
2240 saveCurrentInputMethodAndSubtypeToHistory();
2241
2242 // Set Subtype here
2243 if (imi == null || subtypeId < 0) {
2244 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002245 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002246 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002247 if (subtypeId < imi.getSubtypeCount()) {
2248 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2249 mSettings.putSelectedSubtype(subtype.hashCode());
2250 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002251 } else {
2252 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2253 mCurrentSubtype = null;
2254 }
satokab751aa2010-09-14 19:17:36 +09002255 }
satok723a27e2010-11-11 14:58:11 +09002256
2257 if (!setSubtypeOnly) {
2258 // Set InputMethod here
2259 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2260 }
2261 }
2262
2263 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2264 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2265 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2266 // newDefaultIme is empty when there is no candidate for the selected IME.
2267 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2268 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2269 if (subtypeHashCode != null) {
2270 try {
2271 lastSubtypeId = getSubtypeIdFromHashCode(
2272 imi, Integer.valueOf(subtypeHashCode));
2273 } catch (NumberFormatException e) {
2274 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2275 }
2276 }
2277 }
2278 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002279 }
2280
2281 private int getSelectedInputMethodSubtypeId(String id) {
2282 InputMethodInfo imi = mMethodMap.get(id);
2283 if (imi == null) {
2284 return NOT_A_SUBTYPE_ID;
2285 }
satokab751aa2010-09-14 19:17:36 +09002286 int subtypeId;
2287 try {
2288 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2289 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2290 } catch (SettingNotFoundException e) {
2291 return NOT_A_SUBTYPE_ID;
2292 }
satok723a27e2010-11-11 14:58:11 +09002293 return getSubtypeIdFromHashCode(imi, subtypeId);
2294 }
2295
2296 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002297 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002298 final int subtypeCount = imi.getSubtypeCount();
2299 for (int i = 0; i < subtypeCount; ++i) {
2300 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002301 if (subtypeHashCode == ims.hashCode()) {
2302 return i;
2303 }
satokab751aa2010-09-14 19:17:36 +09002304 }
2305 }
2306 return NOT_A_SUBTYPE_ID;
2307 }
2308
satokdf31ae62011-01-15 06:19:44 +09002309 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2310 Resources res, List<InputMethodSubtype> subtypes) {
2311 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002312 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2313 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2314 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002315 final int N = subtypes.size();
2316 boolean containsKeyboardSubtype = false;
2317 for (int i = 0; i < N; ++i) {
2318 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002319 final String locale = subtype.getLocale();
2320 final String mode = subtype.getMode();
2321 // When system locale starts with subtype's locale, that subtype will be applicable
2322 // for system locale
2323 // For instance, it's clearly applicable for cases like system locale = en_US and
2324 // subtype = en, but it is not necessarily considered applicable for cases like system
2325 // locale = en and subtype = en_US.
2326 // We just call systemLocale.startsWith(locale) in this function because there is no
2327 // need to find applicable subtypes aggressively unlike
2328 // findLastResortApplicableSubtypeLocked.
2329 if (systemLocale.startsWith(locale)) {
2330 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2331 // If more applicable subtypes are contained, skip.
2332 if (applicableSubtype != null
2333 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2334 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002335 if (!containsKeyboardSubtype
2336 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2337 containsKeyboardSubtype = true;
2338 }
2339 }
2340 }
satok3da92232011-01-11 22:46:30 +09002341 ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2342 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002343 if (!containsKeyboardSubtype) {
2344 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002345 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002346 if (lastResortKeyboardSubtype != null) {
2347 applicableSubtypes.add(lastResortKeyboardSubtype);
2348 }
2349 }
2350 return applicableSubtypes;
2351 }
2352
satok4e4569d2010-11-19 18:45:53 +09002353 /**
2354 * If there are no selected subtypes, tries finding the most applicable one according to the
2355 * given locale.
2356 * @param subtypes this function will search the most applicable subtype in subtypes
2357 * @param mode subtypes will be filtered by mode
2358 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002359 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2360 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002361 * @return the most applicable subtypeId
2362 */
satokdf31ae62011-01-15 06:19:44 +09002363 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2364 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002365 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002366 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002367 return null;
satok8fbb1e82010-11-02 23:15:58 +09002368 }
satok4e4569d2010-11-19 18:45:53 +09002369 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002370 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002371 }
satok8fbb1e82010-11-02 23:15:58 +09002372 final String language = locale.substring(0, 2);
2373 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002374 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002375 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002376 final int N = subtypes.size();
2377 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002378 InputMethodSubtype subtype = subtypes.get(i);
2379 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002380 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2381 // and all subtypes with all modes can be candidates.
2382 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002383 if (firstMatchedModeSubtype == null) {
2384 firstMatchedModeSubtype = subtype;
2385 }
satok9ef02832010-11-04 21:17:48 +09002386 if (locale.equals(subtypeLocale)) {
2387 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002388 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002389 break;
2390 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2391 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002392 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002393 partialMatchFound = true;
2394 }
satok8fbb1e82010-11-02 23:15:58 +09002395 }
2396 }
2397
satok7599a7f2010-12-22 13:45:23 +09002398 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2399 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002400 }
2401
satok8fbb1e82010-11-02 23:15:58 +09002402 // The first subtype applicable to the system locale will be defined as the most applicable
2403 // subtype.
2404 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002405 if (applicableSubtype != null) {
2406 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2407 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2408 }
satok8fbb1e82010-11-02 23:15:58 +09002409 }
satokcd7cd292010-11-20 15:46:23 +09002410 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002411 }
2412
satok4e4569d2010-11-19 18:45:53 +09002413 // If there are no selected shortcuts, tries finding the most applicable ones.
2414 private Pair<InputMethodInfo, InputMethodSubtype>
2415 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2416 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2417 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002418 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002419 boolean foundInSystemIME = false;
2420
2421 // Search applicable subtype for each InputMethodInfo
2422 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002423 final String imiId = imi.getId();
2424 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2425 continue;
2426 }
satokcd7cd292010-11-20 15:46:23 +09002427 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002428 final List<InputMethodSubtype> enabledSubtypes =
2429 getEnabledInputMethodSubtypeList(imi, true);
2430 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002431 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002432 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002433 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002434 }
satokdf31ae62011-01-15 06:19:44 +09002435 // 2. Search by the system locale from enabledSubtypes.
2436 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002437 if (subtype == null) {
2438 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002439 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002440 }
satok7599a7f2010-12-22 13:45:23 +09002441 // 4. Search by the current subtype's locale from all subtypes.
2442 if (subtype == null && mCurrentSubtype != null) {
2443 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002444 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002445 }
2446 // 5. Search by the system locale from all subtypes.
2447 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002448 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002449 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002450 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002451 }
satokcd7cd292010-11-20 15:46:23 +09002452 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002453 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002454 // The current input method is the most applicable IME.
2455 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002456 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002457 break;
satok7599a7f2010-12-22 13:45:23 +09002458 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002459 // The system input method is 2nd applicable IME.
2460 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002461 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002462 if ((imi.getServiceInfo().applicationInfo.flags
2463 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2464 foundInSystemIME = true;
2465 }
satok4e4569d2010-11-19 18:45:53 +09002466 }
2467 }
2468 }
2469 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002470 if (mostApplicableIMI != null) {
2471 Slog.w(TAG, "Most applicable shortcut input method was:"
2472 + mostApplicableIMI.getId());
2473 if (mostApplicableSubtype != null) {
2474 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2475 + "," + mostApplicableSubtype.getMode() + ","
2476 + mostApplicableSubtype.getLocale());
2477 }
2478 }
satok4e4569d2010-11-19 18:45:53 +09002479 }
satokcd7cd292010-11-20 15:46:23 +09002480 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002481 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002482 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002483 } else {
2484 return null;
2485 }
2486 }
2487
satokab751aa2010-09-14 19:17:36 +09002488 /**
2489 * @return Return the current subtype of this input method.
2490 */
satok42c5a162011-05-26 16:46:14 +09002491 @Override
satokab751aa2010-09-14 19:17:36 +09002492 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002493 boolean subtypeIsSelected = false;
2494 try {
2495 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2496 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2497 } catch (SettingNotFoundException e) {
2498 }
satok3ef8b292010-11-23 06:06:29 +09002499 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002500 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002501 String lastInputMethodId = Settings.Secure.getString(
2502 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002503 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2504 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002505 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2506 if (imi != null) {
2507 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002508 // the most applicable subtype from explicitly or implicitly enabled
2509 // subtypes.
2510 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2511 getEnabledInputMethodSubtypeList(imi, true);
2512 // If there is only one explicitly or implicitly enabled subtype,
2513 // just returns it.
2514 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2515 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2516 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2517 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2518 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2519 SUBTYPE_MODE_KEYBOARD, null, true);
2520 if (mCurrentSubtype == null) {
2521 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2522 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2523 true);
2524 }
2525 }
satok4e4569d2010-11-19 18:45:53 +09002526 }
satokcd7cd292010-11-20 15:46:23 +09002527 } else {
satok3ef8b292010-11-23 06:06:29 +09002528 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002529 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002530 }
satok8fbb1e82010-11-02 23:15:58 +09002531 }
satok3ef8b292010-11-23 06:06:29 +09002532 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002533 }
satokab751aa2010-09-14 19:17:36 +09002534 }
2535
satokf3db1af2010-11-23 13:34:33 +09002536 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2537 InputMethodSubtype subtype) {
2538 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2539 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2540 } else {
2541 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2542 subtypes.add(subtype);
2543 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2544 }
2545 }
2546
satok4e4569d2010-11-19 18:45:53 +09002547 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002548 @Override
satok4e4569d2010-11-19 18:45:53 +09002549 public List getShortcutInputMethodsAndSubtypes() {
2550 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002551 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002552 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002553 // If there are no selected shortcut subtypes, the framework will try to find
2554 // the most applicable subtype from all subtypes whose mode is
2555 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002556 Pair<InputMethodInfo, InputMethodSubtype> info =
2557 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2558 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002559 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002560 ret.add(info.first);
2561 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002562 }
satok3da92232011-01-11 22:46:30 +09002563 return ret;
satokf3db1af2010-11-23 13:34:33 +09002564 }
satokf3db1af2010-11-23 13:34:33 +09002565 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2566 ret.add(imi);
2567 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2568 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002569 }
2570 }
satokf3db1af2010-11-23 13:34:33 +09002571 return ret;
satok4e4569d2010-11-19 18:45:53 +09002572 }
2573 }
2574
satok42c5a162011-05-26 16:46:14 +09002575 @Override
satokb66d2872010-11-10 01:04:04 +09002576 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2577 synchronized (mMethodMap) {
2578 if (subtype != null && mCurMethodId != null) {
2579 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2580 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2581 if (subtypeId != NOT_A_SUBTYPE_ID) {
2582 setInputMethodLocked(mCurMethodId, subtypeId);
2583 return true;
2584 }
2585 }
2586 return false;
2587 }
2588 }
2589
satokd87c2592010-09-29 11:52:06 +09002590 /**
2591 * Utility class for putting and getting settings for InputMethod
2592 * TODO: Move all putters and getters of settings to this class.
2593 */
2594 private static class InputMethodSettings {
2595 // The string for enabled input method is saved as follows:
2596 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2597 private static final char INPUT_METHOD_SEPARATER = ':';
2598 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002599 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002600 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2601
satok723a27e2010-11-11 14:58:11 +09002602 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002603 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2604
satokdf31ae62011-01-15 06:19:44 +09002605 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002606 private final ContentResolver mResolver;
2607 private final HashMap<String, InputMethodInfo> mMethodMap;
2608 private final ArrayList<InputMethodInfo> mMethodList;
2609
2610 private String mEnabledInputMethodsStrCache;
2611
2612 private static void buildEnabledInputMethodsSettingString(
2613 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2614 String id = pair.first;
2615 ArrayList<String> subtypes = pair.second;
2616 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002617 // Inputmethod and subtypes are saved in the settings as follows:
2618 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2619 for (String subtypeId: subtypes) {
2620 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002621 }
2622 }
2623
2624 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002625 Resources res, ContentResolver resolver,
2626 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2627 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002628 mResolver = resolver;
2629 mMethodMap = methodMap;
2630 mMethodList = methodList;
2631 }
2632
2633 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2634 return createEnabledInputMethodListLocked(
2635 getEnabledInputMethodsAndSubtypeListLocked());
2636 }
2637
satok7f35c8c2010-10-07 21:13:11 +09002638 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002639 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2640 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002641 getEnabledInputMethodsAndSubtypeListLocked());
2642 }
2643
satok67ddf9c2010-11-17 09:45:54 +09002644 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2645 InputMethodInfo imi) {
2646 List<Pair<String, ArrayList<String>>> imsList =
2647 getEnabledInputMethodsAndSubtypeListLocked();
2648 ArrayList<InputMethodSubtype> enabledSubtypes =
2649 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002650 if (imi != null) {
2651 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2652 InputMethodInfo info = mMethodMap.get(imsPair.first);
2653 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002654 final int subtypeCount = info.getSubtypeCount();
2655 for (int i = 0; i < subtypeCount; ++i) {
2656 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002657 for (String s: imsPair.second) {
2658 if (String.valueOf(ims.hashCode()).equals(s)) {
2659 enabledSubtypes.add(ims);
2660 }
satok67ddf9c2010-11-17 09:45:54 +09002661 }
2662 }
satok884ef9a2010-11-18 10:39:46 +09002663 break;
satok67ddf9c2010-11-17 09:45:54 +09002664 }
satok67ddf9c2010-11-17 09:45:54 +09002665 }
2666 }
2667 return enabledSubtypes;
2668 }
2669
satokd87c2592010-09-29 11:52:06 +09002670 // At the initial boot, the settings for input methods are not set,
2671 // so we need to enable IME in that case.
2672 public void enableAllIMEsIfThereIsNoEnabledIME() {
2673 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2674 StringBuilder sb = new StringBuilder();
2675 final int N = mMethodList.size();
2676 for (int i = 0; i < N; i++) {
2677 InputMethodInfo imi = mMethodList.get(i);
2678 Slog.i(TAG, "Adding: " + imi.getId());
2679 if (i > 0) sb.append(':');
2680 sb.append(imi.getId());
2681 }
2682 putEnabledInputMethodsStr(sb.toString());
2683 }
2684 }
2685
satokbb4aa062011-01-19 21:40:27 +09002686 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002687 ArrayList<Pair<String, ArrayList<String>>> imsList
2688 = new ArrayList<Pair<String, ArrayList<String>>>();
2689 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2690 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2691 return imsList;
2692 }
satok723a27e2010-11-11 14:58:11 +09002693 mInputMethodSplitter.setString(enabledInputMethodsStr);
2694 while (mInputMethodSplitter.hasNext()) {
2695 String nextImsStr = mInputMethodSplitter.next();
2696 mSubtypeSplitter.setString(nextImsStr);
2697 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002698 ArrayList<String> subtypeHashes = new ArrayList<String>();
2699 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002700 String imeId = mSubtypeSplitter.next();
2701 while (mSubtypeSplitter.hasNext()) {
2702 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002703 }
2704 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2705 }
2706 }
2707 return imsList;
2708 }
2709
2710 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2711 if (reloadInputMethodStr) {
2712 getEnabledInputMethodsStr();
2713 }
2714 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2715 // Add in the newly enabled input method.
2716 putEnabledInputMethodsStr(id);
2717 } else {
2718 putEnabledInputMethodsStr(
2719 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2720 }
2721 }
2722
2723 /**
2724 * Build and put a string of EnabledInputMethods with removing specified Id.
2725 * @return the specified id was removed or not.
2726 */
2727 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2728 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2729 boolean isRemoved = false;
2730 boolean needsAppendSeparator = false;
2731 for (Pair<String, ArrayList<String>> ims: imsList) {
2732 String curId = ims.first;
2733 if (curId.equals(id)) {
2734 // We are disabling this input method, and it is
2735 // currently enabled. Skip it to remove from the
2736 // new list.
2737 isRemoved = true;
2738 } else {
2739 if (needsAppendSeparator) {
2740 builder.append(INPUT_METHOD_SEPARATER);
2741 } else {
2742 needsAppendSeparator = true;
2743 }
2744 buildEnabledInputMethodsSettingString(builder, ims);
2745 }
2746 }
2747 if (isRemoved) {
2748 // Update the setting with the new list of input methods.
2749 putEnabledInputMethodsStr(builder.toString());
2750 }
2751 return isRemoved;
2752 }
2753
2754 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2755 List<Pair<String, ArrayList<String>>> imsList) {
2756 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2757 for (Pair<String, ArrayList<String>> ims: imsList) {
2758 InputMethodInfo info = mMethodMap.get(ims.first);
2759 if (info != null) {
2760 res.add(info);
2761 }
2762 }
2763 return res;
2764 }
2765
satok7f35c8c2010-10-07 21:13:11 +09002766 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002767 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002768 List<Pair<String, ArrayList<String>>> imsList) {
2769 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2770 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2771 for (Pair<String, ArrayList<String>> ims : imsList) {
2772 InputMethodInfo info = mMethodMap.get(ims.first);
2773 if (info != null) {
2774 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2775 }
2776 }
2777 return res;
2778 }
2779
satokd87c2592010-09-29 11:52:06 +09002780 private void putEnabledInputMethodsStr(String str) {
2781 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2782 mEnabledInputMethodsStrCache = str;
2783 }
2784
2785 private String getEnabledInputMethodsStr() {
2786 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2787 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002788 if (DEBUG) {
2789 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2790 }
satokd87c2592010-09-29 11:52:06 +09002791 return mEnabledInputMethodsStrCache;
2792 }
satok723a27e2010-11-11 14:58:11 +09002793
2794 private void saveSubtypeHistory(
2795 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2796 StringBuilder builder = new StringBuilder();
2797 boolean isImeAdded = false;
2798 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2799 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2800 newSubtypeId);
2801 isImeAdded = true;
2802 }
2803 for (Pair<String, String> ime: savedImes) {
2804 String imeId = ime.first;
2805 String subtypeId = ime.second;
2806 if (TextUtils.isEmpty(subtypeId)) {
2807 subtypeId = NOT_A_SUBTYPE_ID_STR;
2808 }
2809 if (isImeAdded) {
2810 builder.append(INPUT_METHOD_SEPARATER);
2811 } else {
2812 isImeAdded = true;
2813 }
2814 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2815 subtypeId);
2816 }
2817 // Remove the last INPUT_METHOD_SEPARATER
2818 putSubtypeHistoryStr(builder.toString());
2819 }
2820
2821 public void addSubtypeToHistory(String imeId, String subtypeId) {
2822 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2823 for (Pair<String, String> ime: subtypeHistory) {
2824 if (ime.first.equals(imeId)) {
2825 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002826 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002827 + ime.second);
2828 }
2829 // We should break here
2830 subtypeHistory.remove(ime);
2831 break;
2832 }
2833 }
satokbb4aa062011-01-19 21:40:27 +09002834 if (DEBUG) {
2835 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2836 }
satok723a27e2010-11-11 14:58:11 +09002837 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2838 }
2839
2840 private void putSubtypeHistoryStr(String str) {
2841 if (DEBUG) {
2842 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2843 }
2844 Settings.Secure.putString(
2845 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2846 }
2847
2848 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2849 // Gets the first one from the history
2850 return getLastSubtypeForInputMethodLockedInternal(null);
2851 }
2852
2853 public String getLastSubtypeForInputMethodLocked(String imeId) {
2854 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2855 if (ime != null) {
2856 return ime.second;
2857 } else {
2858 return null;
2859 }
2860 }
2861
2862 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2863 List<Pair<String, ArrayList<String>>> enabledImes =
2864 getEnabledInputMethodsAndSubtypeListLocked();
2865 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002866 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002867 final String imeInTheHistory = imeAndSubtype.first;
2868 // If imeId is empty, returns the first IME and subtype in the history
2869 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2870 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002871 final String subtypeHashCode =
2872 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2873 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002874 if (!TextUtils.isEmpty(subtypeHashCode)) {
2875 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002876 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002877 }
2878 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2879 }
2880 }
2881 }
2882 if (DEBUG) {
2883 Slog.d(TAG, "No enabled IME found in the history");
2884 }
2885 return null;
2886 }
2887
satokdf31ae62011-01-15 06:19:44 +09002888 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002889 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2890 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2891 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002892 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2893 if (explicitlyEnabledSubtypes.size() == 0) {
2894 // If there are no explicitly enabled subtypes, applicable subtypes are
2895 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002896 InputMethodInfo ime = mMethodMap.get(imeId);
2897 // If IME is enabled and no subtypes are enabled, applicable subtypes
2898 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002899 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002900 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002901 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002902 if (implicitlySelectedSubtypes != null) {
2903 final int N = implicitlySelectedSubtypes.size();
2904 for (int i = 0; i < N; ++i) {
2905 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2906 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2907 return subtypeHashCode;
2908 }
2909 }
2910 }
2911 }
2912 } else {
satokf6cafb62011-01-17 16:29:02 +09002913 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002914 if (s.equals(subtypeHashCode)) {
2915 // If both imeId and subtypeId are enabled, return subtypeId.
2916 return s;
2917 }
satok723a27e2010-11-11 14:58:11 +09002918 }
2919 }
2920 // If imeId was enabled but subtypeId was disabled.
2921 return NOT_A_SUBTYPE_ID_STR;
2922 }
2923 }
2924 // If both imeId and subtypeId are disabled, return null
2925 return null;
2926 }
2927
2928 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2929 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2930 final String subtypeHistoryStr = getSubtypeHistoryStr();
2931 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2932 return imsList;
2933 }
2934 mInputMethodSplitter.setString(subtypeHistoryStr);
2935 while (mInputMethodSplitter.hasNext()) {
2936 String nextImsStr = mInputMethodSplitter.next();
2937 mSubtypeSplitter.setString(nextImsStr);
2938 if (mSubtypeSplitter.hasNext()) {
2939 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2940 // The first element is ime id.
2941 String imeId = mSubtypeSplitter.next();
2942 while (mSubtypeSplitter.hasNext()) {
2943 subtypeId = mSubtypeSplitter.next();
2944 break;
2945 }
2946 imsList.add(new Pair<String, String>(imeId, subtypeId));
2947 }
2948 }
2949 return imsList;
2950 }
2951
2952 private String getSubtypeHistoryStr() {
2953 if (DEBUG) {
2954 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2955 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2956 }
2957 return Settings.Secure.getString(
2958 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2959 }
2960
2961 public void putSelectedInputMethod(String imeId) {
2962 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2963 }
2964
2965 public void putSelectedSubtype(int subtypeId) {
2966 Settings.Secure.putInt(
2967 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2968 }
satokd87c2592010-09-29 11:52:06 +09002969 }
2970
satoke7c6998e2011-06-03 17:57:59 +09002971 private static class InputMethodFileManager {
2972 private static final String SYSTEM_PATH = "system";
2973 private static final String INPUT_METHOD_PATH = "inputmethod";
2974 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2975 private static final String NODE_SUBTYPES = "subtypes";
2976 private static final String NODE_SUBTYPE = "subtype";
2977 private static final String NODE_IMI = "imi";
2978 private static final String ATTR_ID = "id";
2979 private static final String ATTR_LABEL = "label";
2980 private static final String ATTR_ICON = "icon";
2981 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2982 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2983 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2984 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2985 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2986 private final HashMap<String, InputMethodInfo> mMethodMap;
2987 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2988 new HashMap<String, List<InputMethodSubtype>>();
2989 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2990 if (methodMap == null) {
2991 throw new NullPointerException("methodMap is null");
2992 }
2993 mMethodMap = methodMap;
2994 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
2995 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
2996 if (!inputMethodDir.mkdirs()) {
2997 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
2998 }
2999 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3000 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3001 if (!subtypeFile.exists()) {
3002 // If "subtypes.xml" doesn't exist, create a blank file.
3003 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3004 methodMap);
3005 } else {
3006 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3007 }
3008 }
3009
3010 private void deleteAllInputMethodSubtypes(String imiId) {
3011 synchronized (mMethodMap) {
3012 mSubtypesMap.remove(imiId);
3013 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3014 mMethodMap);
3015 }
3016 }
3017
3018 public void addInputMethodSubtypes(
3019 String imiId, InputMethodSubtype[] additionalSubtypes) {
3020 synchronized (mMethodMap) {
3021 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3022 final int N = additionalSubtypes.length;
3023 for (int i = 0; i < N; ++i) {
3024 final InputMethodSubtype subtype = additionalSubtypes[i];
3025 if (!subtypes.contains(subtype)) {
3026 subtypes.add(subtype);
3027 }
3028 }
3029 mSubtypesMap.put(imiId, subtypes);
3030 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3031 mMethodMap);
3032 }
3033 }
3034
3035 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3036 synchronized (mMethodMap) {
3037 return mSubtypesMap;
3038 }
3039 }
3040
3041 private static void writeAdditionalInputMethodSubtypes(
3042 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3043 HashMap<String, InputMethodInfo> methodMap) {
3044 // Safety net for the case that this function is called before methodMap is set.
3045 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3046 FileOutputStream fos = null;
3047 try {
3048 fos = subtypesFile.startWrite();
3049 final XmlSerializer out = new FastXmlSerializer();
3050 out.setOutput(fos, "utf-8");
3051 out.startDocument(null, true);
3052 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3053 out.startTag(null, NODE_SUBTYPES);
3054 for (String imiId : allSubtypes.keySet()) {
3055 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3056 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3057 continue;
3058 }
3059 out.startTag(null, NODE_IMI);
3060 out.attribute(null, ATTR_ID, imiId);
3061 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3062 final int N = subtypesList.size();
3063 for (int i = 0; i < N; ++i) {
3064 final InputMethodSubtype subtype = subtypesList.get(i);
3065 out.startTag(null, NODE_SUBTYPE);
3066 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3067 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3068 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3069 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3070 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3071 out.attribute(null, ATTR_IS_AUXILIARY,
3072 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3073 out.endTag(null, NODE_SUBTYPE);
3074 }
3075 out.endTag(null, NODE_IMI);
3076 }
3077 out.endTag(null, NODE_SUBTYPES);
3078 out.endDocument();
3079 subtypesFile.finishWrite(fos);
3080 } catch (java.io.IOException e) {
3081 Slog.w(TAG, "Error writing subtypes", e);
3082 if (fos != null) {
3083 subtypesFile.failWrite(fos);
3084 }
3085 }
3086 }
3087
3088 private static void readAdditionalInputMethodSubtypes(
3089 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3090 if (allSubtypes == null || subtypesFile == null) return;
3091 allSubtypes.clear();
3092 FileInputStream fis = null;
3093 try {
3094 fis = subtypesFile.openRead();
3095 final XmlPullParser parser = Xml.newPullParser();
3096 parser.setInput(fis, null);
3097 int type = parser.getEventType();
3098 // Skip parsing until START_TAG
3099 while ((type = parser.next()) != XmlPullParser.START_TAG
3100 && type != XmlPullParser.END_DOCUMENT) {}
3101 String firstNodeName = parser.getName();
3102 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3103 throw new XmlPullParserException("Xml doesn't start with subtypes");
3104 }
3105 final int depth =parser.getDepth();
3106 String currentImiId = null;
3107 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3108 while (((type = parser.next()) != XmlPullParser.END_TAG
3109 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3110 if (type != XmlPullParser.START_TAG)
3111 continue;
3112 final String nodeName = parser.getName();
3113 if (NODE_IMI.equals(nodeName)) {
3114 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3115 if (TextUtils.isEmpty(currentImiId)) {
3116 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3117 continue;
3118 }
3119 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3120 allSubtypes.put(currentImiId, tempSubtypesArray);
3121 } else if (NODE_SUBTYPE.equals(nodeName)) {
3122 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3123 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3124 continue;
3125 }
3126 final int icon = Integer.valueOf(
3127 parser.getAttributeValue(null, ATTR_ICON));
3128 final int label = Integer.valueOf(
3129 parser.getAttributeValue(null, ATTR_LABEL));
3130 final String imeSubtypeLocale =
3131 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3132 final String imeSubtypeMode =
3133 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3134 final String imeSubtypeExtraValue =
3135 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
3136 final boolean isAuxiliary =
3137 Boolean.valueOf(parser.getAttributeValue(null, ATTR_IS_AUXILIARY));
3138 final InputMethodSubtype subtype =
3139 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3140 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3141 tempSubtypesArray.add(subtype);
3142 }
3143 }
3144 } catch (XmlPullParserException e) {
3145 Slog.w(TAG, "Error reading subtypes: " + e);
3146 return;
3147 } catch (java.io.IOException e) {
3148 Slog.w(TAG, "Error reading subtypes: " + e);
3149 return;
3150 } catch (NumberFormatException e) {
3151 Slog.w(TAG, "Error reading subtypes: " + e);
3152 return;
3153 } finally {
3154 if (fis != null) {
3155 try {
3156 fis.close();
3157 } catch (java.io.IOException e1) {
3158 Slog.w(TAG, "Failed to close.");
3159 }
3160 }
3161 }
3162 }
3163 }
3164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 @Override
3168 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3169 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3170 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3173 + Binder.getCallingPid()
3174 + ", uid=" + Binder.getCallingUid());
3175 return;
3176 }
3177
3178 IInputMethod method;
3179 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 synchronized (mMethodMap) {
3184 p.println("Current Input Method Manager state:");
3185 int N = mMethodList.size();
3186 p.println(" Input Methods:");
3187 for (int i=0; i<N; i++) {
3188 InputMethodInfo info = mMethodList.get(i);
3189 p.println(" InputMethod #" + i + ":");
3190 info.dump(p, " ");
3191 }
3192 p.println(" Clients:");
3193 for (ClientState ci : mClients.values()) {
3194 p.println(" Client " + ci + ":");
3195 p.println(" client=" + ci.client);
3196 p.println(" inputContext=" + ci.inputContext);
3197 p.println(" sessionRequested=" + ci.sessionRequested);
3198 p.println(" curSession=" + ci.curSession);
3199 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003200 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003202 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3203 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3205 + " mBoundToMethod=" + mBoundToMethod);
3206 p.println(" mCurToken=" + mCurToken);
3207 p.println(" mCurIntent=" + mCurIntent);
3208 method = mCurMethod;
3209 p.println(" mCurMethod=" + mCurMethod);
3210 p.println(" mEnabledSession=" + mEnabledSession);
3211 p.println(" mShowRequested=" + mShowRequested
3212 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3213 + " mShowForced=" + mShowForced
3214 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003215 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003217
Jeff Brownb88102f2010-09-08 11:49:43 -07003218 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 pw.flush();
3221 try {
3222 client.client.asBinder().dump(fd, args);
3223 } catch (RemoteException e) {
3224 p.println("Input method client dead: " + e);
3225 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003226 } else {
3227 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003229
Jeff Brownb88102f2010-09-08 11:49:43 -07003230 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 pw.flush();
3233 try {
3234 method.asBinder().dump(fd, args);
3235 } catch (RemoteException e) {
3236 p.println("Input method service dead: " + e);
3237 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003238 } else {
3239 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
3241 }
3242}