blob: 18d393fde2f761aa6e2ca4b7f4a80374576b2e07 [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);
satok683e2382011-07-12 08:28:52 +0900534 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satok7cfc0ed2011-06-20 21:29:36 +0900535 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
536 com.android.internal.R.bool.show_ongoing_ime_switcher);
537
satoke7c6998e2011-06-03 17:57:59 +0900538 synchronized (mMethodMap) {
539 mFileManager = new InputMethodFileManager(mMethodMap);
540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 IntentFilter screenOnOffFilt = new IntentFilter();
545 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
546 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700547 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
satok913a8922010-08-26 21:53:41 +0900550 mStatusBar = statusBar;
551 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900552 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900553
satokd87c2592010-09-29 11:52:06 +0900554 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900555 mSettings = new InputMethodSettings(
556 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900558 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
satokd87c2592010-09-29 11:52:06 +0900560 if (TextUtils.isEmpty(Settings.Secure.getString(
561 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900563 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
565 try {
satokd87c2592010-09-29 11:52:06 +0900566 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 imi.getPackageName(), 0).getResources();
568 if (res.getBoolean(imi.getIsDefaultResourceId())) {
569 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800570 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572 } catch (PackageManager.NameNotFoundException ex) {
573 } catch (Resources.NotFoundException ex) {
574 }
575 }
576 }
satokd87c2592010-09-29 11:52:06 +0900577 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800579 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900582 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mSettingsObserver = new SettingsObserver(mHandler);
587 updateFromSettingsLocked();
588 }
589
590 @Override
591 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
592 throws RemoteException {
593 try {
594 return super.onTransact(code, data, reply, flags);
595 } catch (RuntimeException e) {
596 // The input method manager only throws security exceptions, so let's
597 // log all others.
598 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800599 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601 throw e;
602 }
603 }
604
605 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700606 synchronized (mMethodMap) {
607 if (!mSystemReady) {
608 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700609 try {
610 startInputInnerLocked();
611 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800612 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700613 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700614 }
615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800617
satoke7c6998e2011-06-03 17:57:59 +0900618 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 public List<InputMethodInfo> getInputMethodList() {
620 synchronized (mMethodMap) {
621 return new ArrayList<InputMethodInfo>(mMethodList);
622 }
623 }
624
satoke7c6998e2011-06-03 17:57:59 +0900625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 public List<InputMethodInfo> getEnabledInputMethodList() {
627 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900628 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 }
630 }
631
satokbb4aa062011-01-19 21:40:27 +0900632 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
633 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
634 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
635 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
636 for (InputMethodInfo imi: getEnabledInputMethodList()) {
637 enabledInputMethodAndSubtypes.put(
638 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
639 }
640 return enabledInputMethodAndSubtypes;
641 }
642
643 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
644 boolean allowsImplicitlySelectedSubtypes) {
645 if (imi == null && mCurMethodId != null) {
646 imi = mMethodMap.get(mCurMethodId);
647 }
satok7265d9b2011-02-14 15:47:30 +0900648 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900649 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900650 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
651 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900652 }
satok7265d9b2011-02-14 15:47:30 +0900653 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900654 }
655
satoke7c6998e2011-06-03 17:57:59 +0900656 @Override
satok16331c82010-12-20 23:48:46 +0900657 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
658 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900659 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900660 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900661 }
662 }
663
satoke7c6998e2011-06-03 17:57:59 +0900664 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 public void addClient(IInputMethodClient client,
666 IInputContext inputContext, int uid, int pid) {
667 synchronized (mMethodMap) {
668 mClients.put(client.asBinder(), new ClientState(client,
669 inputContext, uid, pid));
670 }
671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
satoke7c6998e2011-06-03 17:57:59 +0900673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 public void removeClient(IInputMethodClient client) {
675 synchronized (mMethodMap) {
676 mClients.remove(client.asBinder());
677 }
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 void executeOrSendMessage(IInterface target, Message msg) {
681 if (target.asBinder() instanceof Binder) {
682 mCaller.sendMessage(msg);
683 } else {
684 handleMessage(msg);
685 msg.recycle();
686 }
687 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800688
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700689 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 + mCurClient.client.asBinder());
693 if (mBoundToMethod) {
694 mBoundToMethod = false;
695 if (mCurMethod != null) {
696 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
697 MSG_UNBIND_INPUT, mCurMethod));
698 }
699 }
700 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
701 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
702 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 // Call setActive(false) on the old client
705 try {
706 mCurClient.client.setActive(false);
707 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800708 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 + mCurClient.pid + " uid " + mCurClient.uid);
710 }
711 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
The Android Open Source Project10592532009-03-18 17:39:46 -0700713 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 private int getImeShowFlags() {
718 int flags = 0;
719 if (mShowForced) {
720 flags |= InputMethod.SHOW_FORCED
721 | InputMethod.SHOW_EXPLICIT;
722 } else if (mShowExplicitlyRequested) {
723 flags |= InputMethod.SHOW_EXPLICIT;
724 }
725 return flags;
726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 private int getAppShowFlags() {
729 int flags = 0;
730 if (mShowForced) {
731 flags |= InputMethodManager.SHOW_FORCED;
732 } else if (!mShowExplicitlyRequested) {
733 flags |= InputMethodManager.SHOW_IMPLICIT;
734 }
735 return flags;
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
739 if (!mBoundToMethod) {
740 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
741 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
742 mBoundToMethod = true;
743 }
744 final SessionState session = mCurClient.curSession;
745 if (initial) {
746 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
747 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
748 } else {
749 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
750 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
751 }
752 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800753 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800754 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 return needResult
757 ? new InputBindResult(session.session, mCurId, mCurSeq)
758 : null;
759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 InputBindResult startInputLocked(IInputMethodClient client,
762 IInputContext inputContext, EditorInfo attribute,
763 boolean initial, boolean needResult) {
764 // If no method is currently selected, do nothing.
765 if (mCurMethodId == null) {
766 return mNoBinding;
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 ClientState cs = mClients.get(client.asBinder());
770 if (cs == null) {
771 throw new IllegalArgumentException("unknown client "
772 + client.asBinder());
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 try {
776 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
777 // Check with the window manager to make sure this client actually
778 // has a window with focus. If not, reject. This is thread safe
779 // because if the focus changes some time before or after, the
780 // next client receiving focus that has any interest in input will
781 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800782 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
784 return null;
785 }
786 } catch (RemoteException e) {
787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 if (mCurClient != cs) {
790 // If the client is changing, we need to switch over to the new
791 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700792 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800793 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 + cs.client.asBinder());
795
796 // If the screen is on, inform the new client it is active
797 if (mScreenOn) {
798 try {
799 cs.client.setActive(mScreenOn);
800 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + cs.pid + " uid " + cs.uid);
803 }
804 }
805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 // Bump up the sequence for this client and attach it.
808 mCurSeq++;
809 if (mCurSeq <= 0) mCurSeq = 1;
810 mCurClient = cs;
811 mCurInputContext = inputContext;
812 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // Check if the input method is changing.
815 if (mCurId != null && mCurId.equals(mCurMethodId)) {
816 if (cs.curSession != null) {
817 // Fast case: if we are already connected to the input method,
818 // then just return it.
819 return attachNewInputLocked(initial, needResult);
820 }
821 if (mHaveConnection) {
822 if (mCurMethod != null) {
823 if (!cs.sessionRequested) {
824 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800825 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
827 MSG_CREATE_SESSION, mCurMethod,
828 new MethodCallback(mCurMethod)));
829 }
830 // Return to client, and we will get back with it when
831 // we have had a session made for it.
832 return new InputBindResult(null, mCurId, mCurSeq);
833 } else if (SystemClock.uptimeMillis()
834 < (mLastBindTime+TIME_TO_RECONNECT)) {
835 // In this case we have connected to the service, but
836 // don't yet have its interface. If it hasn't been too
837 // long since we did the connection, we'll return to
838 // the client and wait to get the service interface so
839 // we can report back. If it has been too long, we want
840 // to fall through so we can try a disconnect/reconnect
841 // to see if we can get back in touch with the service.
842 return new InputBindResult(null, mCurId, mCurSeq);
843 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800844 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
845 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
847 }
848 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800849
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700850 return startInputInnerLocked();
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700853 InputBindResult startInputInnerLocked() {
854 if (mCurMethodId == null) {
855 return mNoBinding;
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 if (!mSystemReady) {
859 // If the system is not yet ready, we shouldn't be running third
860 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700861 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 InputMethodInfo info = mMethodMap.get(mCurMethodId);
865 if (info == null) {
866 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700869 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
872 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700873 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
874 com.android.internal.R.string.input_method_binding_label);
875 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
876 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
878 mLastBindTime = SystemClock.uptimeMillis();
879 mHaveConnection = true;
880 mCurId = info.getId();
881 mCurToken = new Binder();
882 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800883 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 mIWindowManager.addWindowToken(mCurToken,
885 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
886 } catch (RemoteException e) {
887 }
888 return new InputBindResult(null, mCurId, mCurSeq);
889 } else {
890 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800891 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 + mCurIntent);
893 }
894 return null;
895 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800896
satoke7c6998e2011-06-03 17:57:59 +0900897 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 public InputBindResult startInput(IInputMethodClient client,
899 IInputContext inputContext, EditorInfo attribute,
900 boolean initial, boolean needResult) {
901 synchronized (mMethodMap) {
902 final long ident = Binder.clearCallingIdentity();
903 try {
904 return startInputLocked(client, inputContext, attribute,
905 initial, needResult);
906 } finally {
907 Binder.restoreCallingIdentity(ident);
908 }
909 }
910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800911
satoke7c6998e2011-06-03 17:57:59 +0900912 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 public void finishInput(IInputMethodClient client) {
914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
satoke7c6998e2011-06-03 17:57:59 +0900916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void onServiceConnected(ComponentName name, IBinder service) {
918 synchronized (mMethodMap) {
919 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
920 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700921 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800922 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700923 unbindCurrentMethodLocked(false);
924 return;
925 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800926 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700927 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
928 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700931 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700933 MSG_CREATE_SESSION, mCurMethod,
934 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 }
937 }
938 }
939
940 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
941 synchronized (mMethodMap) {
942 if (mCurMethod != null && method != null
943 && mCurMethod.asBinder() == method.asBinder()) {
944 if (mCurClient != null) {
945 mCurClient.curSession = new SessionState(mCurClient,
946 method, session);
947 mCurClient.sessionRequested = false;
948 InputBindResult res = attachNewInputLocked(true, true);
949 if (res.method != null) {
950 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
951 MSG_BIND_METHOD, mCurClient.client, res));
952 }
953 }
954 }
955 }
956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800957
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700958 void unbindCurrentMethodLocked(boolean reportToClient) {
959 if (mHaveConnection) {
960 mContext.unbindService(this);
961 mHaveConnection = false;
962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700964 if (mCurToken != null) {
965 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800966 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 mIWindowManager.removeWindowToken(mCurToken);
968 } catch (RemoteException e) {
969 }
970 mCurToken = null;
971 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800972
The Android Open Source Project10592532009-03-18 17:39:46 -0700973 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700974 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 if (reportToClient && mCurClient != null) {
977 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
978 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
979 }
980 }
981
Devin Taylor0c33ed22010-02-23 13:26:46 -0600982 private void finishSession(SessionState sessionState) {
983 if (sessionState != null && sessionState.session != null) {
984 try {
985 sessionState.session.finishSession();
986 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700987 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600988 }
989 }
990 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800991
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700992 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 if (mCurMethod != null) {
994 for (ClientState cs : mClients.values()) {
995 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600996 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 cs.curSession = null;
998 }
Devin Taylor0c33ed22010-02-23 13:26:46 -0600999
1000 finishSession(mEnabledSession);
1001 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 mCurMethod = null;
1003 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001004 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001006
satoke7c6998e2011-06-03 17:57:59 +09001007 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 public void onServiceDisconnected(ComponentName name) {
1009 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001010 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 + " mCurIntent=" + mCurIntent);
1012 if (mCurMethod != null && mCurIntent != null
1013 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001014 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 // We consider this to be a new bind attempt, since the system
1016 // should now try to restart the service for us.
1017 mLastBindTime = SystemClock.uptimeMillis();
1018 mShowRequested = mInputShown;
1019 mInputShown = false;
1020 if (mCurClient != null) {
1021 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1022 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1023 }
1024 }
1025 }
1026 }
1027
satokf9f01002011-05-19 21:31:50 +09001028 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001030 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 long ident = Binder.clearCallingIdentity();
1032 try {
1033 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001034 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 return;
1036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 synchronized (mMethodMap) {
1039 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001040 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001041 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001043 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001044 mStatusBar.setIcon("ime", packageName, iconId, 0);
1045 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 }
1048 } finally {
1049 Binder.restoreCallingIdentity(ident);
1050 }
1051 }
1052
satok7cfc0ed2011-06-20 21:29:36 +09001053 private boolean needsToShowImeSwitchOngoingNotification() {
1054 if (!mShowOngoingImeSwitcherForPhones) return false;
1055 synchronized (mMethodMap) {
1056 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1057 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001058 if (N > 2) return true;
1059 if (N < 1) return false;
1060 int nonAuxCount = 0;
1061 int auxCount = 0;
1062 InputMethodSubtype nonAuxSubtype = null;
1063 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001064 for(int i = 0; i < N; ++i) {
1065 final InputMethodInfo imi = imis.get(i);
1066 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1067 imi, true);
1068 final int subtypeCount = subtypes.size();
1069 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001070 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001071 } else {
1072 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001073 final InputMethodSubtype subtype = subtypes.get(j);
1074 if (!subtype.isAuxiliary()) {
1075 ++nonAuxCount;
1076 nonAuxSubtype = subtype;
1077 } else {
1078 ++auxCount;
1079 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001080 }
1081 }
1082 }
satok7cfc0ed2011-06-20 21:29:36 +09001083 }
satokb6359412011-06-28 17:47:41 +09001084 if (nonAuxCount > 1 || auxCount > 1) {
1085 return true;
1086 } else if (nonAuxCount == 1 && auxCount == 1) {
1087 if (nonAuxSubtype != null && auxSubtype != null
1088 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1089 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1090 return false;
1091 }
1092 return true;
1093 }
1094 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001095 }
satok7cfc0ed2011-06-20 21:29:36 +09001096 }
1097
satokf9f01002011-05-19 21:31:50 +09001098 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001099 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001100 int uid = Binder.getCallingUid();
1101 long ident = Binder.clearCallingIdentity();
1102 try {
1103 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001104 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001105 return;
1106 }
1107
1108 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001109 mImeWindowVis = vis;
1110 mBackDisposition = backDisposition;
1111 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001112 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1113 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1114 final PackageManager pm = mContext.getPackageManager();
1115 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1116 final CharSequence title = mRes.getText(
1117 com.android.internal.R.string.select_input_method);
1118 mImeSwitcherNotification.setLatestEventInfo(
1119 mContext, title, label, mImeSwitchPendingIntent);
1120 mNotificationManager.notify(
1121 com.android.internal.R.string.select_input_method,
1122 mImeSwitcherNotification);
1123 mNotificationShown = true;
1124 } else {
1125 if (mNotificationShown) {
1126 mNotificationManager.cancel(
1127 com.android.internal.R.string.select_input_method);
1128 mNotificationShown = false;
1129 }
1130 }
satok06487a52010-10-29 11:37:18 +09001131 }
1132 } finally {
1133 Binder.restoreCallingIdentity(ident);
1134 }
1135 }
1136
satoke7c6998e2011-06-03 17:57:59 +09001137 @Override
satokf9f01002011-05-19 21:31:50 +09001138 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1139 synchronized (mMethodMap) {
1140 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1141 for (int i = 0; i < spans.length; ++i) {
1142 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001143 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001144 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001145 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001146 }
1147 }
1148 }
1149 }
1150
satoke7c6998e2011-06-03 17:57:59 +09001151 @Override
satokf9f01002011-05-19 21:31:50 +09001152 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1153 synchronized (mMethodMap) {
1154 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1155 // TODO: Do not send the intent if the process of the targetImi is already dead.
1156 if (targetImi != null) {
1157 final String[] suggestions = span.getSuggestions();
1158 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001159 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001160 final Intent intent = new Intent();
1161 // Ensures that only a class in the original IME package will receive the
1162 // notification.
satok42c5a162011-05-26 16:46:14 +09001163 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001164 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1165 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1166 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1167 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1168 mContext.sendBroadcast(intent);
1169 return true;
1170 }
1171 }
1172 return false;
1173 }
1174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001176 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1177 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1178 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1179 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001181 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001182 // There is no input method selected, try to choose new applicable input method.
1183 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1184 id = Settings.Secure.getString(mContext.getContentResolver(),
1185 Settings.Secure.DEFAULT_INPUT_METHOD);
1186 }
1187 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 try {
satokab751aa2010-09-14 19:17:36 +09001189 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001191 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001192 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001193 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
satokf3db1af2010-11-23 13:34:33 +09001195 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001196 } else {
1197 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001198 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001199 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
satokab751aa2010-09-14 19:17:36 +09001203 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 InputMethodInfo info = mMethodMap.get(id);
1205 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001206 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001210 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001211 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1212 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001213 }
1214 if (subtype != mCurrentSubtype) {
1215 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001216 if (subtype != null) {
1217 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1218 }
satokcd7cd292010-11-20 15:46:23 +09001219 if (mCurMethod != null) {
1220 try {
satoke40dea02011-01-30 01:14:02 +09001221 final Configuration conf = mRes.getConfiguration();
1222 final boolean haveHardKeyboard = conf.keyboard
1223 != Configuration.KEYBOARD_NOKEYS;
1224 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001225 && conf.hardKeyboardHidden
1226 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001227 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1228 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1229 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001230 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1231 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001232 // If subtype is null, try to find the most applicable one from
1233 // getCurrentInputMethodSubtype.
1234 if (subtype == null) {
1235 subtype = getCurrentInputMethodSubtype();
1236 }
1237 mCurMethod.changeInputMethodSubtype(subtype);
1238 } catch (RemoteException e) {
1239 return;
satokab751aa2010-09-14 19:17:36 +09001240 }
1241 }
1242 }
1243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 return;
1245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 final long ident = Binder.clearCallingIdentity();
1248 try {
satokab751aa2010-09-14 19:17:36 +09001249 // Set a subtype to this input method.
1250 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001251 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1252 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1253 // because mCurMethodId is stored as a history in
1254 // setSelectedInputMethodAndSubtypeLocked().
1255 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256
1257 if (ActivityManagerNative.isSystemReady()) {
1258 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001259 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 intent.putExtra("input_method_id", id);
1261 mContext.sendBroadcast(intent);
1262 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001263 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 } finally {
1265 Binder.restoreCallingIdentity(ident);
1266 }
1267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001268
satok42c5a162011-05-26 16:46:14 +09001269 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001270 public boolean showSoftInput(IInputMethodClient client, int flags,
1271 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001272 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 long ident = Binder.clearCallingIdentity();
1274 try {
1275 synchronized (mMethodMap) {
1276 if (mCurClient == null || client == null
1277 || mCurClient.client.asBinder() != client.asBinder()) {
1278 try {
1279 // We need to check if this is the current client with
1280 // focus in the window manager, to allow this call to
1281 // be made before input is started in it.
1282 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001283 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001284 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 } catch (RemoteException e) {
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 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
Joe Onorato8a9b2202010-02-26 18:56:32 -08001291 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001292 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294 } finally {
1295 Binder.restoreCallingIdentity(ident);
1296 }
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
The Android Open Source Project4df24232009-03-05 14:34:35 -08001299 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 mShowRequested = true;
1301 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1302 mShowExplicitlyRequested = true;
1303 }
1304 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1305 mShowExplicitlyRequested = true;
1306 mShowForced = true;
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
Dianne Hackborncc278702009-09-02 23:07:23 -07001309 if (!mSystemReady) {
1310 return false;
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
The Android Open Source Project4df24232009-03-05 14:34:35 -08001313 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001315 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1316 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1317 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 } else if (mHaveConnection && SystemClock.uptimeMillis()
1321 < (mLastBindTime+TIME_TO_RECONNECT)) {
1322 // The client has asked to have the input method shown, but
1323 // we have been sitting here too long with a connection to the
1324 // service and no interface received, so let's disconnect/connect
1325 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 SystemClock.uptimeMillis()-mLastBindTime,1);
1328 mContext.unbindService(this);
1329 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project4df24232009-03-05 14:34:35 -08001332 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
satok42c5a162011-05-26 16:46:14 +09001335 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 public boolean hideSoftInput(IInputMethodClient client, int flags,
1337 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001338 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 long ident = Binder.clearCallingIdentity();
1340 try {
1341 synchronized (mMethodMap) {
1342 if (mCurClient == null || client == null
1343 || mCurClient.client.asBinder() != client.asBinder()) {
1344 try {
1345 // We need to check if this is the current client with
1346 // focus in the window manager, to allow this call to
1347 // be made before input is started in it.
1348 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001349 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1350 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001351 mImeWindowVis = 0;
1352 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1353 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001354 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
1356 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001357 mImeWindowVis = 0;
1358 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
Joe Onorato8a9b2202010-02-26 18:56:32 -08001363 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001364 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 } finally {
1367 Binder.restoreCallingIdentity(ident);
1368 }
1369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370
The Android Open Source Project4df24232009-03-05 14:34:35 -08001371 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1373 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001374 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001376 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
1378 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001381 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001383 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001385 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1386 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1387 res = true;
1388 } else {
1389 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 }
1391 mInputShown = false;
1392 mShowRequested = false;
1393 mShowExplicitlyRequested = false;
1394 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001395 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
satok42c5a162011-05-26 16:46:14 +09001398 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001399 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1401 boolean first, int windowFlags) {
1402 long ident = Binder.clearCallingIdentity();
1403 try {
1404 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 + " viewHasFocus=" + viewHasFocus
1407 + " isTextEditor=" + isTextEditor
1408 + " softInputMode=#" + Integer.toHexString(softInputMode)
1409 + " first=" + first + " flags=#"
1410 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 if (mCurClient == null || client == null
1413 || mCurClient.client.asBinder() != client.asBinder()) {
1414 try {
1415 // We need to check if this is the current client with
1416 // focus in the window manager, to allow this call to
1417 // be made before input is started in it.
1418 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 return;
1421 }
1422 } catch (RemoteException e) {
1423 }
1424 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001425
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001426 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001427 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001428 return;
1429 }
1430 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001432 // Should we auto-show the IME even if the caller has not
1433 // specified what should be done with it?
1434 // We only do this automatically if the window can resize
1435 // to accommodate the IME (so what the user sees will give
1436 // them good context without input information being obscured
1437 // by the IME) or if running on a large screen where there
1438 // is more room for the target window + IME.
1439 final boolean doAutoShow =
1440 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1441 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1442 || mRes.getConfiguration().isLayoutSizeAtLeast(
1443 Configuration.SCREENLAYOUT_SIZE_LARGE);
1444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1446 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001447 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1449 // There is no focus view, and this window will
1450 // be behind any soft input window, so hide the
1451 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001453 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001455 } else if (isTextEditor && doAutoShow && (softInputMode &
1456 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 // There is a focus view, and we are navigating forward
1458 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001459 // We only do this automatically if the window an resize
1460 // to accomodate the IME (so what the user sees will give
1461 // them good context without input information being obscured
1462 // by the IME) or if running on a large screen where there
1463 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001464 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001465 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467 break;
1468 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1469 // Do nothing.
1470 break;
1471 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1472 if ((softInputMode &
1473 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001474 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001475 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 }
1477 break;
1478 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001479 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001480 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 break;
1482 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1483 if ((softInputMode &
1484 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001485 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001486 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 }
1488 break;
1489 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001491 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 break;
1493 }
1494 }
1495 } finally {
1496 Binder.restoreCallingIdentity(ident);
1497 }
1498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001499
satok42c5a162011-05-26 16:46:14 +09001500 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1502 synchronized (mMethodMap) {
1503 if (mCurClient == null || client == null
1504 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001505 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001506 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
1508
satok440aab52010-11-25 09:43:11 +09001509 // Always call subtype picker, because subtype picker is a superset of input method
1510 // picker.
satokab751aa2010-09-14 19:17:36 +09001511 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1512 }
1513 }
1514
satok42c5a162011-05-26 16:46:14 +09001515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001517 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1518 }
1519
satok42c5a162011-05-26 16:46:14 +09001520 @Override
satok28203512010-11-24 11:06:49 +09001521 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1522 synchronized (mMethodMap) {
1523 if (subtype != null) {
1524 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1525 mMethodMap.get(id), subtype.hashCode()));
1526 } else {
1527 setInputMethod(token, id);
1528 }
1529 }
satokab751aa2010-09-14 19:17:36 +09001530 }
1531
satok42c5a162011-05-26 16:46:14 +09001532 @Override
satokb416a712010-11-25 20:42:14 +09001533 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001534 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001535 synchronized (mMethodMap) {
1536 if (mCurClient == null || client == null
1537 || mCurClient.client.asBinder() != client.asBinder()) {
1538 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1539 }
satok7fee71f2010-12-17 18:54:26 +09001540 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1541 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001542 }
1543 }
1544
satok4fc87d62011-05-20 16:13:43 +09001545 @Override
satok735cf382010-11-11 20:40:09 +09001546 public boolean switchToLastInputMethod(IBinder token) {
1547 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001548 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001549 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001550 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001551 lastImi = mMethodMap.get(lastIme.first);
1552 } else {
1553 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001554 }
satok4fc87d62011-05-20 16:13:43 +09001555 String targetLastImiId = null;
1556 int subtypeId = NOT_A_SUBTYPE_ID;
1557 if (lastIme != null && lastImi != null) {
1558 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1559 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1560 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1561 : mCurrentSubtype.hashCode();
1562 // If the last IME is the same as the current IME and the last subtype is not
1563 // defined, there is no need to switch to the last IME.
1564 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1565 targetLastImiId = lastIme.first;
1566 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1567 }
1568 }
1569
1570 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1571 // This is a safety net. If the currentSubtype can't be added to the history
1572 // and the framework couldn't find the last ime, we will make the last ime be
1573 // the most applicable enabled keyboard subtype of the system imes.
1574 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1575 if (enabled != null) {
1576 final int N = enabled.size();
1577 final String locale = mCurrentSubtype == null
1578 ? mRes.getConfiguration().locale.toString()
1579 : mCurrentSubtype.getLocale();
1580 for (int i = 0; i < N; ++i) {
1581 final InputMethodInfo imi = enabled.get(i);
1582 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1583 InputMethodSubtype keyboardSubtype =
1584 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1585 SUBTYPE_MODE_KEYBOARD, locale, true);
1586 if (keyboardSubtype != null) {
1587 targetLastImiId = imi.getId();
1588 subtypeId = getSubtypeIdFromHashCode(
1589 imi, keyboardSubtype.hashCode());
1590 if(keyboardSubtype.getLocale().equals(locale)) {
1591 break;
1592 }
1593 }
1594 }
1595 }
1596 }
1597 }
1598
1599 if (!TextUtils.isEmpty(targetLastImiId)) {
1600 if (DEBUG) {
1601 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1602 + ", from: " + mCurMethodId + ", " + subtypeId);
1603 }
1604 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1605 return true;
1606 } else {
1607 return false;
1608 }
satok735cf382010-11-11 20:40:09 +09001609 }
1610 }
1611
satoke7c6998e2011-06-03 17:57:59 +09001612 @Override
satok68f1b782011-04-11 14:26:04 +09001613 public InputMethodSubtype getLastInputMethodSubtype() {
1614 synchronized (mMethodMap) {
1615 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1616 // TODO: Handle the case of the last IME with no subtypes
1617 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1618 || TextUtils.isEmpty(lastIme.second)) return null;
1619 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1620 if (lastImi == null) return null;
1621 try {
1622 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001623 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1624 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1625 return null;
1626 }
1627 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001628 } 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;
satok4a28bde2011-06-29 21:03:40 +09001644 mFileManager.addInputMethodSubtypes(imi, subtypes);
satoke7c6998e2011-06-03 17:57:59 +09001645 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();
satok4a28bde2011-06-29 21:03:40 +09002026 if (DEBUG) {
2027 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2028 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002029 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002030 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2031 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2032 // We show all enabled IMEs and subtypes when an IME is shown.
2033 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002034 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002035 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002036 final String mode = subtype.getMode();
2037 title = TextUtils.concat(subtype.getDisplayName(context,
2038 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2039 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002040 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2041 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002042 // Removing this subtype from enabledSubtypeSet because we no longer
2043 // need to add an entry of this subtype to imList to avoid duplicated
2044 // entries.
2045 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002046 }
satokab751aa2010-09-14 19:17:36 +09002047 }
2048 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002049 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2050 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002051 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002052 }
satok913a8922010-08-26 21:53:41 +09002053
Ken Wakasa761eb372011-03-04 19:06:18 +09002054 final int N = imList.size();
2055 mItems = new CharSequence[N];
2056 for (int i = 0; i < N; ++i) {
2057 mItems[i] = imList.get(i).first;
2058 }
satokab751aa2010-09-14 19:17:36 +09002059 mIms = new InputMethodInfo[N];
2060 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002061 int checkedItem = 0;
2062 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002063 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002064 mIms[i] = value.first;
2065 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002066 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002067 int subtypeId = mSubtypeIds[i];
2068 if ((subtypeId == NOT_A_SUBTYPE_ID)
2069 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2070 || (subtypeId == lastInputMethodSubtypeId)) {
2071 checkedItem = i;
2072 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 }
satokab751aa2010-09-14 19:17:36 +09002075
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002076 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002077 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002078 public void onClick(DialogInterface dialog, int which) {
2079 hideInputMethodMenu();
2080 }
2081 };
satokd87c2592010-09-29 11:52:06 +09002082
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002083 TypedArray a = context.obtainStyledAttributes(null,
2084 com.android.internal.R.styleable.DialogPreference,
2085 com.android.internal.R.attr.alertDialogStyle, 0);
2086 mDialogBuilder = new AlertDialog.Builder(context)
2087 .setTitle(com.android.internal.R.string.select_input_method)
2088 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002089 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002090 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002092 }
2093 })
2094 .setIcon(a.getDrawable(
2095 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2096 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002097
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002098 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2099 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002100 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002101 public void onClick(DialogInterface dialog, int which) {
2102 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002103 if (mIms == null || mIms.length <= which
2104 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002105 return;
2106 }
2107 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002108 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002109 hideInputMethodMenu();
2110 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002111 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002112 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002113 subtypeId = NOT_A_SUBTYPE_ID;
2114 }
2115 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002116 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002119 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120
satok7f35c8c2010-10-07 21:13:11 +09002121 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09002122 mDialogBuilder.setPositiveButton(
2123 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002124 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002125 @Override
satok7f35c8c2010-10-07 21:13:11 +09002126 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002127 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002128 }
2129 });
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002132 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 mSwitchingDialog.getWindow().setType(
2134 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002135 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 mSwitchingDialog.show();
2137 }
2138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002141 synchronized (mMethodMap) {
2142 hideInputMethodMenuLocked();
2143 }
2144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002145
The Android Open Source Project10592532009-03-18 17:39:46 -07002146 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002147 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148
The Android Open Source Project10592532009-03-18 17:39:46 -07002149 if (mSwitchingDialog != null) {
2150 mSwitchingDialog.dismiss();
2151 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
The Android Open Source Project10592532009-03-18 17:39:46 -07002154 mDialogBuilder = null;
2155 mItems = null;
2156 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002160
satok42c5a162011-05-26 16:46:14 +09002161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 public boolean setInputMethodEnabled(String id, boolean enabled) {
2163 synchronized (mMethodMap) {
2164 if (mContext.checkCallingOrSelfPermission(
2165 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2166 != PackageManager.PERMISSION_GRANTED) {
2167 throw new SecurityException(
2168 "Requires permission "
2169 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2170 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 long ident = Binder.clearCallingIdentity();
2173 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002174 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 } finally {
2176 Binder.restoreCallingIdentity(ident);
2177 }
2178 }
2179 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002180
2181 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2182 // Make sure this is a valid input method.
2183 InputMethodInfo imm = mMethodMap.get(id);
2184 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002185 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002186 }
2187
satokd87c2592010-09-29 11:52:06 +09002188 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2189 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002190
satokd87c2592010-09-29 11:52:06 +09002191 if (enabled) {
2192 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2193 if (pair.first.equals(id)) {
2194 // We are enabling this input method, but it is already enabled.
2195 // Nothing to do. The previous state was enabled.
2196 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002197 }
2198 }
satokd87c2592010-09-29 11:52:06 +09002199 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2200 // Previous state was disabled.
2201 return false;
2202 } else {
2203 StringBuilder builder = new StringBuilder();
2204 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2205 builder, enabledInputMethodsList, id)) {
2206 // Disabled input method is currently selected, switch to another one.
2207 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2208 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002209 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2210 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2211 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002212 }
2213 // Previous state was enabled.
2214 return true;
2215 } else {
2216 // We are disabling the input method but it is already disabled.
2217 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002218 return false;
2219 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002220 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002221 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222
satok57ffc002011-01-25 00:11:47 +09002223 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2224 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002225 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002226 }
2227
satok723a27e2010-11-11 14:58:11 +09002228 private void saveCurrentInputMethodAndSubtypeToHistory() {
2229 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2230 if (mCurrentSubtype != null) {
2231 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2232 }
satok57ffc002011-01-25 00:11:47 +09002233 if (canAddToLastInputMethod(mCurrentSubtype)) {
2234 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2235 }
satokab751aa2010-09-14 19:17:36 +09002236 }
2237
satok723a27e2010-11-11 14:58:11 +09002238 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2239 boolean setSubtypeOnly) {
2240 // Update the history of InputMethod and Subtype
2241 saveCurrentInputMethodAndSubtypeToHistory();
2242
2243 // Set Subtype here
2244 if (imi == null || subtypeId < 0) {
2245 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002246 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002247 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002248 if (subtypeId < imi.getSubtypeCount()) {
2249 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2250 mSettings.putSelectedSubtype(subtype.hashCode());
2251 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002252 } else {
2253 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2254 mCurrentSubtype = null;
2255 }
satokab751aa2010-09-14 19:17:36 +09002256 }
satok723a27e2010-11-11 14:58:11 +09002257
2258 if (!setSubtypeOnly) {
2259 // Set InputMethod here
2260 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2261 }
2262 }
2263
2264 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2265 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2266 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2267 // newDefaultIme is empty when there is no candidate for the selected IME.
2268 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2269 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2270 if (subtypeHashCode != null) {
2271 try {
2272 lastSubtypeId = getSubtypeIdFromHashCode(
2273 imi, Integer.valueOf(subtypeHashCode));
2274 } catch (NumberFormatException e) {
2275 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2276 }
2277 }
2278 }
2279 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002280 }
2281
2282 private int getSelectedInputMethodSubtypeId(String id) {
2283 InputMethodInfo imi = mMethodMap.get(id);
2284 if (imi == null) {
2285 return NOT_A_SUBTYPE_ID;
2286 }
satokab751aa2010-09-14 19:17:36 +09002287 int subtypeId;
2288 try {
2289 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2290 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2291 } catch (SettingNotFoundException e) {
2292 return NOT_A_SUBTYPE_ID;
2293 }
satok723a27e2010-11-11 14:58:11 +09002294 return getSubtypeIdFromHashCode(imi, subtypeId);
2295 }
2296
2297 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002298 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002299 final int subtypeCount = imi.getSubtypeCount();
2300 for (int i = 0; i < subtypeCount; ++i) {
2301 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002302 if (subtypeHashCode == ims.hashCode()) {
2303 return i;
2304 }
satokab751aa2010-09-14 19:17:36 +09002305 }
2306 }
2307 return NOT_A_SUBTYPE_ID;
2308 }
2309
satokdf31ae62011-01-15 06:19:44 +09002310 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2311 Resources res, List<InputMethodSubtype> subtypes) {
2312 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002313 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2314 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2315 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002316 final int N = subtypes.size();
2317 boolean containsKeyboardSubtype = false;
2318 for (int i = 0; i < N; ++i) {
2319 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002320 final String locale = subtype.getLocale();
2321 final String mode = subtype.getMode();
2322 // When system locale starts with subtype's locale, that subtype will be applicable
2323 // for system locale
2324 // For instance, it's clearly applicable for cases like system locale = en_US and
2325 // subtype = en, but it is not necessarily considered applicable for cases like system
2326 // locale = en and subtype = en_US.
2327 // We just call systemLocale.startsWith(locale) in this function because there is no
2328 // need to find applicable subtypes aggressively unlike
2329 // findLastResortApplicableSubtypeLocked.
2330 if (systemLocale.startsWith(locale)) {
2331 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2332 // If more applicable subtypes are contained, skip.
2333 if (applicableSubtype != null
2334 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2335 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002336 if (!containsKeyboardSubtype
2337 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2338 containsKeyboardSubtype = true;
2339 }
2340 }
2341 }
satok4a28bde2011-06-29 21:03:40 +09002342 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002343 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002344 if (!containsKeyboardSubtype) {
2345 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002346 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002347 if (lastResortKeyboardSubtype != null) {
2348 applicableSubtypes.add(lastResortKeyboardSubtype);
2349 }
2350 }
2351 return applicableSubtypes;
2352 }
2353
satok4e4569d2010-11-19 18:45:53 +09002354 /**
2355 * If there are no selected subtypes, tries finding the most applicable one according to the
2356 * given locale.
2357 * @param subtypes this function will search the most applicable subtype in subtypes
2358 * @param mode subtypes will be filtered by mode
2359 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002360 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2361 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002362 * @return the most applicable subtypeId
2363 */
satokdf31ae62011-01-15 06:19:44 +09002364 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2365 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002366 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002367 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002368 return null;
satok8fbb1e82010-11-02 23:15:58 +09002369 }
satok4e4569d2010-11-19 18:45:53 +09002370 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002371 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002372 }
satok8fbb1e82010-11-02 23:15:58 +09002373 final String language = locale.substring(0, 2);
2374 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002375 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002376 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002377 final int N = subtypes.size();
2378 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002379 InputMethodSubtype subtype = subtypes.get(i);
2380 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002381 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2382 // and all subtypes with all modes can be candidates.
2383 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002384 if (firstMatchedModeSubtype == null) {
2385 firstMatchedModeSubtype = subtype;
2386 }
satok9ef02832010-11-04 21:17:48 +09002387 if (locale.equals(subtypeLocale)) {
2388 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002389 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002390 break;
2391 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2392 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002393 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002394 partialMatchFound = true;
2395 }
satok8fbb1e82010-11-02 23:15:58 +09002396 }
2397 }
2398
satok7599a7f2010-12-22 13:45:23 +09002399 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2400 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002401 }
2402
satok8fbb1e82010-11-02 23:15:58 +09002403 // The first subtype applicable to the system locale will be defined as the most applicable
2404 // subtype.
2405 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002406 if (applicableSubtype != null) {
2407 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2408 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2409 }
satok8fbb1e82010-11-02 23:15:58 +09002410 }
satokcd7cd292010-11-20 15:46:23 +09002411 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002412 }
2413
satok4e4569d2010-11-19 18:45:53 +09002414 // If there are no selected shortcuts, tries finding the most applicable ones.
2415 private Pair<InputMethodInfo, InputMethodSubtype>
2416 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2417 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2418 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002419 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002420 boolean foundInSystemIME = false;
2421
2422 // Search applicable subtype for each InputMethodInfo
2423 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002424 final String imiId = imi.getId();
2425 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2426 continue;
2427 }
satokcd7cd292010-11-20 15:46:23 +09002428 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002429 final List<InputMethodSubtype> enabledSubtypes =
2430 getEnabledInputMethodSubtypeList(imi, true);
2431 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002432 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002433 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002434 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002435 }
satokdf31ae62011-01-15 06:19:44 +09002436 // 2. Search by the system locale from enabledSubtypes.
2437 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002438 if (subtype == null) {
2439 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002440 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002441 }
satok7599a7f2010-12-22 13:45:23 +09002442 // 4. Search by the current subtype's locale from all subtypes.
2443 if (subtype == null && mCurrentSubtype != null) {
2444 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002445 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002446 }
2447 // 5. Search by the system locale from all subtypes.
2448 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002449 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002450 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002451 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002452 }
satokcd7cd292010-11-20 15:46:23 +09002453 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002454 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002455 // The current input method is the most applicable IME.
2456 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002457 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002458 break;
satok7599a7f2010-12-22 13:45:23 +09002459 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002460 // The system input method is 2nd applicable IME.
2461 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002462 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002463 if ((imi.getServiceInfo().applicationInfo.flags
2464 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2465 foundInSystemIME = true;
2466 }
satok4e4569d2010-11-19 18:45:53 +09002467 }
2468 }
2469 }
2470 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002471 if (mostApplicableIMI != null) {
2472 Slog.w(TAG, "Most applicable shortcut input method was:"
2473 + mostApplicableIMI.getId());
2474 if (mostApplicableSubtype != null) {
2475 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2476 + "," + mostApplicableSubtype.getMode() + ","
2477 + mostApplicableSubtype.getLocale());
2478 }
2479 }
satok4e4569d2010-11-19 18:45:53 +09002480 }
satokcd7cd292010-11-20 15:46:23 +09002481 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002482 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002483 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002484 } else {
2485 return null;
2486 }
2487 }
2488
satokab751aa2010-09-14 19:17:36 +09002489 /**
2490 * @return Return the current subtype of this input method.
2491 */
satok42c5a162011-05-26 16:46:14 +09002492 @Override
satokab751aa2010-09-14 19:17:36 +09002493 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002494 boolean subtypeIsSelected = false;
2495 try {
2496 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2497 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2498 } catch (SettingNotFoundException e) {
2499 }
satok3ef8b292010-11-23 06:06:29 +09002500 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002501 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002502 String lastInputMethodId = Settings.Secure.getString(
2503 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002504 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2505 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002506 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2507 if (imi != null) {
2508 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002509 // the most applicable subtype from explicitly or implicitly enabled
2510 // subtypes.
2511 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2512 getEnabledInputMethodSubtypeList(imi, true);
2513 // If there is only one explicitly or implicitly enabled subtype,
2514 // just returns it.
2515 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2516 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2517 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2518 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2519 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2520 SUBTYPE_MODE_KEYBOARD, null, true);
2521 if (mCurrentSubtype == null) {
2522 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2523 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2524 true);
2525 }
2526 }
satok4e4569d2010-11-19 18:45:53 +09002527 }
satokcd7cd292010-11-20 15:46:23 +09002528 } else {
satok3ef8b292010-11-23 06:06:29 +09002529 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002530 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002531 }
satok8fbb1e82010-11-02 23:15:58 +09002532 }
satok3ef8b292010-11-23 06:06:29 +09002533 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002534 }
satokab751aa2010-09-14 19:17:36 +09002535 }
2536
satokf3db1af2010-11-23 13:34:33 +09002537 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2538 InputMethodSubtype subtype) {
2539 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2540 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2541 } else {
2542 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2543 subtypes.add(subtype);
2544 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2545 }
2546 }
2547
satok4e4569d2010-11-19 18:45:53 +09002548 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002549 @Override
satok4e4569d2010-11-19 18:45:53 +09002550 public List getShortcutInputMethodsAndSubtypes() {
2551 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002552 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002553 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002554 // If there are no selected shortcut subtypes, the framework will try to find
2555 // the most applicable subtype from all subtypes whose mode is
2556 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002557 Pair<InputMethodInfo, InputMethodSubtype> info =
2558 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2559 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002560 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002561 ret.add(info.first);
2562 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002563 }
satok3da92232011-01-11 22:46:30 +09002564 return ret;
satokf3db1af2010-11-23 13:34:33 +09002565 }
satokf3db1af2010-11-23 13:34:33 +09002566 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2567 ret.add(imi);
2568 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2569 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002570 }
2571 }
satokf3db1af2010-11-23 13:34:33 +09002572 return ret;
satok4e4569d2010-11-19 18:45:53 +09002573 }
2574 }
2575
satok42c5a162011-05-26 16:46:14 +09002576 @Override
satokb66d2872010-11-10 01:04:04 +09002577 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2578 synchronized (mMethodMap) {
2579 if (subtype != null && mCurMethodId != null) {
2580 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2581 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2582 if (subtypeId != NOT_A_SUBTYPE_ID) {
2583 setInputMethodLocked(mCurMethodId, subtypeId);
2584 return true;
2585 }
2586 }
2587 return false;
2588 }
2589 }
2590
satokd87c2592010-09-29 11:52:06 +09002591 /**
2592 * Utility class for putting and getting settings for InputMethod
2593 * TODO: Move all putters and getters of settings to this class.
2594 */
2595 private static class InputMethodSettings {
2596 // The string for enabled input method is saved as follows:
2597 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2598 private static final char INPUT_METHOD_SEPARATER = ':';
2599 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002600 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002601 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2602
satok723a27e2010-11-11 14:58:11 +09002603 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002604 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2605
satokdf31ae62011-01-15 06:19:44 +09002606 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002607 private final ContentResolver mResolver;
2608 private final HashMap<String, InputMethodInfo> mMethodMap;
2609 private final ArrayList<InputMethodInfo> mMethodList;
2610
2611 private String mEnabledInputMethodsStrCache;
2612
2613 private static void buildEnabledInputMethodsSettingString(
2614 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2615 String id = pair.first;
2616 ArrayList<String> subtypes = pair.second;
2617 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002618 // Inputmethod and subtypes are saved in the settings as follows:
2619 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2620 for (String subtypeId: subtypes) {
2621 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002622 }
2623 }
2624
2625 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002626 Resources res, ContentResolver resolver,
2627 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2628 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002629 mResolver = resolver;
2630 mMethodMap = methodMap;
2631 mMethodList = methodList;
2632 }
2633
2634 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2635 return createEnabledInputMethodListLocked(
2636 getEnabledInputMethodsAndSubtypeListLocked());
2637 }
2638
satok7f35c8c2010-10-07 21:13:11 +09002639 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002640 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2641 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002642 getEnabledInputMethodsAndSubtypeListLocked());
2643 }
2644
satok67ddf9c2010-11-17 09:45:54 +09002645 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2646 InputMethodInfo imi) {
2647 List<Pair<String, ArrayList<String>>> imsList =
2648 getEnabledInputMethodsAndSubtypeListLocked();
2649 ArrayList<InputMethodSubtype> enabledSubtypes =
2650 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002651 if (imi != null) {
2652 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2653 InputMethodInfo info = mMethodMap.get(imsPair.first);
2654 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002655 final int subtypeCount = info.getSubtypeCount();
2656 for (int i = 0; i < subtypeCount; ++i) {
2657 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002658 for (String s: imsPair.second) {
2659 if (String.valueOf(ims.hashCode()).equals(s)) {
2660 enabledSubtypes.add(ims);
2661 }
satok67ddf9c2010-11-17 09:45:54 +09002662 }
2663 }
satok884ef9a2010-11-18 10:39:46 +09002664 break;
satok67ddf9c2010-11-17 09:45:54 +09002665 }
satok67ddf9c2010-11-17 09:45:54 +09002666 }
2667 }
2668 return enabledSubtypes;
2669 }
2670
satokd87c2592010-09-29 11:52:06 +09002671 // At the initial boot, the settings for input methods are not set,
2672 // so we need to enable IME in that case.
2673 public void enableAllIMEsIfThereIsNoEnabledIME() {
2674 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2675 StringBuilder sb = new StringBuilder();
2676 final int N = mMethodList.size();
2677 for (int i = 0; i < N; i++) {
2678 InputMethodInfo imi = mMethodList.get(i);
2679 Slog.i(TAG, "Adding: " + imi.getId());
2680 if (i > 0) sb.append(':');
2681 sb.append(imi.getId());
2682 }
2683 putEnabledInputMethodsStr(sb.toString());
2684 }
2685 }
2686
satokbb4aa062011-01-19 21:40:27 +09002687 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002688 ArrayList<Pair<String, ArrayList<String>>> imsList
2689 = new ArrayList<Pair<String, ArrayList<String>>>();
2690 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2691 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2692 return imsList;
2693 }
satok723a27e2010-11-11 14:58:11 +09002694 mInputMethodSplitter.setString(enabledInputMethodsStr);
2695 while (mInputMethodSplitter.hasNext()) {
2696 String nextImsStr = mInputMethodSplitter.next();
2697 mSubtypeSplitter.setString(nextImsStr);
2698 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002699 ArrayList<String> subtypeHashes = new ArrayList<String>();
2700 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002701 String imeId = mSubtypeSplitter.next();
2702 while (mSubtypeSplitter.hasNext()) {
2703 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002704 }
2705 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2706 }
2707 }
2708 return imsList;
2709 }
2710
2711 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2712 if (reloadInputMethodStr) {
2713 getEnabledInputMethodsStr();
2714 }
2715 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2716 // Add in the newly enabled input method.
2717 putEnabledInputMethodsStr(id);
2718 } else {
2719 putEnabledInputMethodsStr(
2720 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2721 }
2722 }
2723
2724 /**
2725 * Build and put a string of EnabledInputMethods with removing specified Id.
2726 * @return the specified id was removed or not.
2727 */
2728 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2729 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2730 boolean isRemoved = false;
2731 boolean needsAppendSeparator = false;
2732 for (Pair<String, ArrayList<String>> ims: imsList) {
2733 String curId = ims.first;
2734 if (curId.equals(id)) {
2735 // We are disabling this input method, and it is
2736 // currently enabled. Skip it to remove from the
2737 // new list.
2738 isRemoved = true;
2739 } else {
2740 if (needsAppendSeparator) {
2741 builder.append(INPUT_METHOD_SEPARATER);
2742 } else {
2743 needsAppendSeparator = true;
2744 }
2745 buildEnabledInputMethodsSettingString(builder, ims);
2746 }
2747 }
2748 if (isRemoved) {
2749 // Update the setting with the new list of input methods.
2750 putEnabledInputMethodsStr(builder.toString());
2751 }
2752 return isRemoved;
2753 }
2754
2755 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2756 List<Pair<String, ArrayList<String>>> imsList) {
2757 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2758 for (Pair<String, ArrayList<String>> ims: imsList) {
2759 InputMethodInfo info = mMethodMap.get(ims.first);
2760 if (info != null) {
2761 res.add(info);
2762 }
2763 }
2764 return res;
2765 }
2766
satok7f35c8c2010-10-07 21:13:11 +09002767 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002768 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002769 List<Pair<String, ArrayList<String>>> imsList) {
2770 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2771 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2772 for (Pair<String, ArrayList<String>> ims : imsList) {
2773 InputMethodInfo info = mMethodMap.get(ims.first);
2774 if (info != null) {
2775 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2776 }
2777 }
2778 return res;
2779 }
2780
satokd87c2592010-09-29 11:52:06 +09002781 private void putEnabledInputMethodsStr(String str) {
2782 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2783 mEnabledInputMethodsStrCache = str;
2784 }
2785
2786 private String getEnabledInputMethodsStr() {
2787 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2788 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002789 if (DEBUG) {
2790 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2791 }
satokd87c2592010-09-29 11:52:06 +09002792 return mEnabledInputMethodsStrCache;
2793 }
satok723a27e2010-11-11 14:58:11 +09002794
2795 private void saveSubtypeHistory(
2796 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2797 StringBuilder builder = new StringBuilder();
2798 boolean isImeAdded = false;
2799 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2800 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2801 newSubtypeId);
2802 isImeAdded = true;
2803 }
2804 for (Pair<String, String> ime: savedImes) {
2805 String imeId = ime.first;
2806 String subtypeId = ime.second;
2807 if (TextUtils.isEmpty(subtypeId)) {
2808 subtypeId = NOT_A_SUBTYPE_ID_STR;
2809 }
2810 if (isImeAdded) {
2811 builder.append(INPUT_METHOD_SEPARATER);
2812 } else {
2813 isImeAdded = true;
2814 }
2815 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2816 subtypeId);
2817 }
2818 // Remove the last INPUT_METHOD_SEPARATER
2819 putSubtypeHistoryStr(builder.toString());
2820 }
2821
2822 public void addSubtypeToHistory(String imeId, String subtypeId) {
2823 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2824 for (Pair<String, String> ime: subtypeHistory) {
2825 if (ime.first.equals(imeId)) {
2826 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002827 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002828 + ime.second);
2829 }
2830 // We should break here
2831 subtypeHistory.remove(ime);
2832 break;
2833 }
2834 }
satokbb4aa062011-01-19 21:40:27 +09002835 if (DEBUG) {
2836 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2837 }
satok723a27e2010-11-11 14:58:11 +09002838 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2839 }
2840
2841 private void putSubtypeHistoryStr(String str) {
2842 if (DEBUG) {
2843 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2844 }
2845 Settings.Secure.putString(
2846 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2847 }
2848
2849 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2850 // Gets the first one from the history
2851 return getLastSubtypeForInputMethodLockedInternal(null);
2852 }
2853
2854 public String getLastSubtypeForInputMethodLocked(String imeId) {
2855 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2856 if (ime != null) {
2857 return ime.second;
2858 } else {
2859 return null;
2860 }
2861 }
2862
2863 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2864 List<Pair<String, ArrayList<String>>> enabledImes =
2865 getEnabledInputMethodsAndSubtypeListLocked();
2866 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002867 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002868 final String imeInTheHistory = imeAndSubtype.first;
2869 // If imeId is empty, returns the first IME and subtype in the history
2870 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2871 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002872 final String subtypeHashCode =
2873 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2874 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002875 if (!TextUtils.isEmpty(subtypeHashCode)) {
2876 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002877 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002878 }
2879 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2880 }
2881 }
2882 }
2883 if (DEBUG) {
2884 Slog.d(TAG, "No enabled IME found in the history");
2885 }
2886 return null;
2887 }
2888
satokdf31ae62011-01-15 06:19:44 +09002889 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002890 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2891 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2892 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002893 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2894 if (explicitlyEnabledSubtypes.size() == 0) {
2895 // If there are no explicitly enabled subtypes, applicable subtypes are
2896 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002897 InputMethodInfo ime = mMethodMap.get(imeId);
2898 // If IME is enabled and no subtypes are enabled, applicable subtypes
2899 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002900 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002901 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002902 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002903 if (implicitlySelectedSubtypes != null) {
2904 final int N = implicitlySelectedSubtypes.size();
2905 for (int i = 0; i < N; ++i) {
2906 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2907 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2908 return subtypeHashCode;
2909 }
2910 }
2911 }
2912 }
2913 } else {
satokf6cafb62011-01-17 16:29:02 +09002914 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002915 if (s.equals(subtypeHashCode)) {
2916 // If both imeId and subtypeId are enabled, return subtypeId.
2917 return s;
2918 }
satok723a27e2010-11-11 14:58:11 +09002919 }
2920 }
2921 // If imeId was enabled but subtypeId was disabled.
2922 return NOT_A_SUBTYPE_ID_STR;
2923 }
2924 }
2925 // If both imeId and subtypeId are disabled, return null
2926 return null;
2927 }
2928
2929 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2930 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2931 final String subtypeHistoryStr = getSubtypeHistoryStr();
2932 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2933 return imsList;
2934 }
2935 mInputMethodSplitter.setString(subtypeHistoryStr);
2936 while (mInputMethodSplitter.hasNext()) {
2937 String nextImsStr = mInputMethodSplitter.next();
2938 mSubtypeSplitter.setString(nextImsStr);
2939 if (mSubtypeSplitter.hasNext()) {
2940 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2941 // The first element is ime id.
2942 String imeId = mSubtypeSplitter.next();
2943 while (mSubtypeSplitter.hasNext()) {
2944 subtypeId = mSubtypeSplitter.next();
2945 break;
2946 }
2947 imsList.add(new Pair<String, String>(imeId, subtypeId));
2948 }
2949 }
2950 return imsList;
2951 }
2952
2953 private String getSubtypeHistoryStr() {
2954 if (DEBUG) {
2955 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2956 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2957 }
2958 return Settings.Secure.getString(
2959 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2960 }
2961
2962 public void putSelectedInputMethod(String imeId) {
2963 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2964 }
2965
2966 public void putSelectedSubtype(int subtypeId) {
2967 Settings.Secure.putInt(
2968 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2969 }
satokd87c2592010-09-29 11:52:06 +09002970 }
2971
satoke7c6998e2011-06-03 17:57:59 +09002972 private static class InputMethodFileManager {
2973 private static final String SYSTEM_PATH = "system";
2974 private static final String INPUT_METHOD_PATH = "inputmethod";
2975 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2976 private static final String NODE_SUBTYPES = "subtypes";
2977 private static final String NODE_SUBTYPE = "subtype";
2978 private static final String NODE_IMI = "imi";
2979 private static final String ATTR_ID = "id";
2980 private static final String ATTR_LABEL = "label";
2981 private static final String ATTR_ICON = "icon";
2982 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2983 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2984 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2985 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2986 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2987 private final HashMap<String, InputMethodInfo> mMethodMap;
2988 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2989 new HashMap<String, List<InputMethodSubtype>>();
2990 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2991 if (methodMap == null) {
2992 throw new NullPointerException("methodMap is null");
2993 }
2994 mMethodMap = methodMap;
2995 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
2996 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
2997 if (!inputMethodDir.mkdirs()) {
2998 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
2999 }
3000 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3001 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3002 if (!subtypeFile.exists()) {
3003 // If "subtypes.xml" doesn't exist, create a blank file.
3004 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3005 methodMap);
3006 } else {
3007 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3008 }
3009 }
3010
3011 private void deleteAllInputMethodSubtypes(String imiId) {
3012 synchronized (mMethodMap) {
3013 mSubtypesMap.remove(imiId);
3014 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3015 mMethodMap);
3016 }
3017 }
3018
3019 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003020 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003021 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003022 final HashSet<InputMethodSubtype> existingSubtypes =
3023 new HashSet<InputMethodSubtype>();
3024 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3025 existingSubtypes.add(imi.getSubtypeAt(i));
3026 }
3027
satoke7c6998e2011-06-03 17:57:59 +09003028 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3029 final int N = additionalSubtypes.length;
3030 for (int i = 0; i < N; ++i) {
3031 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003032 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003033 subtypes.add(subtype);
3034 }
3035 }
satok4a28bde2011-06-29 21:03:40 +09003036 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003037 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3038 mMethodMap);
3039 }
3040 }
3041
3042 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3043 synchronized (mMethodMap) {
3044 return mSubtypesMap;
3045 }
3046 }
3047
3048 private static void writeAdditionalInputMethodSubtypes(
3049 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3050 HashMap<String, InputMethodInfo> methodMap) {
3051 // Safety net for the case that this function is called before methodMap is set.
3052 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3053 FileOutputStream fos = null;
3054 try {
3055 fos = subtypesFile.startWrite();
3056 final XmlSerializer out = new FastXmlSerializer();
3057 out.setOutput(fos, "utf-8");
3058 out.startDocument(null, true);
3059 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3060 out.startTag(null, NODE_SUBTYPES);
3061 for (String imiId : allSubtypes.keySet()) {
3062 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3063 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3064 continue;
3065 }
3066 out.startTag(null, NODE_IMI);
3067 out.attribute(null, ATTR_ID, imiId);
3068 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3069 final int N = subtypesList.size();
3070 for (int i = 0; i < N; ++i) {
3071 final InputMethodSubtype subtype = subtypesList.get(i);
3072 out.startTag(null, NODE_SUBTYPE);
3073 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3074 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3075 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3076 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3077 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3078 out.attribute(null, ATTR_IS_AUXILIARY,
3079 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3080 out.endTag(null, NODE_SUBTYPE);
3081 }
3082 out.endTag(null, NODE_IMI);
3083 }
3084 out.endTag(null, NODE_SUBTYPES);
3085 out.endDocument();
3086 subtypesFile.finishWrite(fos);
3087 } catch (java.io.IOException e) {
3088 Slog.w(TAG, "Error writing subtypes", e);
3089 if (fos != null) {
3090 subtypesFile.failWrite(fos);
3091 }
3092 }
3093 }
3094
3095 private static void readAdditionalInputMethodSubtypes(
3096 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3097 if (allSubtypes == null || subtypesFile == null) return;
3098 allSubtypes.clear();
3099 FileInputStream fis = null;
3100 try {
3101 fis = subtypesFile.openRead();
3102 final XmlPullParser parser = Xml.newPullParser();
3103 parser.setInput(fis, null);
3104 int type = parser.getEventType();
3105 // Skip parsing until START_TAG
3106 while ((type = parser.next()) != XmlPullParser.START_TAG
3107 && type != XmlPullParser.END_DOCUMENT) {}
3108 String firstNodeName = parser.getName();
3109 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3110 throw new XmlPullParserException("Xml doesn't start with subtypes");
3111 }
3112 final int depth =parser.getDepth();
3113 String currentImiId = null;
3114 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3115 while (((type = parser.next()) != XmlPullParser.END_TAG
3116 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3117 if (type != XmlPullParser.START_TAG)
3118 continue;
3119 final String nodeName = parser.getName();
3120 if (NODE_IMI.equals(nodeName)) {
3121 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3122 if (TextUtils.isEmpty(currentImiId)) {
3123 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3124 continue;
3125 }
3126 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3127 allSubtypes.put(currentImiId, tempSubtypesArray);
3128 } else if (NODE_SUBTYPE.equals(nodeName)) {
3129 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3130 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3131 continue;
3132 }
3133 final int icon = Integer.valueOf(
3134 parser.getAttributeValue(null, ATTR_ICON));
3135 final int label = Integer.valueOf(
3136 parser.getAttributeValue(null, ATTR_LABEL));
3137 final String imeSubtypeLocale =
3138 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3139 final String imeSubtypeMode =
3140 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3141 final String imeSubtypeExtraValue =
3142 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003143 final boolean isAuxiliary = "1".equals(String.valueOf(
3144 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003145 final InputMethodSubtype subtype =
3146 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3147 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3148 tempSubtypesArray.add(subtype);
3149 }
3150 }
3151 } catch (XmlPullParserException e) {
3152 Slog.w(TAG, "Error reading subtypes: " + e);
3153 return;
3154 } catch (java.io.IOException e) {
3155 Slog.w(TAG, "Error reading subtypes: " + e);
3156 return;
3157 } catch (NumberFormatException e) {
3158 Slog.w(TAG, "Error reading subtypes: " + e);
3159 return;
3160 } finally {
3161 if (fis != null) {
3162 try {
3163 fis.close();
3164 } catch (java.io.IOException e1) {
3165 Slog.w(TAG, "Failed to close.");
3166 }
3167 }
3168 }
3169 }
3170 }
3171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 @Override
3175 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3176 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3177 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3180 + Binder.getCallingPid()
3181 + ", uid=" + Binder.getCallingUid());
3182 return;
3183 }
3184
3185 IInputMethod method;
3186 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 synchronized (mMethodMap) {
3191 p.println("Current Input Method Manager state:");
3192 int N = mMethodList.size();
3193 p.println(" Input Methods:");
3194 for (int i=0; i<N; i++) {
3195 InputMethodInfo info = mMethodList.get(i);
3196 p.println(" InputMethod #" + i + ":");
3197 info.dump(p, " ");
3198 }
3199 p.println(" Clients:");
3200 for (ClientState ci : mClients.values()) {
3201 p.println(" Client " + ci + ":");
3202 p.println(" client=" + ci.client);
3203 p.println(" inputContext=" + ci.inputContext);
3204 p.println(" sessionRequested=" + ci.sessionRequested);
3205 p.println(" curSession=" + ci.curSession);
3206 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003207 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003209 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3210 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3212 + " mBoundToMethod=" + mBoundToMethod);
3213 p.println(" mCurToken=" + mCurToken);
3214 p.println(" mCurIntent=" + mCurIntent);
3215 method = mCurMethod;
3216 p.println(" mCurMethod=" + mCurMethod);
3217 p.println(" mEnabledSession=" + mEnabledSession);
3218 p.println(" mShowRequested=" + mShowRequested
3219 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3220 + " mShowForced=" + mShowForced
3221 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003222 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003224
Jeff Brownb88102f2010-09-08 11:49:43 -07003225 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 pw.flush();
3228 try {
3229 client.client.asBinder().dump(fd, args);
3230 } catch (RemoteException e) {
3231 p.println("Input method client dead: " + e);
3232 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003233 } else {
3234 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003236
Jeff Brownb88102f2010-09-08 11:49:43 -07003237 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 pw.flush();
3240 try {
3241 method.asBinder().dump(fd, args);
3242 } catch (RemoteException e) {
3243 p.println("Input method service dead: " + e);
3244 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003245 } else {
3246 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
3248 }
3249}