blob: 2d55433d9cd0ef2ffced9a7f32cd8b1d6c19aeff [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090020import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090022import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.view.IInputContext;
24import com.android.internal.view.IInputMethod;
25import com.android.internal.view.IInputMethodCallback;
26import com.android.internal.view.IInputMethodClient;
27import com.android.internal.view.IInputMethodManager;
28import com.android.internal.view.IInputMethodSession;
29import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
satok7cfc0ed2011-06-20 21:29:36 +090038import android.app.Notification;
39import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070040import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.DialogInterface.OnCancelListener;
46import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090047import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageManager;
51import android.content.pm.ResolveInfo;
52import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070053import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.res.Resources;
55import android.content.res.TypedArray;
56import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090057import android.graphics.BitmapFactory;
Joe Onorato857fd9b2011-01-27 15:08:35 -080058import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
63import android.os.IInterface;
64import android.os.Message;
65import android.os.Parcel;
66import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080067import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090072import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090074import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090076import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090077import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.PrintWriterPrinter;
79import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090080import android.util.Slog;
81import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.IWindowManager;
83import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090084import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.inputmethod.InputBinding;
86import android.view.inputmethod.InputMethod;
87import android.view.inputmethod.InputMethodInfo;
88import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090089import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
satoke7c6998e2011-06-03 17:57:59 +090091import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090093import java.io.FileInputStream;
94import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.IOException;
96import java.io.PrintWriter;
97import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +090098import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +090099import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900101import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900103import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900104import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106/**
107 * This class provides a system service that manages input methods.
108 */
109public class InputMethodManagerService extends IInputMethodManager.Stub
110 implements ServiceConnection, Handler.Callback {
111 static final boolean DEBUG = false;
112 static final String TAG = "InputManagerService";
113
114 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900115 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900116 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900117 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 static final int MSG_UNBIND_INPUT = 1000;
120 static final int MSG_BIND_INPUT = 1010;
121 static final int MSG_SHOW_SOFT_INPUT = 1020;
122 static final int MSG_HIDE_SOFT_INPUT = 1030;
123 static final int MSG_ATTACH_TOKEN = 1040;
124 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 static final int MSG_START_INPUT = 2000;
127 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 static final int MSG_UNBIND_METHOD = 3000;
130 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
satokf9f01002011-05-19 21:31:50 +0900134 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
135
satokab751aa2010-09-14 19:17:36 +0900136 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900137 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900138 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
139 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900140 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800143 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900145 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700147 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final IWindowManager mIWindowManager;
149 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900150 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 // All known input methods. mMethodMap also serves as the global
155 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900156 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
157 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900158 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
159 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800160
satok7cfc0ed2011-06-20 21:29:36 +0900161 // Ongoing notification
162 private final NotificationManager mNotificationManager;
163 private final Notification mImeSwitcherNotification;
164 private final PendingIntent mImeSwitchPendingIntent;
165 private final boolean mShowOngoingImeSwitcherForPhones;
166 private boolean mNotificationShown;
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 class SessionState {
169 final ClientState client;
170 final IInputMethod method;
171 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 @Override
174 public String toString() {
175 return "SessionState{uid " + client.uid + " pid " + client.pid
176 + " method " + Integer.toHexString(
177 System.identityHashCode(method))
178 + " session " + Integer.toHexString(
179 System.identityHashCode(session))
180 + "}";
181 }
182
183 SessionState(ClientState _client, IInputMethod _method,
184 IInputMethodSession _session) {
185 client = _client;
186 method = _method;
187 session = _session;
188 }
189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 class ClientState {
192 final IInputMethodClient client;
193 final IInputContext inputContext;
194 final int uid;
195 final int pid;
196 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 boolean sessionRequested;
199 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 @Override
202 public String toString() {
203 return "ClientState{" + Integer.toHexString(
204 System.identityHashCode(this)) + " uid " + uid
205 + " pid " + pid + "}";
206 }
207
208 ClientState(IInputMethodClient _client, IInputContext _inputContext,
209 int _uid, int _pid) {
210 client = _client;
211 inputContext = _inputContext;
212 uid = _uid;
213 pid = _pid;
214 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
215 }
216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 final HashMap<IBinder, ClientState> mClients
219 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700222 * Set once the system is ready to run third party code.
223 */
224 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * Id of the currently selected input method.
228 */
229 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * The current binding sequence number, incremented every time there is
233 * a new bind performed.
234 */
235 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /**
238 * The client that is currently bound to an input method.
239 */
240 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700243 * The last window token that gained focus.
244 */
245 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * The input context last provided by the current client.
249 */
250 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
253 * The attributes last provided by the current client.
254 */
255 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The input method ID of the input method service that we are currently
259 * connected to or in the process of connecting to.
260 */
261 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
satokab751aa2010-09-14 19:17:36 +0900264 * The current subtype of the current input method.
265 */
266 private InputMethodSubtype mCurrentSubtype;
267
satok4e4569d2010-11-19 18:45:53 +0900268 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900269 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
270 mShortcutInputMethodsAndSubtypes =
271 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900272
273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * Set to true if our ServiceConnection is currently actively bound to
275 * a service (whether or not we have gotten its IBinder back yet).
276 */
277 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Set if the client has asked for the input method to be shown.
281 */
282 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Set if we were explicitly told to show the input method.
286 */
287 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Set if we were forced to be shown.
291 */
292 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if we last told the input method to show itself.
296 */
297 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * The Intent used to connect to the current input method.
301 */
302 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * The token we have made for the currently active input method, to
306 * identify it in the future.
307 */
308 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 /**
311 * If non-null, this is the input method service we are currently connected
312 * to.
313 */
314 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * Time that we last initiated a bind to the input method, to determine
318 * if we should try to disconnect and reconnect to it.
319 */
320 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Have we called mCurMethod.bindInput()?
324 */
325 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Currently enabled session. Only touched by service thread, not
329 * protected by a lock.
330 */
331 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * True if the screen is on. The value is true initially.
335 */
336 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
Joe Onorato857fd9b2011-01-27 15:08:35 -0800338 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
339 int mImeWindowVis;
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 AlertDialog.Builder mDialogBuilder;
342 AlertDialog mSwitchingDialog;
343 InputMethodInfo[] mIms;
344 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900345 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 class SettingsObserver extends ContentObserver {
348 SettingsObserver(Handler handler) {
349 super(handler);
350 ContentResolver resolver = mContext.getContentResolver();
351 resolver.registerContentObserver(Settings.Secure.getUriFor(
352 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900353 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900354 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900356 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 @Override public void onChange(boolean selfChange) {
360 synchronized (mMethodMap) {
361 updateFromSettingsLocked();
362 }
363 }
364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
367 @Override
368 public void onReceive(Context context, Intent intent) {
369 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
370 mScreenOn = true;
371 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
372 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700373 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
374 hideInputMethodMenu();
375 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800377 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 // Inform the current client of the change in active status
381 try {
382 if (mCurClient != null && mCurClient.client != null) {
383 mCurClient.client.setActive(mScreenOn);
384 }
385 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800386 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 + mCurClient.pid + " uid " + mCurClient.uid);
388 }
389 }
390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800392 class MyPackageMonitor extends PackageMonitor {
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800395 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800397 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
399 final int N = mMethodList.size();
400 if (curInputMethodId != null) {
401 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800402 InputMethodInfo imi = mMethodList.get(i);
403 if (imi.getId().equals(curInputMethodId)) {
404 for (String pkg : packages) {
405 if (imi.getPackageName().equals(pkg)) {
406 if (!doit) {
407 return true;
408 }
satok723a27e2010-11-11 14:58:11 +0900409 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800410 chooseNewDefaultIMELocked();
411 return true;
412 }
413 }
414 }
415 }
416 }
417 }
418 return false;
419 }
420
421 @Override
422 public void onSomePackagesChanged() {
423 synchronized (mMethodMap) {
424 InputMethodInfo curIm = null;
425 String curInputMethodId = Settings.Secure.getString(mContext
426 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
427 final int N = mMethodList.size();
428 if (curInputMethodId != null) {
429 for (int i=0; i<N; i++) {
430 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900431 final String imiId = imi.getId();
432 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 curIm = imi;
434 }
satoke7c6998e2011-06-03 17:57:59 +0900435
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900437 if (isPackageModified(imi.getPackageName())) {
438 mFileManager.deleteAllInputMethodSubtypes(imiId);
439 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 if (change == PACKAGE_TEMPORARY_CHANGE
441 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800442 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 + imi.getComponent());
444 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446 }
447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800449 buildInputMethodListLocked(mMethodList, mMethodMap);
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800453 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 int change = isPackageDisappearing(curIm.getPackageName());
455 if (change == PACKAGE_TEMPORARY_CHANGE
456 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 ServiceInfo si = null;
458 try {
459 si = mContext.getPackageManager().getServiceInfo(
460 curIm.getComponent(), 0);
461 } catch (PackageManager.NameNotFoundException ex) {
462 }
463 if (si == null) {
464 // Uh oh, current input method is no longer around!
465 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800466 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800467 mImeWindowVis = 0;
468 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
469 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800471 changed = true;
472 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900474 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 }
476 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800477 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 }
satokab751aa2010-09-14 19:17:36 +0900479
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 if (curIm == null) {
481 // We currently don't have a default input method... is
482 // one now available?
483 changed = chooseNewDefaultIMELocked();
484 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800485
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 if (changed) {
487 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 }
489 }
490 }
491 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 class MethodCallback extends IInputMethodCallback.Stub {
494 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 MethodCallback(IInputMethod method) {
497 mMethod = method;
498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
satoke7c6998e2011-06-03 17:57:59 +0900500 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 public void finishedEvent(int seq, boolean handled) throws RemoteException {
502 }
503
satoke7c6998e2011-06-03 17:57:59 +0900504 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public void sessionCreated(IInputMethodSession session) throws RemoteException {
506 onSessionCreated(mMethod, session);
507 }
508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Joe Onorato089de882010-04-12 08:18:45 -0700510 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800512 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 mHandler = new Handler(this);
514 mIWindowManager = IWindowManager.Stub.asInterface(
515 ServiceManager.getService(Context.WINDOW_SERVICE));
516 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900517 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public void executeMessage(Message msg) {
519 handleMessage(msg);
520 }
521 });
satok7cfc0ed2011-06-20 21:29:36 +0900522
523 mNotificationManager = (NotificationManager)
524 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
525 mImeSwitcherNotification = new Notification();
526 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
527 mImeSwitcherNotification.when = 0;
528 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
529 mImeSwitcherNotification.tickerText = null;
530 mImeSwitcherNotification.defaults = 0; // please be quiet
531 mImeSwitcherNotification.sound = null;
532 mImeSwitcherNotification.vibrate = null;
533 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
534 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
535 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
536 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
537 mImeSwitchPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
538 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
539 com.android.internal.R.bool.show_ongoing_ime_switcher);
540
satoke7c6998e2011-06-03 17:57:59 +0900541 synchronized (mMethodMap) {
542 mFileManager = new InputMethodFileManager(mMethodMap);
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 IntentFilter screenOnOffFilt = new IntentFilter();
548 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
549 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700550 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800552
satok913a8922010-08-26 21:53:41 +0900553 mStatusBar = statusBar;
554 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900555 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900556
satokd87c2592010-09-29 11:52:06 +0900557 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900558 mSettings = new InputMethodSettings(
559 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900561 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
satokd87c2592010-09-29 11:52:06 +0900563 if (TextUtils.isEmpty(Settings.Secure.getString(
564 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900566 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
568 try {
satokd87c2592010-09-29 11:52:06 +0900569 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 imi.getPackageName(), 0).getResources();
571 if (res.getBoolean(imi.getIsDefaultResourceId())) {
572 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800573 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
575 } catch (PackageManager.NameNotFoundException ex) {
576 } catch (Resources.NotFoundException ex) {
577 }
578 }
579 }
satokd87c2592010-09-29 11:52:06 +0900580 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800582 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900585 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 mSettingsObserver = new SettingsObserver(mHandler);
590 updateFromSettingsLocked();
591 }
592
593 @Override
594 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
595 throws RemoteException {
596 try {
597 return super.onTransact(code, data, reply, flags);
598 } catch (RuntimeException e) {
599 // The input method manager only throws security exceptions, so let's
600 // log all others.
601 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800602 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604 throw e;
605 }
606 }
607
608 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700609 synchronized (mMethodMap) {
610 if (!mSystemReady) {
611 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700612 try {
613 startInputInnerLocked();
614 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800615 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700616 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700617 }
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
satoke7c6998e2011-06-03 17:57:59 +0900621 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public List<InputMethodInfo> getInputMethodList() {
623 synchronized (mMethodMap) {
624 return new ArrayList<InputMethodInfo>(mMethodList);
625 }
626 }
627
satoke7c6998e2011-06-03 17:57:59 +0900628 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 public List<InputMethodInfo> getEnabledInputMethodList() {
630 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900631 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 }
634
satokbb4aa062011-01-19 21:40:27 +0900635 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
636 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
637 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
638 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
639 for (InputMethodInfo imi: getEnabledInputMethodList()) {
640 enabledInputMethodAndSubtypes.put(
641 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
642 }
643 return enabledInputMethodAndSubtypes;
644 }
645
646 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
647 boolean allowsImplicitlySelectedSubtypes) {
648 if (imi == null && mCurMethodId != null) {
649 imi = mMethodMap.get(mCurMethodId);
650 }
satok7265d9b2011-02-14 15:47:30 +0900651 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900652 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900653 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
654 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900655 }
satok7265d9b2011-02-14 15:47:30 +0900656 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900657 }
658
satoke7c6998e2011-06-03 17:57:59 +0900659 @Override
satok16331c82010-12-20 23:48:46 +0900660 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
661 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900662 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900663 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900664 }
665 }
666
satoke7c6998e2011-06-03 17:57:59 +0900667 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 public void addClient(IInputMethodClient client,
669 IInputContext inputContext, int uid, int pid) {
670 synchronized (mMethodMap) {
671 mClients.put(client.asBinder(), new ClientState(client,
672 inputContext, uid, pid));
673 }
674 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800675
satoke7c6998e2011-06-03 17:57:59 +0900676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public void removeClient(IInputMethodClient client) {
678 synchronized (mMethodMap) {
679 mClients.remove(client.asBinder());
680 }
681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 void executeOrSendMessage(IInterface target, Message msg) {
684 if (target.asBinder() instanceof Binder) {
685 mCaller.sendMessage(msg);
686 } else {
687 handleMessage(msg);
688 msg.recycle();
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700692 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800694 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 + mCurClient.client.asBinder());
696 if (mBoundToMethod) {
697 mBoundToMethod = false;
698 if (mCurMethod != null) {
699 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
700 MSG_UNBIND_INPUT, mCurMethod));
701 }
702 }
703 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
704 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
705 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 // Call setActive(false) on the old client
708 try {
709 mCurClient.client.setActive(false);
710 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800711 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 + mCurClient.pid + " uid " + mCurClient.uid);
713 }
714 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project10592532009-03-18 17:39:46 -0700716 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 private int getImeShowFlags() {
721 int flags = 0;
722 if (mShowForced) {
723 flags |= InputMethod.SHOW_FORCED
724 | InputMethod.SHOW_EXPLICIT;
725 } else if (mShowExplicitlyRequested) {
726 flags |= InputMethod.SHOW_EXPLICIT;
727 }
728 return flags;
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 private int getAppShowFlags() {
732 int flags = 0;
733 if (mShowForced) {
734 flags |= InputMethodManager.SHOW_FORCED;
735 } else if (!mShowExplicitlyRequested) {
736 flags |= InputMethodManager.SHOW_IMPLICIT;
737 }
738 return flags;
739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
742 if (!mBoundToMethod) {
743 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
744 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
745 mBoundToMethod = true;
746 }
747 final SessionState session = mCurClient.curSession;
748 if (initial) {
749 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
750 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
751 } else {
752 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
753 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
754 }
755 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800756 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800757 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 return needResult
760 ? new InputBindResult(session.session, mCurId, mCurSeq)
761 : null;
762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 InputBindResult startInputLocked(IInputMethodClient client,
765 IInputContext inputContext, EditorInfo attribute,
766 boolean initial, boolean needResult) {
767 // If no method is currently selected, do nothing.
768 if (mCurMethodId == null) {
769 return mNoBinding;
770 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 ClientState cs = mClients.get(client.asBinder());
773 if (cs == null) {
774 throw new IllegalArgumentException("unknown client "
775 + client.asBinder());
776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 try {
779 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
780 // Check with the window manager to make sure this client actually
781 // has a window with focus. If not, reject. This is thread safe
782 // because if the focus changes some time before or after, the
783 // next client receiving focus that has any interest in input will
784 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800785 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
787 return null;
788 }
789 } catch (RemoteException e) {
790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (mCurClient != cs) {
793 // If the client is changing, we need to switch over to the new
794 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700795 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800796 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 + cs.client.asBinder());
798
799 // If the screen is on, inform the new client it is active
800 if (mScreenOn) {
801 try {
802 cs.client.setActive(mScreenOn);
803 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800804 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 + cs.pid + " uid " + cs.uid);
806 }
807 }
808 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 // Bump up the sequence for this client and attach it.
811 mCurSeq++;
812 if (mCurSeq <= 0) mCurSeq = 1;
813 mCurClient = cs;
814 mCurInputContext = inputContext;
815 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 // Check if the input method is changing.
818 if (mCurId != null && mCurId.equals(mCurMethodId)) {
819 if (cs.curSession != null) {
820 // Fast case: if we are already connected to the input method,
821 // then just return it.
822 return attachNewInputLocked(initial, needResult);
823 }
824 if (mHaveConnection) {
825 if (mCurMethod != null) {
826 if (!cs.sessionRequested) {
827 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
830 MSG_CREATE_SESSION, mCurMethod,
831 new MethodCallback(mCurMethod)));
832 }
833 // Return to client, and we will get back with it when
834 // we have had a session made for it.
835 return new InputBindResult(null, mCurId, mCurSeq);
836 } else if (SystemClock.uptimeMillis()
837 < (mLastBindTime+TIME_TO_RECONNECT)) {
838 // In this case we have connected to the service, but
839 // don't yet have its interface. If it hasn't been too
840 // long since we did the connection, we'll return to
841 // the client and wait to get the service interface so
842 // we can report back. If it has been too long, we want
843 // to fall through so we can try a disconnect/reconnect
844 // to see if we can get back in touch with the service.
845 return new InputBindResult(null, mCurId, mCurSeq);
846 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800847 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
848 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850 }
851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700853 return startInputInnerLocked();
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700856 InputBindResult startInputInnerLocked() {
857 if (mCurMethodId == null) {
858 return mNoBinding;
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 if (!mSystemReady) {
862 // If the system is not yet ready, we shouldn't be running third
863 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 InputMethodInfo info = mMethodMap.get(mCurMethodId);
868 if (info == null) {
869 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700872 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
875 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700876 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
877 com.android.internal.R.string.input_method_binding_label);
878 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
879 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
881 mLastBindTime = SystemClock.uptimeMillis();
882 mHaveConnection = true;
883 mCurId = info.getId();
884 mCurToken = new Binder();
885 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800886 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 mIWindowManager.addWindowToken(mCurToken,
888 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
889 } catch (RemoteException e) {
890 }
891 return new InputBindResult(null, mCurId, mCurSeq);
892 } else {
893 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 + mCurIntent);
896 }
897 return null;
898 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800899
satoke7c6998e2011-06-03 17:57:59 +0900900 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public InputBindResult startInput(IInputMethodClient client,
902 IInputContext inputContext, EditorInfo attribute,
903 boolean initial, boolean needResult) {
904 synchronized (mMethodMap) {
905 final long ident = Binder.clearCallingIdentity();
906 try {
907 return startInputLocked(client, inputContext, attribute,
908 initial, needResult);
909 } finally {
910 Binder.restoreCallingIdentity(ident);
911 }
912 }
913 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800914
satoke7c6998e2011-06-03 17:57:59 +0900915 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public void finishInput(IInputMethodClient client) {
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
satoke7c6998e2011-06-03 17:57:59 +0900919 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void onServiceConnected(ComponentName name, IBinder service) {
921 synchronized (mMethodMap) {
922 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
923 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700924 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800925 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700926 unbindCurrentMethodLocked(false);
927 return;
928 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700930 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
931 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800933 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700934 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700936 MSG_CREATE_SESSION, mCurMethod,
937 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939 }
940 }
941 }
942
943 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
944 synchronized (mMethodMap) {
945 if (mCurMethod != null && method != null
946 && mCurMethod.asBinder() == method.asBinder()) {
947 if (mCurClient != null) {
948 mCurClient.curSession = new SessionState(mCurClient,
949 method, session);
950 mCurClient.sessionRequested = false;
951 InputBindResult res = attachNewInputLocked(true, true);
952 if (res.method != null) {
953 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
954 MSG_BIND_METHOD, mCurClient.client, res));
955 }
956 }
957 }
958 }
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700961 void unbindCurrentMethodLocked(boolean reportToClient) {
962 if (mHaveConnection) {
963 mContext.unbindService(this);
964 mHaveConnection = false;
965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 if (mCurToken != null) {
968 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800969 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 mIWindowManager.removeWindowToken(mCurToken);
971 } catch (RemoteException e) {
972 }
973 mCurToken = null;
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Project10592532009-03-18 17:39:46 -0700976 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 if (reportToClient && mCurClient != null) {
980 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
981 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
982 }
983 }
984
Devin Taylor0c33ed22010-02-23 13:26:46 -0600985 private void finishSession(SessionState sessionState) {
986 if (sessionState != null && sessionState.session != null) {
987 try {
988 sessionState.session.finishSession();
989 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700990 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600991 }
992 }
993 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800994
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700995 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (mCurMethod != null) {
997 for (ClientState cs : mClients.values()) {
998 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -0600999 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 cs.curSession = null;
1001 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001002
1003 finishSession(mEnabledSession);
1004 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 mCurMethod = null;
1006 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001007 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001009
satoke7c6998e2011-06-03 17:57:59 +09001010 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 public void onServiceDisconnected(ComponentName name) {
1012 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001013 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 + " mCurIntent=" + mCurIntent);
1015 if (mCurMethod != null && mCurIntent != null
1016 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001017 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 // We consider this to be a new bind attempt, since the system
1019 // should now try to restart the service for us.
1020 mLastBindTime = SystemClock.uptimeMillis();
1021 mShowRequested = mInputShown;
1022 mInputShown = false;
1023 if (mCurClient != null) {
1024 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1025 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1026 }
1027 }
1028 }
1029 }
1030
satokf9f01002011-05-19 21:31:50 +09001031 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001033 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 long ident = Binder.clearCallingIdentity();
1035 try {
1036 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001037 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 return;
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 synchronized (mMethodMap) {
1042 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001043 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001044 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001046 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001047 mStatusBar.setIcon("ime", packageName, iconId, 0);
1048 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 }
1051 } finally {
1052 Binder.restoreCallingIdentity(ident);
1053 }
1054 }
1055
satok7cfc0ed2011-06-20 21:29:36 +09001056 private boolean needsToShowImeSwitchOngoingNotification() {
1057 if (!mShowOngoingImeSwitcherForPhones) return false;
1058 synchronized (mMethodMap) {
1059 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1060 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001061 if (N > 2) return true;
1062 if (N < 1) return false;
1063 int nonAuxCount = 0;
1064 int auxCount = 0;
1065 InputMethodSubtype nonAuxSubtype = null;
1066 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001067 for(int i = 0; i < N; ++i) {
1068 final InputMethodInfo imi = imis.get(i);
1069 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1070 imi, true);
1071 final int subtypeCount = subtypes.size();
1072 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001073 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001074 } else {
1075 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001076 final InputMethodSubtype subtype = subtypes.get(j);
1077 if (!subtype.isAuxiliary()) {
1078 ++nonAuxCount;
1079 nonAuxSubtype = subtype;
1080 } else {
1081 ++auxCount;
1082 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001083 }
1084 }
1085 }
satok7cfc0ed2011-06-20 21:29:36 +09001086 }
satokb6359412011-06-28 17:47:41 +09001087 if (nonAuxCount > 1 || auxCount > 1) {
1088 return true;
1089 } else if (nonAuxCount == 1 && auxCount == 1) {
1090 if (nonAuxSubtype != null && auxSubtype != null
1091 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1092 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1093 return false;
1094 }
1095 return true;
1096 }
1097 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001098 }
satok7cfc0ed2011-06-20 21:29:36 +09001099 }
1100
satokf9f01002011-05-19 21:31:50 +09001101 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001102 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001103 int uid = Binder.getCallingUid();
1104 long ident = Binder.clearCallingIdentity();
1105 try {
1106 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001107 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001108 return;
1109 }
1110
1111 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001112 mImeWindowVis = vis;
1113 mBackDisposition = backDisposition;
1114 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001115 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1116 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1117 final PackageManager pm = mContext.getPackageManager();
1118 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1119 final CharSequence title = mRes.getText(
1120 com.android.internal.R.string.select_input_method);
1121 mImeSwitcherNotification.setLatestEventInfo(
1122 mContext, title, label, mImeSwitchPendingIntent);
1123 mNotificationManager.notify(
1124 com.android.internal.R.string.select_input_method,
1125 mImeSwitcherNotification);
1126 mNotificationShown = true;
1127 } else {
1128 if (mNotificationShown) {
1129 mNotificationManager.cancel(
1130 com.android.internal.R.string.select_input_method);
1131 mNotificationShown = false;
1132 }
1133 }
satok06487a52010-10-29 11:37:18 +09001134 }
1135 } finally {
1136 Binder.restoreCallingIdentity(ident);
1137 }
1138 }
1139
satoke7c6998e2011-06-03 17:57:59 +09001140 @Override
satokf9f01002011-05-19 21:31:50 +09001141 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1142 synchronized (mMethodMap) {
1143 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1144 for (int i = 0; i < spans.length; ++i) {
1145 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001146 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001147 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001148 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001149 }
1150 }
1151 }
1152 }
1153
satoke7c6998e2011-06-03 17:57:59 +09001154 @Override
satokf9f01002011-05-19 21:31:50 +09001155 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1156 synchronized (mMethodMap) {
1157 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1158 // TODO: Do not send the intent if the process of the targetImi is already dead.
1159 if (targetImi != null) {
1160 final String[] suggestions = span.getSuggestions();
1161 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001162 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001163 final Intent intent = new Intent();
1164 // Ensures that only a class in the original IME package will receive the
1165 // notification.
satok42c5a162011-05-26 16:46:14 +09001166 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001167 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1168 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1169 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1170 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1171 mContext.sendBroadcast(intent);
1172 return true;
1173 }
1174 }
1175 return false;
1176 }
1177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001179 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1180 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1181 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1182 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001184 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001185 // There is no input method selected, try to choose new applicable input method.
1186 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1187 id = Settings.Secure.getString(mContext.getContentResolver(),
1188 Settings.Secure.DEFAULT_INPUT_METHOD);
1189 }
1190 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 try {
satokab751aa2010-09-14 19:17:36 +09001192 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001194 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001195 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001196 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
satokf3db1af2010-11-23 13:34:33 +09001198 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001199 } else {
1200 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001201 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001202 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 }
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
satokab751aa2010-09-14 19:17:36 +09001206 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 InputMethodInfo info = mMethodMap.get(id);
1208 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001209 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001213 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001214 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1215 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001216 }
1217 if (subtype != mCurrentSubtype) {
1218 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001219 if (subtype != null) {
1220 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1221 }
satokcd7cd292010-11-20 15:46:23 +09001222 if (mCurMethod != null) {
1223 try {
satoke40dea02011-01-30 01:14:02 +09001224 final Configuration conf = mRes.getConfiguration();
1225 final boolean haveHardKeyboard = conf.keyboard
1226 != Configuration.KEYBOARD_NOKEYS;
1227 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001228 && conf.hardKeyboardHidden
1229 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001230 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1231 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1232 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001233 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1234 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001235 // If subtype is null, try to find the most applicable one from
1236 // getCurrentInputMethodSubtype.
1237 if (subtype == null) {
1238 subtype = getCurrentInputMethodSubtype();
1239 }
1240 mCurMethod.changeInputMethodSubtype(subtype);
1241 } catch (RemoteException e) {
1242 return;
satokab751aa2010-09-14 19:17:36 +09001243 }
1244 }
1245 }
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 return;
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 final long ident = Binder.clearCallingIdentity();
1251 try {
satokab751aa2010-09-14 19:17:36 +09001252 // Set a subtype to this input method.
1253 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001254 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1255 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1256 // because mCurMethodId is stored as a history in
1257 // setSelectedInputMethodAndSubtypeLocked().
1258 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259
1260 if (ActivityManagerNative.isSystemReady()) {
1261 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001262 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 intent.putExtra("input_method_id", id);
1264 mContext.sendBroadcast(intent);
1265 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001266 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 } finally {
1268 Binder.restoreCallingIdentity(ident);
1269 }
1270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001271
satok42c5a162011-05-26 16:46:14 +09001272 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001273 public boolean showSoftInput(IInputMethodClient client, int flags,
1274 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001275 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 long ident = Binder.clearCallingIdentity();
1277 try {
1278 synchronized (mMethodMap) {
1279 if (mCurClient == null || client == null
1280 || mCurClient.client.asBinder() != client.asBinder()) {
1281 try {
1282 // We need to check if this is the current client with
1283 // focus in the window manager, to allow this call to
1284 // be made before input is started in it.
1285 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001286 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001290 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001293
Joe Onorato8a9b2202010-02-26 18:56:32 -08001294 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001295 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297 } finally {
1298 Binder.restoreCallingIdentity(ident);
1299 }
1300 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
The Android Open Source Project4df24232009-03-05 14:34:35 -08001302 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 mShowRequested = true;
1304 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1305 mShowExplicitlyRequested = true;
1306 }
1307 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1308 mShowExplicitlyRequested = true;
1309 mShowForced = true;
1310 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001311
Dianne Hackborncc278702009-09-02 23:07:23 -07001312 if (!mSystemReady) {
1313 return false;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Project4df24232009-03-05 14:34:35 -08001316 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001318 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1319 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1320 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001322 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 } else if (mHaveConnection && SystemClock.uptimeMillis()
1324 < (mLastBindTime+TIME_TO_RECONNECT)) {
1325 // The client has asked to have the input method shown, but
1326 // we have been sitting here too long with a connection to the
1327 // service and no interface received, so let's disconnect/connect
1328 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 SystemClock.uptimeMillis()-mLastBindTime,1);
1331 mContext.unbindService(this);
1332 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Project4df24232009-03-05 14:34:35 -08001335 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
satok42c5a162011-05-26 16:46:14 +09001338 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001339 public boolean hideSoftInput(IInputMethodClient client, int flags,
1340 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001341 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 long ident = Binder.clearCallingIdentity();
1343 try {
1344 synchronized (mMethodMap) {
1345 if (mCurClient == null || client == null
1346 || mCurClient.client.asBinder() != client.asBinder()) {
1347 try {
1348 // We need to check if this is the current client with
1349 // focus in the window manager, to allow this call to
1350 // be made before input is started in it.
1351 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001352 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1353 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001354 mImeWindowVis = 0;
1355 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1356 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001360 mImeWindowVis = 0;
1361 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001362 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
Joe Onorato8a9b2202010-02-26 18:56:32 -08001366 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001367 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369 } finally {
1370 Binder.restoreCallingIdentity(ident);
1371 }
1372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
The Android Open Source Project4df24232009-03-05 14:34:35 -08001374 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1376 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001384 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1389 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1390 res = true;
1391 } else {
1392 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 mInputShown = false;
1395 mShowRequested = false;
1396 mShowExplicitlyRequested = false;
1397 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001400
satok42c5a162011-05-26 16:46:14 +09001401 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001402 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1404 boolean first, int windowFlags) {
1405 long ident = Binder.clearCallingIdentity();
1406 try {
1407 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 + " viewHasFocus=" + viewHasFocus
1410 + " isTextEditor=" + isTextEditor
1411 + " softInputMode=#" + Integer.toHexString(softInputMode)
1412 + " first=" + first + " flags=#"
1413 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 if (mCurClient == null || client == null
1416 || mCurClient.client.asBinder() != client.asBinder()) {
1417 try {
1418 // We need to check if this is the current client with
1419 // focus in the window manager, to allow this call to
1420 // be made before input is started in it.
1421 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001422 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 return;
1424 }
1425 } catch (RemoteException e) {
1426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001429 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001430 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001431 return;
1432 }
1433 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001435 // Should we auto-show the IME even if the caller has not
1436 // specified what should be done with it?
1437 // We only do this automatically if the window can resize
1438 // to accommodate the IME (so what the user sees will give
1439 // them good context without input information being obscured
1440 // by the IME) or if running on a large screen where there
1441 // is more room for the target window + IME.
1442 final boolean doAutoShow =
1443 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1444 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1445 || mRes.getConfiguration().isLayoutSizeAtLeast(
1446 Configuration.SCREENLAYOUT_SIZE_LARGE);
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1449 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001450 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1452 // There is no focus view, and this window will
1453 // be behind any soft input window, so hide the
1454 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001456 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001458 } else if (isTextEditor && doAutoShow && (softInputMode &
1459 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 // There is a focus view, and we are navigating forward
1461 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001462 // We only do this automatically if the window an resize
1463 // to accomodate the IME (so what the user sees will give
1464 // them good context without input information being obscured
1465 // by the IME) or if running on a large screen where there
1466 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
1470 break;
1471 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1472 // Do nothing.
1473 break;
1474 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1475 if ((softInputMode &
1476 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001477 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001478 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
1480 break;
1481 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001482 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001483 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 break;
1485 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1486 if ((softInputMode &
1487 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001489 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
1491 break;
1492 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001493 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001494 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 break;
1496 }
1497 }
1498 } finally {
1499 Binder.restoreCallingIdentity(ident);
1500 }
1501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
satok42c5a162011-05-26 16:46:14 +09001503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1505 synchronized (mMethodMap) {
1506 if (mCurClient == null || client == null
1507 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001508 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001509 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511
satok440aab52010-11-25 09:43:11 +09001512 // Always call subtype picker, because subtype picker is a superset of input method
1513 // picker.
satokab751aa2010-09-14 19:17:36 +09001514 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1515 }
1516 }
1517
satok42c5a162011-05-26 16:46:14 +09001518 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001520 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1521 }
1522
satok42c5a162011-05-26 16:46:14 +09001523 @Override
satok28203512010-11-24 11:06:49 +09001524 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1525 synchronized (mMethodMap) {
1526 if (subtype != null) {
1527 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1528 mMethodMap.get(id), subtype.hashCode()));
1529 } else {
1530 setInputMethod(token, id);
1531 }
1532 }
satokab751aa2010-09-14 19:17:36 +09001533 }
1534
satok42c5a162011-05-26 16:46:14 +09001535 @Override
satokb416a712010-11-25 20:42:14 +09001536 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001537 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001538 synchronized (mMethodMap) {
1539 if (mCurClient == null || client == null
1540 || mCurClient.client.asBinder() != client.asBinder()) {
1541 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1542 }
satok7fee71f2010-12-17 18:54:26 +09001543 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1544 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001545 }
1546 }
1547
satok4fc87d62011-05-20 16:13:43 +09001548 @Override
satok735cf382010-11-11 20:40:09 +09001549 public boolean switchToLastInputMethod(IBinder token) {
1550 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001551 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001552 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001553 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001554 lastImi = mMethodMap.get(lastIme.first);
1555 } else {
1556 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001557 }
satok4fc87d62011-05-20 16:13:43 +09001558 String targetLastImiId = null;
1559 int subtypeId = NOT_A_SUBTYPE_ID;
1560 if (lastIme != null && lastImi != null) {
1561 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1562 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1563 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1564 : mCurrentSubtype.hashCode();
1565 // If the last IME is the same as the current IME and the last subtype is not
1566 // defined, there is no need to switch to the last IME.
1567 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1568 targetLastImiId = lastIme.first;
1569 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1570 }
1571 }
1572
1573 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1574 // This is a safety net. If the currentSubtype can't be added to the history
1575 // and the framework couldn't find the last ime, we will make the last ime be
1576 // the most applicable enabled keyboard subtype of the system imes.
1577 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1578 if (enabled != null) {
1579 final int N = enabled.size();
1580 final String locale = mCurrentSubtype == null
1581 ? mRes.getConfiguration().locale.toString()
1582 : mCurrentSubtype.getLocale();
1583 for (int i = 0; i < N; ++i) {
1584 final InputMethodInfo imi = enabled.get(i);
1585 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1586 InputMethodSubtype keyboardSubtype =
1587 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1588 SUBTYPE_MODE_KEYBOARD, locale, true);
1589 if (keyboardSubtype != null) {
1590 targetLastImiId = imi.getId();
1591 subtypeId = getSubtypeIdFromHashCode(
1592 imi, keyboardSubtype.hashCode());
1593 if(keyboardSubtype.getLocale().equals(locale)) {
1594 break;
1595 }
1596 }
1597 }
1598 }
1599 }
1600 }
1601
1602 if (!TextUtils.isEmpty(targetLastImiId)) {
1603 if (DEBUG) {
1604 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1605 + ", from: " + mCurMethodId + ", " + subtypeId);
1606 }
1607 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1608 return true;
1609 } else {
1610 return false;
1611 }
satok735cf382010-11-11 20:40:09 +09001612 }
1613 }
1614
satoke7c6998e2011-06-03 17:57:59 +09001615 @Override
satok68f1b782011-04-11 14:26:04 +09001616 public InputMethodSubtype getLastInputMethodSubtype() {
1617 synchronized (mMethodMap) {
1618 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1619 // TODO: Handle the case of the last IME with no subtypes
1620 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1621 || TextUtils.isEmpty(lastIme.second)) return null;
1622 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1623 if (lastImi == null) return null;
1624 try {
1625 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001626 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1627 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1628 return null;
1629 }
1630 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001631 } catch (NumberFormatException e) {
1632 return null;
1633 }
1634 }
1635 }
1636
satoke7c6998e2011-06-03 17:57:59 +09001637 @Override
1638 public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) {
1639 if (token == null || mCurToken != token) {
1640 return false;
1641 }
1642 if (subtypes == null || subtypes.length == 0) return false;
1643 synchronized (mMethodMap) {
1644 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1645 if (imi == null) return false;
1646 final int N = subtypes.length;
satok4a28bde2011-06-29 21:03:40 +09001647 mFileManager.addInputMethodSubtypes(imi, subtypes);
satoke7c6998e2011-06-03 17:57:59 +09001648 buildInputMethodListLocked(mMethodList, mMethodMap);
1649 return true;
1650 }
1651 }
1652
satok28203512010-11-24 11:06:49 +09001653 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 synchronized (mMethodMap) {
1655 if (token == null) {
1656 if (mContext.checkCallingOrSelfPermission(
1657 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1658 != PackageManager.PERMISSION_GRANTED) {
1659 throw new SecurityException(
1660 "Using null token requires permission "
1661 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1662 }
1663 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001664 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1665 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 return;
1667 }
1668
1669 long ident = Binder.clearCallingIdentity();
1670 try {
satokab751aa2010-09-14 19:17:36 +09001671 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 } finally {
1673 Binder.restoreCallingIdentity(ident);
1674 }
1675 }
1676 }
1677
satok42c5a162011-05-26 16:46:14 +09001678 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 public void hideMySoftInput(IBinder token, int flags) {
1680 synchronized (mMethodMap) {
1681 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001682 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1683 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 return;
1685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 long ident = Binder.clearCallingIdentity();
1687 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001688 hideCurrentInputLocked(flags, null);
1689 } finally {
1690 Binder.restoreCallingIdentity(ident);
1691 }
1692 }
1693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001694
satok42c5a162011-05-26 16:46:14 +09001695 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 public void showMySoftInput(IBinder token, int flags) {
1697 synchronized (mMethodMap) {
1698 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001699 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1700 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001701 return;
1702 }
1703 long ident = Binder.clearCallingIdentity();
1704 try {
1705 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 } finally {
1707 Binder.restoreCallingIdentity(ident);
1708 }
1709 }
1710 }
1711
1712 void setEnabledSessionInMainThread(SessionState session) {
1713 if (mEnabledSession != session) {
1714 if (mEnabledSession != null) {
1715 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001716 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 mEnabledSession.method.setSessionEnabled(
1718 mEnabledSession.session, false);
1719 } catch (RemoteException e) {
1720 }
1721 }
1722 mEnabledSession = session;
1723 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001724 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 session.method.setSessionEnabled(
1726 session.session, true);
1727 } catch (RemoteException e) {
1728 }
1729 }
1730 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001731
satok42c5a162011-05-26 16:46:14 +09001732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 public boolean handleMessage(Message msg) {
1734 HandlerCaller.SomeArgs args;
1735 switch (msg.what) {
1736 case MSG_SHOW_IM_PICKER:
1737 showInputMethodMenu();
1738 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001739
satokab751aa2010-09-14 19:17:36 +09001740 case MSG_SHOW_IM_SUBTYPE_PICKER:
1741 showInputMethodSubtypeMenu();
1742 return true;
1743
satok47a44912010-10-06 16:03:58 +09001744 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001745 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001746 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001747 return true;
1748
1749 case MSG_SHOW_IM_CONFIG:
1750 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001751 return true;
1752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 case MSG_UNBIND_INPUT:
1756 try {
1757 ((IInputMethod)msg.obj).unbindInput();
1758 } catch (RemoteException e) {
1759 // There is nothing interesting about the method dying.
1760 }
1761 return true;
1762 case MSG_BIND_INPUT:
1763 args = (HandlerCaller.SomeArgs)msg.obj;
1764 try {
1765 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1766 } catch (RemoteException e) {
1767 }
1768 return true;
1769 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001772 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1773 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 } catch (RemoteException e) {
1775 }
1776 return true;
1777 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001778 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 ((IInputMethod)args.arg1).hideSoftInput(0,
1781 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 } catch (RemoteException e) {
1783 }
1784 return true;
1785 case MSG_ATTACH_TOKEN:
1786 args = (HandlerCaller.SomeArgs)msg.obj;
1787 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001788 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1790 } catch (RemoteException e) {
1791 }
1792 return true;
1793 case MSG_CREATE_SESSION:
1794 args = (HandlerCaller.SomeArgs)msg.obj;
1795 try {
1796 ((IInputMethod)args.arg1).createSession(
1797 (IInputMethodCallback)args.arg2);
1798 } catch (RemoteException e) {
1799 }
1800 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 case MSG_START_INPUT:
1804 args = (HandlerCaller.SomeArgs)msg.obj;
1805 try {
1806 SessionState session = (SessionState)args.arg1;
1807 setEnabledSessionInMainThread(session);
1808 session.method.startInput((IInputContext)args.arg2,
1809 (EditorInfo)args.arg3);
1810 } catch (RemoteException e) {
1811 }
1812 return true;
1813 case MSG_RESTART_INPUT:
1814 args = (HandlerCaller.SomeArgs)msg.obj;
1815 try {
1816 SessionState session = (SessionState)args.arg1;
1817 setEnabledSessionInMainThread(session);
1818 session.method.restartInput((IInputContext)args.arg2,
1819 (EditorInfo)args.arg3);
1820 } catch (RemoteException e) {
1821 }
1822 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 case MSG_UNBIND_METHOD:
1827 try {
1828 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1829 } catch (RemoteException e) {
1830 // There is nothing interesting about the last client dying.
1831 }
1832 return true;
1833 case MSG_BIND_METHOD:
1834 args = (HandlerCaller.SomeArgs)msg.obj;
1835 try {
1836 ((IInputMethodClient)args.arg1).onBindMethod(
1837 (InputBindResult)args.arg2);
1838 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001839 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841 return true;
1842 }
1843 return false;
1844 }
1845
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001846 private boolean isSystemIme(InputMethodInfo inputMethod) {
1847 return (inputMethod.getServiceInfo().applicationInfo.flags
1848 & ApplicationInfo.FLAG_SYSTEM) != 0;
1849 }
1850
Ken Wakasa586f0512011-01-20 22:31:01 +09001851 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1852 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1853 final int subtypeCount = imi.getSubtypeCount();
1854 for (int i = 0; i < subtypeCount; ++i) {
1855 subtypes.add(imi.getSubtypeAt(i));
1856 }
1857 return subtypes;
1858 }
1859
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001860 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001861 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001862 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001863 // We'd prefer to fall back on a system IME, since that is safer.
1864 int i=enabled.size();
1865 while (i > 0) {
1866 i--;
1867 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1868 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1869 break;
1870 }
1871 }
satokab751aa2010-09-14 19:17:36 +09001872 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001873 if (DEBUG) {
1874 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1875 }
satok723a27e2010-11-11 14:58:11 +09001876 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001877 return true;
1878 }
1879
1880 return false;
1881 }
1882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1884 HashMap<String, InputMethodInfo> map) {
1885 list.clear();
1886 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001889 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001890 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1891 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1892 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1893 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894
1895 List<ResolveInfo> services = pm.queryIntentServices(
1896 new Intent(InputMethod.SERVICE_INTERFACE),
1897 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001898
satoke7c6998e2011-06-03 17:57:59 +09001899 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1900 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 for (int i = 0; i < services.size(); ++i) {
1902 ResolveInfo ri = services.get(i);
1903 ServiceInfo si = ri.serviceInfo;
1904 ComponentName compName = new ComponentName(si.packageName, si.name);
1905 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1906 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001907 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 + ": it does not require the permission "
1909 + android.Manifest.permission.BIND_INPUT_METHOD);
1910 continue;
1911 }
1912
Joe Onorato8a9b2202010-02-26 18:56:32 -08001913 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914
1915 try {
satoke7c6998e2011-06-03 17:57:59 +09001916 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001918 final String id = p.getId();
1919 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920
Amith Yamasanie861ec12010-03-24 21:39:27 -07001921 // System IMEs are enabled by default, unless there's a hard keyboard
1922 // and the system IME was explicitly disabled
1923 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1924 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001925 }
1926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001928 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001934 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001937
1938 String defaultIme = Settings.Secure.getString(mContext
1939 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001940 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001941 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001942 updateFromSettingsLocked();
1943 }
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948
satokab751aa2010-09-14 19:17:36 +09001949 private void showInputMethodMenu() {
1950 showInputMethodMenuInternal(false);
1951 }
1952
1953 private void showInputMethodSubtypeMenu() {
1954 showInputMethodMenuInternal(true);
1955 }
1956
satok217f5482010-12-15 05:19:19 +09001957 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001958 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001959 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001960 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1961 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001962 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001963 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001964 }
satok217f5482010-12-15 05:19:19 +09001965 mContext.startActivity(intent);
1966 }
1967
1968 private void showConfigureInputMethods() {
1969 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1970 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1971 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1972 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001973 mContext.startActivity(intent);
1974 }
1975
satokab751aa2010-09-14 19:17:36 +09001976 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 String lastInputMethodId = Settings.Secure.getString(context
1984 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001985 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001986 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001987
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001988 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001989 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1990 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001991 if (immis == null || immis.size() == 0) {
1992 return;
1993 }
1994
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001995 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996
Ken Wakasa761eb372011-03-04 19:06:18 +09001997 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1998 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
1999 new Comparator<InputMethodInfo>() {
2000 @Override
2001 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2002 if (imi2 == null) return 0;
2003 if (imi1 == null) return 1;
2004 if (pm == null) {
2005 return imi1.getId().compareTo(imi2.getId());
2006 }
2007 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2008 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2009 return imiId1.toString().compareTo(imiId2.toString());
2010 }
2011 });
satok913a8922010-08-26 21:53:41 +09002012
Ken Wakasa761eb372011-03-04 19:06:18 +09002013 sortedImmis.putAll(immis);
2014
2015 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2016 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2017
2018 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002019 if (imi == null) continue;
2020 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002021 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002022 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2023 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002024 }
satokbb4aa062011-01-19 21:40:27 +09002025 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002026 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002027 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002028 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002029 if (DEBUG) {
2030 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2031 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002032 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002033 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2034 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2035 // We show all enabled IMEs and subtypes when an IME is shown.
2036 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002037 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002038 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002039 final String mode = subtype.getMode();
2040 title = TextUtils.concat(subtype.getDisplayName(context,
2041 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2042 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002043 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2044 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002045 // Removing this subtype from enabledSubtypeSet because we no longer
2046 // need to add an entry of this subtype to imList to avoid duplicated
2047 // entries.
2048 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002049 }
satokab751aa2010-09-14 19:17:36 +09002050 }
2051 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002052 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2053 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002054 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002055 }
satok913a8922010-08-26 21:53:41 +09002056
Ken Wakasa761eb372011-03-04 19:06:18 +09002057 final int N = imList.size();
2058 mItems = new CharSequence[N];
2059 for (int i = 0; i < N; ++i) {
2060 mItems[i] = imList.get(i).first;
2061 }
satokab751aa2010-09-14 19:17:36 +09002062 mIms = new InputMethodInfo[N];
2063 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002064 int checkedItem = 0;
2065 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002066 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002067 mIms[i] = value.first;
2068 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002069 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002070 int subtypeId = mSubtypeIds[i];
2071 if ((subtypeId == NOT_A_SUBTYPE_ID)
2072 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2073 || (subtypeId == lastInputMethodSubtypeId)) {
2074 checkedItem = i;
2075 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
satokab751aa2010-09-14 19:17:36 +09002078
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002079 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002080 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002081 public void onClick(DialogInterface dialog, int which) {
2082 hideInputMethodMenu();
2083 }
2084 };
satokd87c2592010-09-29 11:52:06 +09002085
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002086 TypedArray a = context.obtainStyledAttributes(null,
2087 com.android.internal.R.styleable.DialogPreference,
2088 com.android.internal.R.attr.alertDialogStyle, 0);
2089 mDialogBuilder = new AlertDialog.Builder(context)
2090 .setTitle(com.android.internal.R.string.select_input_method)
2091 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002092 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002093 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002095 }
2096 })
2097 .setIcon(a.getDrawable(
2098 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2099 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002100
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002101 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2102 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002103 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002104 public void onClick(DialogInterface dialog, int which) {
2105 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002106 if (mIms == null || mIms.length <= which
2107 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002108 return;
2109 }
2110 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002111 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002112 hideInputMethodMenu();
2113 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002114 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002115 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002116 subtypeId = NOT_A_SUBTYPE_ID;
2117 }
2118 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002119 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002122 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123
satok7f35c8c2010-10-07 21:13:11 +09002124 if (showSubtypes) {
satok82beadf2010-12-27 19:03:06 +09002125 mDialogBuilder.setPositiveButton(
2126 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002127 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002128 @Override
satok7f35c8c2010-10-07 21:13:11 +09002129 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002130 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002131 }
2132 });
2133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002135 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 mSwitchingDialog.getWindow().setType(
2137 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002138 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 mSwitchingDialog.show();
2140 }
2141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002144 synchronized (mMethodMap) {
2145 hideInputMethodMenuLocked();
2146 }
2147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002148
The Android Open Source Project10592532009-03-18 17:39:46 -07002149 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002150 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151
The Android Open Source Project10592532009-03-18 17:39:46 -07002152 if (mSwitchingDialog != null) {
2153 mSwitchingDialog.dismiss();
2154 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002156
The Android Open Source Project10592532009-03-18 17:39:46 -07002157 mDialogBuilder = null;
2158 mItems = null;
2159 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163
satok42c5a162011-05-26 16:46:14 +09002164 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 public boolean setInputMethodEnabled(String id, boolean enabled) {
2166 synchronized (mMethodMap) {
2167 if (mContext.checkCallingOrSelfPermission(
2168 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2169 != PackageManager.PERMISSION_GRANTED) {
2170 throw new SecurityException(
2171 "Requires permission "
2172 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2173 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 long ident = Binder.clearCallingIdentity();
2176 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002177 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 } finally {
2179 Binder.restoreCallingIdentity(ident);
2180 }
2181 }
2182 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002183
2184 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2185 // Make sure this is a valid input method.
2186 InputMethodInfo imm = mMethodMap.get(id);
2187 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002188 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002189 }
2190
satokd87c2592010-09-29 11:52:06 +09002191 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2192 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002193
satokd87c2592010-09-29 11:52:06 +09002194 if (enabled) {
2195 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2196 if (pair.first.equals(id)) {
2197 // We are enabling this input method, but it is already enabled.
2198 // Nothing to do. The previous state was enabled.
2199 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002200 }
2201 }
satokd87c2592010-09-29 11:52:06 +09002202 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2203 // Previous state was disabled.
2204 return false;
2205 } else {
2206 StringBuilder builder = new StringBuilder();
2207 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2208 builder, enabledInputMethodsList, id)) {
2209 // Disabled input method is currently selected, switch to another one.
2210 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2211 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002212 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2213 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2214 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002215 }
2216 // Previous state was enabled.
2217 return true;
2218 } else {
2219 // We are disabling the input method but it is already disabled.
2220 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002221 return false;
2222 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002223 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002224 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002225
satok57ffc002011-01-25 00:11:47 +09002226 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2227 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002228 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002229 }
2230
satok723a27e2010-11-11 14:58:11 +09002231 private void saveCurrentInputMethodAndSubtypeToHistory() {
2232 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2233 if (mCurrentSubtype != null) {
2234 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2235 }
satok57ffc002011-01-25 00:11:47 +09002236 if (canAddToLastInputMethod(mCurrentSubtype)) {
2237 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2238 }
satokab751aa2010-09-14 19:17:36 +09002239 }
2240
satok723a27e2010-11-11 14:58:11 +09002241 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2242 boolean setSubtypeOnly) {
2243 // Update the history of InputMethod and Subtype
2244 saveCurrentInputMethodAndSubtypeToHistory();
2245
2246 // Set Subtype here
2247 if (imi == null || subtypeId < 0) {
2248 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002249 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002250 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002251 if (subtypeId < imi.getSubtypeCount()) {
2252 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2253 mSettings.putSelectedSubtype(subtype.hashCode());
2254 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002255 } else {
2256 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2257 mCurrentSubtype = null;
2258 }
satokab751aa2010-09-14 19:17:36 +09002259 }
satok723a27e2010-11-11 14:58:11 +09002260
2261 if (!setSubtypeOnly) {
2262 // Set InputMethod here
2263 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2264 }
2265 }
2266
2267 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2268 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2269 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2270 // newDefaultIme is empty when there is no candidate for the selected IME.
2271 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2272 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2273 if (subtypeHashCode != null) {
2274 try {
2275 lastSubtypeId = getSubtypeIdFromHashCode(
2276 imi, Integer.valueOf(subtypeHashCode));
2277 } catch (NumberFormatException e) {
2278 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2279 }
2280 }
2281 }
2282 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002283 }
2284
2285 private int getSelectedInputMethodSubtypeId(String id) {
2286 InputMethodInfo imi = mMethodMap.get(id);
2287 if (imi == null) {
2288 return NOT_A_SUBTYPE_ID;
2289 }
satokab751aa2010-09-14 19:17:36 +09002290 int subtypeId;
2291 try {
2292 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2293 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2294 } catch (SettingNotFoundException e) {
2295 return NOT_A_SUBTYPE_ID;
2296 }
satok723a27e2010-11-11 14:58:11 +09002297 return getSubtypeIdFromHashCode(imi, subtypeId);
2298 }
2299
2300 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002301 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002302 final int subtypeCount = imi.getSubtypeCount();
2303 for (int i = 0; i < subtypeCount; ++i) {
2304 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002305 if (subtypeHashCode == ims.hashCode()) {
2306 return i;
2307 }
satokab751aa2010-09-14 19:17:36 +09002308 }
2309 }
2310 return NOT_A_SUBTYPE_ID;
2311 }
2312
satokdf31ae62011-01-15 06:19:44 +09002313 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2314 Resources res, List<InputMethodSubtype> subtypes) {
2315 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002316 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2317 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2318 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002319 final int N = subtypes.size();
2320 boolean containsKeyboardSubtype = false;
2321 for (int i = 0; i < N; ++i) {
2322 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002323 final String locale = subtype.getLocale();
2324 final String mode = subtype.getMode();
2325 // When system locale starts with subtype's locale, that subtype will be applicable
2326 // for system locale
2327 // For instance, it's clearly applicable for cases like system locale = en_US and
2328 // subtype = en, but it is not necessarily considered applicable for cases like system
2329 // locale = en and subtype = en_US.
2330 // We just call systemLocale.startsWith(locale) in this function because there is no
2331 // need to find applicable subtypes aggressively unlike
2332 // findLastResortApplicableSubtypeLocked.
2333 if (systemLocale.startsWith(locale)) {
2334 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2335 // If more applicable subtypes are contained, skip.
2336 if (applicableSubtype != null
2337 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2338 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002339 if (!containsKeyboardSubtype
2340 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2341 containsKeyboardSubtype = true;
2342 }
2343 }
2344 }
satok4a28bde2011-06-29 21:03:40 +09002345 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002346 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002347 if (!containsKeyboardSubtype) {
2348 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002349 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002350 if (lastResortKeyboardSubtype != null) {
2351 applicableSubtypes.add(lastResortKeyboardSubtype);
2352 }
2353 }
2354 return applicableSubtypes;
2355 }
2356
satok4e4569d2010-11-19 18:45:53 +09002357 /**
2358 * If there are no selected subtypes, tries finding the most applicable one according to the
2359 * given locale.
2360 * @param subtypes this function will search the most applicable subtype in subtypes
2361 * @param mode subtypes will be filtered by mode
2362 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002363 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2364 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002365 * @return the most applicable subtypeId
2366 */
satokdf31ae62011-01-15 06:19:44 +09002367 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2368 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002369 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002370 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002371 return null;
satok8fbb1e82010-11-02 23:15:58 +09002372 }
satok4e4569d2010-11-19 18:45:53 +09002373 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002374 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002375 }
satok8fbb1e82010-11-02 23:15:58 +09002376 final String language = locale.substring(0, 2);
2377 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002378 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002379 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002380 final int N = subtypes.size();
2381 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002382 InputMethodSubtype subtype = subtypes.get(i);
2383 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002384 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2385 // and all subtypes with all modes can be candidates.
2386 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002387 if (firstMatchedModeSubtype == null) {
2388 firstMatchedModeSubtype = subtype;
2389 }
satok9ef02832010-11-04 21:17:48 +09002390 if (locale.equals(subtypeLocale)) {
2391 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002392 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002393 break;
2394 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2395 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002396 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002397 partialMatchFound = true;
2398 }
satok8fbb1e82010-11-02 23:15:58 +09002399 }
2400 }
2401
satok7599a7f2010-12-22 13:45:23 +09002402 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2403 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002404 }
2405
satok8fbb1e82010-11-02 23:15:58 +09002406 // The first subtype applicable to the system locale will be defined as the most applicable
2407 // subtype.
2408 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002409 if (applicableSubtype != null) {
2410 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2411 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2412 }
satok8fbb1e82010-11-02 23:15:58 +09002413 }
satokcd7cd292010-11-20 15:46:23 +09002414 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002415 }
2416
satok4e4569d2010-11-19 18:45:53 +09002417 // If there are no selected shortcuts, tries finding the most applicable ones.
2418 private Pair<InputMethodInfo, InputMethodSubtype>
2419 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2420 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2421 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002422 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002423 boolean foundInSystemIME = false;
2424
2425 // Search applicable subtype for each InputMethodInfo
2426 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002427 final String imiId = imi.getId();
2428 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2429 continue;
2430 }
satokcd7cd292010-11-20 15:46:23 +09002431 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002432 final List<InputMethodSubtype> enabledSubtypes =
2433 getEnabledInputMethodSubtypeList(imi, true);
2434 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002435 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002436 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002437 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002438 }
satokdf31ae62011-01-15 06:19:44 +09002439 // 2. Search by the system locale from enabledSubtypes.
2440 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002441 if (subtype == null) {
2442 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002443 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002444 }
satok7599a7f2010-12-22 13:45:23 +09002445 // 4. Search by the current subtype's locale from all subtypes.
2446 if (subtype == null && mCurrentSubtype != null) {
2447 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002448 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002449 }
2450 // 5. Search by the system locale from all subtypes.
2451 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002452 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002453 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002454 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002455 }
satokcd7cd292010-11-20 15:46:23 +09002456 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002457 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002458 // The current input method is the most applicable IME.
2459 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002460 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002461 break;
satok7599a7f2010-12-22 13:45:23 +09002462 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002463 // The system input method is 2nd applicable IME.
2464 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002465 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002466 if ((imi.getServiceInfo().applicationInfo.flags
2467 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2468 foundInSystemIME = true;
2469 }
satok4e4569d2010-11-19 18:45:53 +09002470 }
2471 }
2472 }
2473 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002474 if (mostApplicableIMI != null) {
2475 Slog.w(TAG, "Most applicable shortcut input method was:"
2476 + mostApplicableIMI.getId());
2477 if (mostApplicableSubtype != null) {
2478 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2479 + "," + mostApplicableSubtype.getMode() + ","
2480 + mostApplicableSubtype.getLocale());
2481 }
2482 }
satok4e4569d2010-11-19 18:45:53 +09002483 }
satokcd7cd292010-11-20 15:46:23 +09002484 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002485 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002486 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002487 } else {
2488 return null;
2489 }
2490 }
2491
satokab751aa2010-09-14 19:17:36 +09002492 /**
2493 * @return Return the current subtype of this input method.
2494 */
satok42c5a162011-05-26 16:46:14 +09002495 @Override
satokab751aa2010-09-14 19:17:36 +09002496 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002497 boolean subtypeIsSelected = false;
2498 try {
2499 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2500 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2501 } catch (SettingNotFoundException e) {
2502 }
satok3ef8b292010-11-23 06:06:29 +09002503 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002504 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002505 String lastInputMethodId = Settings.Secure.getString(
2506 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002507 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2508 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002509 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2510 if (imi != null) {
2511 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002512 // the most applicable subtype from explicitly or implicitly enabled
2513 // subtypes.
2514 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2515 getEnabledInputMethodSubtypeList(imi, true);
2516 // If there is only one explicitly or implicitly enabled subtype,
2517 // just returns it.
2518 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2519 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2520 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2521 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2522 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2523 SUBTYPE_MODE_KEYBOARD, null, true);
2524 if (mCurrentSubtype == null) {
2525 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2526 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2527 true);
2528 }
2529 }
satok4e4569d2010-11-19 18:45:53 +09002530 }
satokcd7cd292010-11-20 15:46:23 +09002531 } else {
satok3ef8b292010-11-23 06:06:29 +09002532 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002533 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002534 }
satok8fbb1e82010-11-02 23:15:58 +09002535 }
satok3ef8b292010-11-23 06:06:29 +09002536 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002537 }
satokab751aa2010-09-14 19:17:36 +09002538 }
2539
satokf3db1af2010-11-23 13:34:33 +09002540 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2541 InputMethodSubtype subtype) {
2542 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2543 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2544 } else {
2545 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2546 subtypes.add(subtype);
2547 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2548 }
2549 }
2550
satok4e4569d2010-11-19 18:45:53 +09002551 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002552 @Override
satok4e4569d2010-11-19 18:45:53 +09002553 public List getShortcutInputMethodsAndSubtypes() {
2554 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002555 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002556 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002557 // If there are no selected shortcut subtypes, the framework will try to find
2558 // the most applicable subtype from all subtypes whose mode is
2559 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002560 Pair<InputMethodInfo, InputMethodSubtype> info =
2561 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2562 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002563 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002564 ret.add(info.first);
2565 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002566 }
satok3da92232011-01-11 22:46:30 +09002567 return ret;
satokf3db1af2010-11-23 13:34:33 +09002568 }
satokf3db1af2010-11-23 13:34:33 +09002569 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2570 ret.add(imi);
2571 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2572 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002573 }
2574 }
satokf3db1af2010-11-23 13:34:33 +09002575 return ret;
satok4e4569d2010-11-19 18:45:53 +09002576 }
2577 }
2578
satok42c5a162011-05-26 16:46:14 +09002579 @Override
satokb66d2872010-11-10 01:04:04 +09002580 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2581 synchronized (mMethodMap) {
2582 if (subtype != null && mCurMethodId != null) {
2583 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2584 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2585 if (subtypeId != NOT_A_SUBTYPE_ID) {
2586 setInputMethodLocked(mCurMethodId, subtypeId);
2587 return true;
2588 }
2589 }
2590 return false;
2591 }
2592 }
2593
satokd87c2592010-09-29 11:52:06 +09002594 /**
2595 * Utility class for putting and getting settings for InputMethod
2596 * TODO: Move all putters and getters of settings to this class.
2597 */
2598 private static class InputMethodSettings {
2599 // The string for enabled input method is saved as follows:
2600 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2601 private static final char INPUT_METHOD_SEPARATER = ':';
2602 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002603 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002604 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2605
satok723a27e2010-11-11 14:58:11 +09002606 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002607 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2608
satokdf31ae62011-01-15 06:19:44 +09002609 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002610 private final ContentResolver mResolver;
2611 private final HashMap<String, InputMethodInfo> mMethodMap;
2612 private final ArrayList<InputMethodInfo> mMethodList;
2613
2614 private String mEnabledInputMethodsStrCache;
2615
2616 private static void buildEnabledInputMethodsSettingString(
2617 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2618 String id = pair.first;
2619 ArrayList<String> subtypes = pair.second;
2620 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002621 // Inputmethod and subtypes are saved in the settings as follows:
2622 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2623 for (String subtypeId: subtypes) {
2624 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002625 }
2626 }
2627
2628 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002629 Resources res, ContentResolver resolver,
2630 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2631 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002632 mResolver = resolver;
2633 mMethodMap = methodMap;
2634 mMethodList = methodList;
2635 }
2636
2637 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2638 return createEnabledInputMethodListLocked(
2639 getEnabledInputMethodsAndSubtypeListLocked());
2640 }
2641
satok7f35c8c2010-10-07 21:13:11 +09002642 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002643 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2644 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002645 getEnabledInputMethodsAndSubtypeListLocked());
2646 }
2647
satok67ddf9c2010-11-17 09:45:54 +09002648 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2649 InputMethodInfo imi) {
2650 List<Pair<String, ArrayList<String>>> imsList =
2651 getEnabledInputMethodsAndSubtypeListLocked();
2652 ArrayList<InputMethodSubtype> enabledSubtypes =
2653 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002654 if (imi != null) {
2655 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2656 InputMethodInfo info = mMethodMap.get(imsPair.first);
2657 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002658 final int subtypeCount = info.getSubtypeCount();
2659 for (int i = 0; i < subtypeCount; ++i) {
2660 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002661 for (String s: imsPair.second) {
2662 if (String.valueOf(ims.hashCode()).equals(s)) {
2663 enabledSubtypes.add(ims);
2664 }
satok67ddf9c2010-11-17 09:45:54 +09002665 }
2666 }
satok884ef9a2010-11-18 10:39:46 +09002667 break;
satok67ddf9c2010-11-17 09:45:54 +09002668 }
satok67ddf9c2010-11-17 09:45:54 +09002669 }
2670 }
2671 return enabledSubtypes;
2672 }
2673
satokd87c2592010-09-29 11:52:06 +09002674 // At the initial boot, the settings for input methods are not set,
2675 // so we need to enable IME in that case.
2676 public void enableAllIMEsIfThereIsNoEnabledIME() {
2677 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2678 StringBuilder sb = new StringBuilder();
2679 final int N = mMethodList.size();
2680 for (int i = 0; i < N; i++) {
2681 InputMethodInfo imi = mMethodList.get(i);
2682 Slog.i(TAG, "Adding: " + imi.getId());
2683 if (i > 0) sb.append(':');
2684 sb.append(imi.getId());
2685 }
2686 putEnabledInputMethodsStr(sb.toString());
2687 }
2688 }
2689
satokbb4aa062011-01-19 21:40:27 +09002690 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002691 ArrayList<Pair<String, ArrayList<String>>> imsList
2692 = new ArrayList<Pair<String, ArrayList<String>>>();
2693 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2694 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2695 return imsList;
2696 }
satok723a27e2010-11-11 14:58:11 +09002697 mInputMethodSplitter.setString(enabledInputMethodsStr);
2698 while (mInputMethodSplitter.hasNext()) {
2699 String nextImsStr = mInputMethodSplitter.next();
2700 mSubtypeSplitter.setString(nextImsStr);
2701 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002702 ArrayList<String> subtypeHashes = new ArrayList<String>();
2703 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002704 String imeId = mSubtypeSplitter.next();
2705 while (mSubtypeSplitter.hasNext()) {
2706 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002707 }
2708 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2709 }
2710 }
2711 return imsList;
2712 }
2713
2714 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2715 if (reloadInputMethodStr) {
2716 getEnabledInputMethodsStr();
2717 }
2718 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2719 // Add in the newly enabled input method.
2720 putEnabledInputMethodsStr(id);
2721 } else {
2722 putEnabledInputMethodsStr(
2723 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2724 }
2725 }
2726
2727 /**
2728 * Build and put a string of EnabledInputMethods with removing specified Id.
2729 * @return the specified id was removed or not.
2730 */
2731 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2732 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2733 boolean isRemoved = false;
2734 boolean needsAppendSeparator = false;
2735 for (Pair<String, ArrayList<String>> ims: imsList) {
2736 String curId = ims.first;
2737 if (curId.equals(id)) {
2738 // We are disabling this input method, and it is
2739 // currently enabled. Skip it to remove from the
2740 // new list.
2741 isRemoved = true;
2742 } else {
2743 if (needsAppendSeparator) {
2744 builder.append(INPUT_METHOD_SEPARATER);
2745 } else {
2746 needsAppendSeparator = true;
2747 }
2748 buildEnabledInputMethodsSettingString(builder, ims);
2749 }
2750 }
2751 if (isRemoved) {
2752 // Update the setting with the new list of input methods.
2753 putEnabledInputMethodsStr(builder.toString());
2754 }
2755 return isRemoved;
2756 }
2757
2758 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2759 List<Pair<String, ArrayList<String>>> imsList) {
2760 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2761 for (Pair<String, ArrayList<String>> ims: imsList) {
2762 InputMethodInfo info = mMethodMap.get(ims.first);
2763 if (info != null) {
2764 res.add(info);
2765 }
2766 }
2767 return res;
2768 }
2769
satok7f35c8c2010-10-07 21:13:11 +09002770 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002771 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002772 List<Pair<String, ArrayList<String>>> imsList) {
2773 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2774 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2775 for (Pair<String, ArrayList<String>> ims : imsList) {
2776 InputMethodInfo info = mMethodMap.get(ims.first);
2777 if (info != null) {
2778 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2779 }
2780 }
2781 return res;
2782 }
2783
satokd87c2592010-09-29 11:52:06 +09002784 private void putEnabledInputMethodsStr(String str) {
2785 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2786 mEnabledInputMethodsStrCache = str;
2787 }
2788
2789 private String getEnabledInputMethodsStr() {
2790 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2791 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002792 if (DEBUG) {
2793 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2794 }
satokd87c2592010-09-29 11:52:06 +09002795 return mEnabledInputMethodsStrCache;
2796 }
satok723a27e2010-11-11 14:58:11 +09002797
2798 private void saveSubtypeHistory(
2799 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2800 StringBuilder builder = new StringBuilder();
2801 boolean isImeAdded = false;
2802 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2803 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2804 newSubtypeId);
2805 isImeAdded = true;
2806 }
2807 for (Pair<String, String> ime: savedImes) {
2808 String imeId = ime.first;
2809 String subtypeId = ime.second;
2810 if (TextUtils.isEmpty(subtypeId)) {
2811 subtypeId = NOT_A_SUBTYPE_ID_STR;
2812 }
2813 if (isImeAdded) {
2814 builder.append(INPUT_METHOD_SEPARATER);
2815 } else {
2816 isImeAdded = true;
2817 }
2818 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2819 subtypeId);
2820 }
2821 // Remove the last INPUT_METHOD_SEPARATER
2822 putSubtypeHistoryStr(builder.toString());
2823 }
2824
2825 public void addSubtypeToHistory(String imeId, String subtypeId) {
2826 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2827 for (Pair<String, String> ime: subtypeHistory) {
2828 if (ime.first.equals(imeId)) {
2829 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002830 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002831 + ime.second);
2832 }
2833 // We should break here
2834 subtypeHistory.remove(ime);
2835 break;
2836 }
2837 }
satokbb4aa062011-01-19 21:40:27 +09002838 if (DEBUG) {
2839 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2840 }
satok723a27e2010-11-11 14:58:11 +09002841 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2842 }
2843
2844 private void putSubtypeHistoryStr(String str) {
2845 if (DEBUG) {
2846 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2847 }
2848 Settings.Secure.putString(
2849 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2850 }
2851
2852 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2853 // Gets the first one from the history
2854 return getLastSubtypeForInputMethodLockedInternal(null);
2855 }
2856
2857 public String getLastSubtypeForInputMethodLocked(String imeId) {
2858 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2859 if (ime != null) {
2860 return ime.second;
2861 } else {
2862 return null;
2863 }
2864 }
2865
2866 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2867 List<Pair<String, ArrayList<String>>> enabledImes =
2868 getEnabledInputMethodsAndSubtypeListLocked();
2869 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002870 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002871 final String imeInTheHistory = imeAndSubtype.first;
2872 // If imeId is empty, returns the first IME and subtype in the history
2873 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2874 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002875 final String subtypeHashCode =
2876 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2877 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002878 if (!TextUtils.isEmpty(subtypeHashCode)) {
2879 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002880 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002881 }
2882 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2883 }
2884 }
2885 }
2886 if (DEBUG) {
2887 Slog.d(TAG, "No enabled IME found in the history");
2888 }
2889 return null;
2890 }
2891
satokdf31ae62011-01-15 06:19:44 +09002892 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002893 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2894 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2895 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002896 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2897 if (explicitlyEnabledSubtypes.size() == 0) {
2898 // If there are no explicitly enabled subtypes, applicable subtypes are
2899 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002900 InputMethodInfo ime = mMethodMap.get(imeId);
2901 // If IME is enabled and no subtypes are enabled, applicable subtypes
2902 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002903 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002904 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002905 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002906 if (implicitlySelectedSubtypes != null) {
2907 final int N = implicitlySelectedSubtypes.size();
2908 for (int i = 0; i < N; ++i) {
2909 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2910 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2911 return subtypeHashCode;
2912 }
2913 }
2914 }
2915 }
2916 } else {
satokf6cafb62011-01-17 16:29:02 +09002917 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002918 if (s.equals(subtypeHashCode)) {
2919 // If both imeId and subtypeId are enabled, return subtypeId.
2920 return s;
2921 }
satok723a27e2010-11-11 14:58:11 +09002922 }
2923 }
2924 // If imeId was enabled but subtypeId was disabled.
2925 return NOT_A_SUBTYPE_ID_STR;
2926 }
2927 }
2928 // If both imeId and subtypeId are disabled, return null
2929 return null;
2930 }
2931
2932 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2933 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2934 final String subtypeHistoryStr = getSubtypeHistoryStr();
2935 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2936 return imsList;
2937 }
2938 mInputMethodSplitter.setString(subtypeHistoryStr);
2939 while (mInputMethodSplitter.hasNext()) {
2940 String nextImsStr = mInputMethodSplitter.next();
2941 mSubtypeSplitter.setString(nextImsStr);
2942 if (mSubtypeSplitter.hasNext()) {
2943 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2944 // The first element is ime id.
2945 String imeId = mSubtypeSplitter.next();
2946 while (mSubtypeSplitter.hasNext()) {
2947 subtypeId = mSubtypeSplitter.next();
2948 break;
2949 }
2950 imsList.add(new Pair<String, String>(imeId, subtypeId));
2951 }
2952 }
2953 return imsList;
2954 }
2955
2956 private String getSubtypeHistoryStr() {
2957 if (DEBUG) {
2958 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2959 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2960 }
2961 return Settings.Secure.getString(
2962 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2963 }
2964
2965 public void putSelectedInputMethod(String imeId) {
2966 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2967 }
2968
2969 public void putSelectedSubtype(int subtypeId) {
2970 Settings.Secure.putInt(
2971 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2972 }
satokd87c2592010-09-29 11:52:06 +09002973 }
2974
satoke7c6998e2011-06-03 17:57:59 +09002975 private static class InputMethodFileManager {
2976 private static final String SYSTEM_PATH = "system";
2977 private static final String INPUT_METHOD_PATH = "inputmethod";
2978 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2979 private static final String NODE_SUBTYPES = "subtypes";
2980 private static final String NODE_SUBTYPE = "subtype";
2981 private static final String NODE_IMI = "imi";
2982 private static final String ATTR_ID = "id";
2983 private static final String ATTR_LABEL = "label";
2984 private static final String ATTR_ICON = "icon";
2985 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2986 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2987 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2988 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2989 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2990 private final HashMap<String, InputMethodInfo> mMethodMap;
2991 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2992 new HashMap<String, List<InputMethodSubtype>>();
2993 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2994 if (methodMap == null) {
2995 throw new NullPointerException("methodMap is null");
2996 }
2997 mMethodMap = methodMap;
2998 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
2999 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3000 if (!inputMethodDir.mkdirs()) {
3001 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3002 }
3003 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3004 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3005 if (!subtypeFile.exists()) {
3006 // If "subtypes.xml" doesn't exist, create a blank file.
3007 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3008 methodMap);
3009 } else {
3010 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3011 }
3012 }
3013
3014 private void deleteAllInputMethodSubtypes(String imiId) {
3015 synchronized (mMethodMap) {
3016 mSubtypesMap.remove(imiId);
3017 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3018 mMethodMap);
3019 }
3020 }
3021
3022 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003023 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003024 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003025 final HashSet<InputMethodSubtype> existingSubtypes =
3026 new HashSet<InputMethodSubtype>();
3027 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3028 existingSubtypes.add(imi.getSubtypeAt(i));
3029 }
3030
satoke7c6998e2011-06-03 17:57:59 +09003031 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3032 final int N = additionalSubtypes.length;
3033 for (int i = 0; i < N; ++i) {
3034 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003035 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003036 subtypes.add(subtype);
3037 }
3038 }
satok4a28bde2011-06-29 21:03:40 +09003039 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003040 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3041 mMethodMap);
3042 }
3043 }
3044
3045 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3046 synchronized (mMethodMap) {
3047 return mSubtypesMap;
3048 }
3049 }
3050
3051 private static void writeAdditionalInputMethodSubtypes(
3052 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3053 HashMap<String, InputMethodInfo> methodMap) {
3054 // Safety net for the case that this function is called before methodMap is set.
3055 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3056 FileOutputStream fos = null;
3057 try {
3058 fos = subtypesFile.startWrite();
3059 final XmlSerializer out = new FastXmlSerializer();
3060 out.setOutput(fos, "utf-8");
3061 out.startDocument(null, true);
3062 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3063 out.startTag(null, NODE_SUBTYPES);
3064 for (String imiId : allSubtypes.keySet()) {
3065 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3066 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3067 continue;
3068 }
3069 out.startTag(null, NODE_IMI);
3070 out.attribute(null, ATTR_ID, imiId);
3071 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3072 final int N = subtypesList.size();
3073 for (int i = 0; i < N; ++i) {
3074 final InputMethodSubtype subtype = subtypesList.get(i);
3075 out.startTag(null, NODE_SUBTYPE);
3076 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3077 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3078 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3079 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3080 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3081 out.attribute(null, ATTR_IS_AUXILIARY,
3082 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3083 out.endTag(null, NODE_SUBTYPE);
3084 }
3085 out.endTag(null, NODE_IMI);
3086 }
3087 out.endTag(null, NODE_SUBTYPES);
3088 out.endDocument();
3089 subtypesFile.finishWrite(fos);
3090 } catch (java.io.IOException e) {
3091 Slog.w(TAG, "Error writing subtypes", e);
3092 if (fos != null) {
3093 subtypesFile.failWrite(fos);
3094 }
3095 }
3096 }
3097
3098 private static void readAdditionalInputMethodSubtypes(
3099 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3100 if (allSubtypes == null || subtypesFile == null) return;
3101 allSubtypes.clear();
3102 FileInputStream fis = null;
3103 try {
3104 fis = subtypesFile.openRead();
3105 final XmlPullParser parser = Xml.newPullParser();
3106 parser.setInput(fis, null);
3107 int type = parser.getEventType();
3108 // Skip parsing until START_TAG
3109 while ((type = parser.next()) != XmlPullParser.START_TAG
3110 && type != XmlPullParser.END_DOCUMENT) {}
3111 String firstNodeName = parser.getName();
3112 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3113 throw new XmlPullParserException("Xml doesn't start with subtypes");
3114 }
3115 final int depth =parser.getDepth();
3116 String currentImiId = null;
3117 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3118 while (((type = parser.next()) != XmlPullParser.END_TAG
3119 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3120 if (type != XmlPullParser.START_TAG)
3121 continue;
3122 final String nodeName = parser.getName();
3123 if (NODE_IMI.equals(nodeName)) {
3124 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3125 if (TextUtils.isEmpty(currentImiId)) {
3126 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3127 continue;
3128 }
3129 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3130 allSubtypes.put(currentImiId, tempSubtypesArray);
3131 } else if (NODE_SUBTYPE.equals(nodeName)) {
3132 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3133 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3134 continue;
3135 }
3136 final int icon = Integer.valueOf(
3137 parser.getAttributeValue(null, ATTR_ICON));
3138 final int label = Integer.valueOf(
3139 parser.getAttributeValue(null, ATTR_LABEL));
3140 final String imeSubtypeLocale =
3141 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3142 final String imeSubtypeMode =
3143 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3144 final String imeSubtypeExtraValue =
3145 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003146 final boolean isAuxiliary = "1".equals(String.valueOf(
3147 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003148 final InputMethodSubtype subtype =
3149 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3150 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3151 tempSubtypesArray.add(subtype);
3152 }
3153 }
3154 } catch (XmlPullParserException e) {
3155 Slog.w(TAG, "Error reading subtypes: " + e);
3156 return;
3157 } catch (java.io.IOException e) {
3158 Slog.w(TAG, "Error reading subtypes: " + e);
3159 return;
3160 } catch (NumberFormatException e) {
3161 Slog.w(TAG, "Error reading subtypes: " + e);
3162 return;
3163 } finally {
3164 if (fis != null) {
3165 try {
3166 fis.close();
3167 } catch (java.io.IOException e1) {
3168 Slog.w(TAG, "Failed to close.");
3169 }
3170 }
3171 }
3172 }
3173 }
3174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 @Override
3178 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3179 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3180 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3183 + Binder.getCallingPid()
3184 + ", uid=" + Binder.getCallingUid());
3185 return;
3186 }
3187
3188 IInputMethod method;
3189 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 synchronized (mMethodMap) {
3194 p.println("Current Input Method Manager state:");
3195 int N = mMethodList.size();
3196 p.println(" Input Methods:");
3197 for (int i=0; i<N; i++) {
3198 InputMethodInfo info = mMethodList.get(i);
3199 p.println(" InputMethod #" + i + ":");
3200 info.dump(p, " ");
3201 }
3202 p.println(" Clients:");
3203 for (ClientState ci : mClients.values()) {
3204 p.println(" Client " + ci + ":");
3205 p.println(" client=" + ci.client);
3206 p.println(" inputContext=" + ci.inputContext);
3207 p.println(" sessionRequested=" + ci.sessionRequested);
3208 p.println(" curSession=" + ci.curSession);
3209 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003210 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003212 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3213 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3215 + " mBoundToMethod=" + mBoundToMethod);
3216 p.println(" mCurToken=" + mCurToken);
3217 p.println(" mCurIntent=" + mCurIntent);
3218 method = mCurMethod;
3219 p.println(" mCurMethod=" + mCurMethod);
3220 p.println(" mEnabledSession=" + mEnabledSession);
3221 p.println(" mShowRequested=" + mShowRequested
3222 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3223 + " mShowForced=" + mShowForced
3224 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003225 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003227
Jeff Brownb88102f2010-09-08 11:49:43 -07003228 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 pw.flush();
3231 try {
3232 client.client.asBinder().dump(fd, args);
3233 } catch (RemoteException e) {
3234 p.println("Input method client dead: " + e);
3235 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003236 } else {
3237 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003239
Jeff Brownb88102f2010-09-08 11:49:43 -07003240 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 pw.flush();
3243 try {
3244 method.asBinder().dump(fd, args);
3245 } catch (RemoteException e) {
3246 p.println("Input method service dead: " + e);
3247 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003248 } else {
3249 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 }
3251 }
3252}