blob: 17ea03bd20b044cbeaf8ddb3526b1fcfec302c9d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090019import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
satoke7c6998e2011-06-03 17:57:59 +090031import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090033import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35import android.app.ActivityManagerNative;
36import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090037import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090038import android.app.Notification;
39import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070040import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.DialogInterface.OnCancelListener;
46import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090047import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070054import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.Resources;
56import android.content.res.TypedArray;
57import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080058import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
63import android.os.IInterface;
64import android.os.Message;
65import android.os.Parcel;
66import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080067import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090072import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090074import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090076import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090077import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.PrintWriterPrinter;
79import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090080import android.util.Slog;
81import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090083import android.view.LayoutInflater;
84import android.view.View;
85import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090087import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.inputmethod.InputBinding;
89import android.view.inputmethod.InputMethod;
90import android.view.inputmethod.InputMethodInfo;
91import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090092import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090093import android.widget.ArrayAdapter;
94import android.widget.RadioButton;
95import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
satoke7c6998e2011-06-03 17:57:59 +090097import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090099import java.io.FileInputStream;
100import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.IOException;
102import java.io.PrintWriter;
103import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +0900104import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900106import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.util.List;
satok913a8922010-08-26 21:53:41 +0900108import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110/**
111 * This class provides a system service that manages input methods.
112 */
113public class InputMethodManagerService extends IInputMethodManager.Stub
114 implements ServiceConnection, Handler.Callback {
115 static final boolean DEBUG = false;
116 static final String TAG = "InputManagerService";
117
118 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900119 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900120 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900121 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static final int MSG_UNBIND_INPUT = 1000;
124 static final int MSG_BIND_INPUT = 1010;
125 static final int MSG_SHOW_SOFT_INPUT = 1020;
126 static final int MSG_HIDE_SOFT_INPUT = 1030;
127 static final int MSG_ATTACH_TOKEN = 1040;
128 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_START_INPUT = 2000;
131 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 static final int MSG_UNBIND_METHOD = 3000;
134 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
satokf9f01002011-05-19 21:31:50 +0900138 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
139
satokab751aa2010-09-14 19:17:36 +0900140 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900141 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900142 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
143 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900144 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800147 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900149 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final IWindowManager mIWindowManager;
152 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900153 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // All known input methods. mMethodMap also serves as the global
158 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900159 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
160 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900161 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
162 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
satok7cfc0ed2011-06-20 21:29:36 +0900164 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700165 private NotificationManager mNotificationManager;
166 private KeyguardManager mKeyguardManager;
167 private StatusBarManagerService mStatusBar;
168 private Notification mImeSwitcherNotification;
169 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900170 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900171 private boolean mNotificationShown;
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 class SessionState {
174 final ClientState client;
175 final IInputMethod method;
176 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
179 public String toString() {
180 return "SessionState{uid " + client.uid + " pid " + client.pid
181 + " method " + Integer.toHexString(
182 System.identityHashCode(method))
183 + " session " + Integer.toHexString(
184 System.identityHashCode(session))
185 + "}";
186 }
187
188 SessionState(ClientState _client, IInputMethod _method,
189 IInputMethodSession _session) {
190 client = _client;
191 method = _method;
192 session = _session;
193 }
194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 class ClientState {
197 final IInputMethodClient client;
198 final IInputContext inputContext;
199 final int uid;
200 final int pid;
201 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 boolean sessionRequested;
204 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "ClientState{" + Integer.toHexString(
209 System.identityHashCode(this)) + " uid " + uid
210 + " pid " + pid + "}";
211 }
212
213 ClientState(IInputMethodClient _client, IInputContext _inputContext,
214 int _uid, int _pid) {
215 client = _client;
216 inputContext = _inputContext;
217 uid = _uid;
218 pid = _pid;
219 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
220 }
221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final HashMap<IBinder, ClientState> mClients
224 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700227 * Set once the system is ready to run third party code.
228 */
229 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * Id of the currently selected input method.
233 */
234 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The current binding sequence number, incremented every time there is
238 * a new bind performed.
239 */
240 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * The client that is currently bound to an input method.
244 */
245 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700248 * The last window token that gained focus.
249 */
250 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * The input context last provided by the current client.
254 */
255 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The attributes last provided by the current client.
259 */
260 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * The input method ID of the input method service that we are currently
264 * connected to or in the process of connecting to.
265 */
266 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
satokab751aa2010-09-14 19:17:36 +0900269 * The current subtype of the current input method.
270 */
271 private InputMethodSubtype mCurrentSubtype;
272
satok4e4569d2010-11-19 18:45:53 +0900273 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900274 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
275 mShortcutInputMethodsAndSubtypes =
276 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900277
278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * Set to true if our ServiceConnection is currently actively bound to
280 * a service (whether or not we have gotten its IBinder back yet).
281 */
282 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Set if the client has asked for the input method to be shown.
286 */
287 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Set if we were explicitly told to show the input method.
291 */
292 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if we were forced to be shown.
296 */
297 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Set if we last told the input method to show itself.
301 */
302 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * The Intent used to connect to the current input method.
306 */
307 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The token we have made for the currently active input method, to
311 * identify it in the future.
312 */
313 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * If non-null, this is the input method service we are currently connected
317 * to.
318 */
319 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Time that we last initiated a bind to the input method, to determine
323 * if we should try to disconnect and reconnect to it.
324 */
325 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Have we called mCurMethod.bindInput()?
329 */
330 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Currently enabled session. Only touched by service thread, not
334 * protected by a lock.
335 */
336 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * True if the screen is on. The value is true initially.
340 */
341 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
Joe Onorato857fd9b2011-01-27 15:08:35 -0800343 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
344 int mImeWindowVis;
345
Ken Wakasa05dbb652011-08-22 15:22:43 +0900346 private AlertDialog.Builder mDialogBuilder;
347 private AlertDialog mSwitchingDialog;
348 private InputMethodInfo[] mIms;
349 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 class SettingsObserver extends ContentObserver {
352 SettingsObserver(Handler handler) {
353 super(handler);
354 ContentResolver resolver = mContext.getContentResolver();
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
356 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900358 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
359 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900360 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override public void onChange(boolean selfChange) {
364 synchronized (mMethodMap) {
365 updateFromSettingsLocked();
366 }
367 }
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
371 @Override
372 public void onReceive(Context context, Intent intent) {
373 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
374 mScreenOn = true;
375 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
376 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900377 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700378 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
379 hideInputMethodMenu();
380 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800382 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384
385 // Inform the current client of the change in active status
386 try {
387 if (mCurClient != null && mCurClient.client != null) {
388 mCurClient.client.setActive(mScreenOn);
389 }
390 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800391 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 + mCurClient.pid + " uid " + mCurClient.uid);
393 }
394 }
395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800396
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800397 class MyPackageMonitor extends PackageMonitor {
398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800400 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800402 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
404 final int N = mMethodList.size();
405 if (curInputMethodId != null) {
406 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800407 InputMethodInfo imi = mMethodList.get(i);
408 if (imi.getId().equals(curInputMethodId)) {
409 for (String pkg : packages) {
410 if (imi.getPackageName().equals(pkg)) {
411 if (!doit) {
412 return true;
413 }
satok723a27e2010-11-11 14:58:11 +0900414 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800415 chooseNewDefaultIMELocked();
416 return true;
417 }
418 }
419 }
420 }
421 }
422 }
423 return false;
424 }
425
426 @Override
427 public void onSomePackagesChanged() {
428 synchronized (mMethodMap) {
429 InputMethodInfo curIm = null;
430 String curInputMethodId = Settings.Secure.getString(mContext
431 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
432 final int N = mMethodList.size();
433 if (curInputMethodId != null) {
434 for (int i=0; i<N; i++) {
435 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900436 final String imiId = imi.getId();
437 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 curIm = imi;
439 }
satoke7c6998e2011-06-03 17:57:59 +0900440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900442 if (isPackageModified(imi.getPackageName())) {
443 mFileManager.deleteAllInputMethodSubtypes(imiId);
444 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800445 if (change == PACKAGE_TEMPORARY_CHANGE
446 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 + imi.getComponent());
449 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451 }
452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800453
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 buildInputMethodListLocked(mMethodList, mMethodMap);
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800457
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800458 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800459 int change = isPackageDisappearing(curIm.getPackageName());
460 if (change == PACKAGE_TEMPORARY_CHANGE
461 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800462 ServiceInfo si = null;
463 try {
464 si = mContext.getPackageManager().getServiceInfo(
465 curIm.getComponent(), 0);
466 } catch (PackageManager.NameNotFoundException ex) {
467 }
468 if (si == null) {
469 // Uh oh, current input method is no longer around!
470 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800471 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900472 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 changed = true;
475 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900477 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 }
479 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 }
satokab751aa2010-09-14 19:17:36 +0900482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 if (curIm == null) {
484 // We currently don't have a default input method... is
485 // one now available?
486 changed = chooseNewDefaultIMELocked();
487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 if (changed) {
490 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 }
494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
Jean Chalarde0d32a62011-10-20 20:36:07 +0900496 private static class MethodCallback extends IInputMethodCallback.Stub {
497 private final IInputMethod mMethod;
498 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
Jean Chalarde0d32a62011-10-20 20:36:07 +0900500 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900502 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800504
satoke7c6998e2011-06-03 17:57:59 +0900505 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 public void finishedEvent(int seq, boolean handled) throws RemoteException {
507 }
508
satoke7c6998e2011-06-03 17:57:59 +0900509 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900511 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Dianne Hackborn661cd522011-08-22 00:26:20 -0700515 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800517 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 mHandler = new Handler(this);
519 mIWindowManager = IWindowManager.Stub.asInterface(
520 ServiceManager.getService(Context.WINDOW_SERVICE));
521 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900522 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public void executeMessage(Message msg) {
524 handleMessage(msg);
525 }
526 });
satok7cfc0ed2011-06-20 21:29:36 +0900527
satok7cfc0ed2011-06-20 21:29:36 +0900528 mImeSwitcherNotification = new Notification();
529 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
530 mImeSwitcherNotification.when = 0;
531 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
532 mImeSwitcherNotification.tickerText = null;
533 mImeSwitcherNotification.defaults = 0; // please be quiet
534 mImeSwitcherNotification.sound = null;
535 mImeSwitcherNotification.vibrate = null;
536 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900537 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900538
539 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900540
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
satok7cfc0ed2011-06-20 21:29:36 +0900553 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900554
satokd87c2592010-09-29 11:52:06 +0900555 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900556 mSettings = new InputMethodSettings(
557 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900559 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
satokd87c2592010-09-29 11:52:06 +0900561 if (TextUtils.isEmpty(Settings.Secure.getString(
562 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900564 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
566 try {
satokd87c2592010-09-29 11:52:06 +0900567 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 imi.getPackageName(), 0).getResources();
569 if (res.getBoolean(imi.getIsDefaultResourceId())) {
570 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800571 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573 } catch (PackageManager.NameNotFoundException ex) {
574 } catch (Resources.NotFoundException ex) {
575 }
576 }
577 }
satokd87c2592010-09-29 11:52:06 +0900578 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900579 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800580 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900583 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 mSettingsObserver = new SettingsObserver(mHandler);
588 updateFromSettingsLocked();
589 }
590
591 @Override
592 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
593 throws RemoteException {
594 try {
595 return super.onTransact(code, data, reply, flags);
596 } catch (RuntimeException e) {
597 // The input method manager only throws security exceptions, so let's
598 // log all others.
599 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602 throw e;
603 }
604 }
605
Dianne Hackborn661cd522011-08-22 00:26:20 -0700606 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700607 synchronized (mMethodMap) {
608 if (!mSystemReady) {
609 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700610 mKeyguardManager = (KeyguardManager)
611 mContext.getSystemService(Context.KEYGUARD_SERVICE);
612 mNotificationManager = (NotificationManager)
613 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
614 mStatusBar = statusBar;
615 statusBar.setIconVisibility("ime", false);
616 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900617 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
618 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700619 try {
620 startInputInnerLocked();
621 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800622 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700623 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700624 }
625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800627
satok15452a42011-10-28 17:58:28 +0900628 private void setImeWindowVisibilityStatusHiddenLocked() {
629 mImeWindowVis = 0;
630 updateImeWindowStatusLocked();
631 }
632
633 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900634 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700635 }
636
satoke7c6998e2011-06-03 17:57:59 +0900637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public List<InputMethodInfo> getInputMethodList() {
639 synchronized (mMethodMap) {
640 return new ArrayList<InputMethodInfo>(mMethodList);
641 }
642 }
643
satoke7c6998e2011-06-03 17:57:59 +0900644 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 public List<InputMethodInfo> getEnabledInputMethodList() {
646 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900647 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649 }
650
satokbb4aa062011-01-19 21:40:27 +0900651 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
652 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
653 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
654 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
655 for (InputMethodInfo imi: getEnabledInputMethodList()) {
656 enabledInputMethodAndSubtypes.put(
657 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
658 }
659 return enabledInputMethodAndSubtypes;
660 }
661
662 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
663 boolean allowsImplicitlySelectedSubtypes) {
664 if (imi == null && mCurMethodId != null) {
665 imi = mMethodMap.get(mCurMethodId);
666 }
satok7265d9b2011-02-14 15:47:30 +0900667 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900668 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900669 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900670 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900671 }
satok7265d9b2011-02-14 15:47:30 +0900672 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900673 }
674
satoke7c6998e2011-06-03 17:57:59 +0900675 @Override
satok16331c82010-12-20 23:48:46 +0900676 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
677 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900678 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900679 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900680 }
681 }
682
satoke7c6998e2011-06-03 17:57:59 +0900683 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public void addClient(IInputMethodClient client,
685 IInputContext inputContext, int uid, int pid) {
686 synchronized (mMethodMap) {
687 mClients.put(client.asBinder(), new ClientState(client,
688 inputContext, uid, pid));
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
satoke7c6998e2011-06-03 17:57:59 +0900692 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 public void removeClient(IInputMethodClient client) {
694 synchronized (mMethodMap) {
695 mClients.remove(client.asBinder());
696 }
697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 void executeOrSendMessage(IInterface target, Message msg) {
700 if (target.asBinder() instanceof Binder) {
701 mCaller.sendMessage(msg);
702 } else {
703 handleMessage(msg);
704 msg.recycle();
705 }
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700708 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 + mCurClient.client.asBinder());
712 if (mBoundToMethod) {
713 mBoundToMethod = false;
714 if (mCurMethod != null) {
715 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
716 MSG_UNBIND_INPUT, mCurMethod));
717 }
718 }
719 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
720 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
721 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 // Call setActive(false) on the old client
724 try {
725 mCurClient.client.setActive(false);
726 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800727 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 + mCurClient.pid + " uid " + mCurClient.uid);
729 }
730 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
The Android Open Source Project10592532009-03-18 17:39:46 -0700732 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 private int getImeShowFlags() {
737 int flags = 0;
738 if (mShowForced) {
739 flags |= InputMethod.SHOW_FORCED
740 | InputMethod.SHOW_EXPLICIT;
741 } else if (mShowExplicitlyRequested) {
742 flags |= InputMethod.SHOW_EXPLICIT;
743 }
744 return flags;
745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 private int getAppShowFlags() {
748 int flags = 0;
749 if (mShowForced) {
750 flags |= InputMethodManager.SHOW_FORCED;
751 } else if (!mShowExplicitlyRequested) {
752 flags |= InputMethodManager.SHOW_IMPLICIT;
753 }
754 return flags;
755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
758 if (!mBoundToMethod) {
759 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
760 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
761 mBoundToMethod = true;
762 }
763 final SessionState session = mCurClient.curSession;
764 if (initial) {
765 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
766 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
767 } else {
768 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
769 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
770 }
771 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800773 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 return needResult
776 ? new InputBindResult(session.session, mCurId, mCurSeq)
777 : null;
778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 InputBindResult startInputLocked(IInputMethodClient client,
781 IInputContext inputContext, EditorInfo attribute,
782 boolean initial, boolean needResult) {
783 // If no method is currently selected, do nothing.
784 if (mCurMethodId == null) {
785 return mNoBinding;
786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 ClientState cs = mClients.get(client.asBinder());
789 if (cs == null) {
790 throw new IllegalArgumentException("unknown client "
791 + client.asBinder());
792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 try {
795 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
796 // Check with the window manager to make sure this client actually
797 // has a window with focus. If not, reject. This is thread safe
798 // because if the focus changes some time before or after, the
799 // next client receiving focus that has any interest in input will
800 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
803 return null;
804 }
805 } catch (RemoteException e) {
806 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 if (mCurClient != cs) {
809 // If the client is changing, we need to switch over to the new
810 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700811 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800812 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 + cs.client.asBinder());
814
815 // If the screen is on, inform the new client it is active
816 if (mScreenOn) {
817 try {
818 cs.client.setActive(mScreenOn);
819 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800820 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 + cs.pid + " uid " + cs.uid);
822 }
823 }
824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Bump up the sequence for this client and attach it.
827 mCurSeq++;
828 if (mCurSeq <= 0) mCurSeq = 1;
829 mCurClient = cs;
830 mCurInputContext = inputContext;
831 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 // Check if the input method is changing.
834 if (mCurId != null && mCurId.equals(mCurMethodId)) {
835 if (cs.curSession != null) {
836 // Fast case: if we are already connected to the input method,
837 // then just return it.
838 return attachNewInputLocked(initial, needResult);
839 }
840 if (mHaveConnection) {
841 if (mCurMethod != null) {
842 if (!cs.sessionRequested) {
843 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800844 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
846 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900847 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849 // Return to client, and we will get back with it when
850 // we have had a session made for it.
851 return new InputBindResult(null, mCurId, mCurSeq);
852 } else if (SystemClock.uptimeMillis()
853 < (mLastBindTime+TIME_TO_RECONNECT)) {
854 // In this case we have connected to the service, but
855 // don't yet have its interface. If it hasn't been too
856 // long since we did the connection, we'll return to
857 // the client and wait to get the service interface so
858 // we can report back. If it has been too long, we want
859 // to fall through so we can try a disconnect/reconnect
860 // to see if we can get back in touch with the service.
861 return new InputBindResult(null, mCurId, mCurSeq);
862 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
864 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866 }
867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700869 return startInputInnerLocked();
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700872 InputBindResult startInputInnerLocked() {
873 if (mCurMethodId == null) {
874 return mNoBinding;
875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700877 if (!mSystemReady) {
878 // If the system is not yet ready, we shouldn't be running third
879 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700880 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 InputMethodInfo info = mMethodMap.get(mCurMethodId);
884 if (info == null) {
885 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
886 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800887
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700888 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
891 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700892 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
893 com.android.internal.R.string.input_method_binding_label);
894 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
895 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
897 mLastBindTime = SystemClock.uptimeMillis();
898 mHaveConnection = true;
899 mCurId = info.getId();
900 mCurToken = new Binder();
901 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800902 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 mIWindowManager.addWindowToken(mCurToken,
904 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
905 } catch (RemoteException e) {
906 }
907 return new InputBindResult(null, mCurId, mCurSeq);
908 } else {
909 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800910 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 + mCurIntent);
912 }
913 return null;
914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
satoke7c6998e2011-06-03 17:57:59 +0900916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public InputBindResult startInput(IInputMethodClient client,
918 IInputContext inputContext, EditorInfo attribute,
919 boolean initial, boolean needResult) {
920 synchronized (mMethodMap) {
921 final long ident = Binder.clearCallingIdentity();
922 try {
923 return startInputLocked(client, inputContext, attribute,
924 initial, needResult);
925 } finally {
926 Binder.restoreCallingIdentity(ident);
927 }
928 }
929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800930
satoke7c6998e2011-06-03 17:57:59 +0900931 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 public void finishInput(IInputMethodClient client) {
933 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800934
satoke7c6998e2011-06-03 17:57:59 +0900935 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 public void onServiceConnected(ComponentName name, IBinder service) {
937 synchronized (mMethodMap) {
938 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
939 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700940 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800941 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700942 unbindCurrentMethodLocked(false);
943 return;
944 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800945 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700946 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
947 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800949 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700950 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700952 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900953 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955 }
956 }
957 }
958
959 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
960 synchronized (mMethodMap) {
961 if (mCurMethod != null && method != null
962 && mCurMethod.asBinder() == method.asBinder()) {
963 if (mCurClient != null) {
964 mCurClient.curSession = new SessionState(mCurClient,
965 method, session);
966 mCurClient.sessionRequested = false;
967 InputBindResult res = attachNewInputLocked(true, true);
968 if (res.method != null) {
969 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
970 MSG_BIND_METHOD, mCurClient.client, res));
971 }
972 }
973 }
974 }
975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800976
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700977 void unbindCurrentMethodLocked(boolean reportToClient) {
978 if (mHaveConnection) {
979 mContext.unbindService(this);
980 mHaveConnection = false;
981 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800982
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 if (mCurToken != null) {
984 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800985 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 mIWindowManager.removeWindowToken(mCurToken);
987 } catch (RemoteException e) {
988 }
989 mCurToken = null;
990 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800991
The Android Open Source Project10592532009-03-18 17:39:46 -0700992 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700993 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800994
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700995 if (reportToClient && mCurClient != null) {
996 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
997 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
998 }
999 }
1000
Devin Taylor0c33ed22010-02-23 13:26:46 -06001001 private void finishSession(SessionState sessionState) {
1002 if (sessionState != null && sessionState.session != null) {
1003 try {
1004 sessionState.session.finishSession();
1005 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001006 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001007 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001008 }
1009 }
1010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001012 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 if (mCurMethod != null) {
1014 for (ClientState cs : mClients.values()) {
1015 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001016 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 cs.curSession = null;
1018 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001019
1020 finishSession(mEnabledSession);
1021 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 mCurMethod = null;
1023 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001024 if (mStatusBar != null) {
1025 mStatusBar.setIconVisibility("ime", false);
1026 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001028
satoke7c6998e2011-06-03 17:57:59 +09001029 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 public void onServiceDisconnected(ComponentName name) {
1031 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001032 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 + " mCurIntent=" + mCurIntent);
1034 if (mCurMethod != null && mCurIntent != null
1035 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001036 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 // We consider this to be a new bind attempt, since the system
1038 // should now try to restart the service for us.
1039 mLastBindTime = SystemClock.uptimeMillis();
1040 mShowRequested = mInputShown;
1041 mInputShown = false;
1042 if (mCurClient != null) {
1043 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1044 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1045 }
1046 }
1047 }
1048 }
1049
satokf9f01002011-05-19 21:31:50 +09001050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001052 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 long ident = Binder.clearCallingIdentity();
1054 try {
1055 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001056 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 return;
1058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 synchronized (mMethodMap) {
1061 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001062 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001063 if (mStatusBar != null) {
1064 mStatusBar.setIconVisibility("ime", false);
1065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001067 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001068 CharSequence contentDescription = null;
1069 try {
1070 PackageManager packageManager = mContext.getPackageManager();
1071 contentDescription = packageManager.getApplicationLabel(
1072 packageManager.getApplicationInfo(packageName, 0));
1073 } catch (NameNotFoundException nnfe) {
1074 /* ignore */
1075 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001076 if (mStatusBar != null) {
1077 mStatusBar.setIcon("ime", packageName, iconId, 0,
1078 contentDescription != null
1079 ? contentDescription.toString() : null);
1080 mStatusBar.setIconVisibility("ime", true);
1081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083 }
1084 } finally {
1085 Binder.restoreCallingIdentity(ident);
1086 }
1087 }
1088
satok7cfc0ed2011-06-20 21:29:36 +09001089 private boolean needsToShowImeSwitchOngoingNotification() {
1090 if (!mShowOngoingImeSwitcherForPhones) return false;
1091 synchronized (mMethodMap) {
1092 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1093 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001094 if (N > 2) return true;
1095 if (N < 1) return false;
1096 int nonAuxCount = 0;
1097 int auxCount = 0;
1098 InputMethodSubtype nonAuxSubtype = null;
1099 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001100 for(int i = 0; i < N; ++i) {
1101 final InputMethodInfo imi = imis.get(i);
1102 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1103 imi, true);
1104 final int subtypeCount = subtypes.size();
1105 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001106 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001107 } else {
1108 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001109 final InputMethodSubtype subtype = subtypes.get(j);
1110 if (!subtype.isAuxiliary()) {
1111 ++nonAuxCount;
1112 nonAuxSubtype = subtype;
1113 } else {
1114 ++auxCount;
1115 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001116 }
1117 }
1118 }
satok7cfc0ed2011-06-20 21:29:36 +09001119 }
satokb6359412011-06-28 17:47:41 +09001120 if (nonAuxCount > 1 || auxCount > 1) {
1121 return true;
1122 } else if (nonAuxCount == 1 && auxCount == 1) {
1123 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001124 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1125 || auxSubtype.overridesImplicitlyEnabledSubtype()
1126 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001127 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1128 return false;
1129 }
1130 return true;
1131 }
1132 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001133 }
satok7cfc0ed2011-06-20 21:29:36 +09001134 }
1135
satokdbf29502011-08-25 15:28:23 +09001136 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001137 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001138 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001139 int uid = Binder.getCallingUid();
1140 long ident = Binder.clearCallingIdentity();
1141 try {
1142 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001143 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001144 return;
1145 }
1146
1147 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001148 mImeWindowVis = vis;
1149 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001150 if (mStatusBar != null) {
1151 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1152 }
satok7cfc0ed2011-06-20 21:29:36 +09001153 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001154 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1155 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001156 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001157 final CharSequence title = mRes.getText(
1158 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001159 final CharSequence imiLabel = imi.loadLabel(pm);
1160 final CharSequence summary = mCurrentSubtype != null
1161 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1162 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1163 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001164 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001165 : imiLabel;
1166
satok7cfc0ed2011-06-20 21:29:36 +09001167 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001168 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001169 if (mNotificationManager != null) {
1170 mNotificationManager.notify(
1171 com.android.internal.R.string.select_input_method,
1172 mImeSwitcherNotification);
1173 mNotificationShown = true;
1174 }
satok7cfc0ed2011-06-20 21:29:36 +09001175 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001176 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001177 mNotificationManager.cancel(
1178 com.android.internal.R.string.select_input_method);
1179 mNotificationShown = false;
1180 }
1181 }
satok06487a52010-10-29 11:37:18 +09001182 }
1183 } finally {
1184 Binder.restoreCallingIdentity(ident);
1185 }
1186 }
1187
satoke7c6998e2011-06-03 17:57:59 +09001188 @Override
satokf9f01002011-05-19 21:31:50 +09001189 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1190 synchronized (mMethodMap) {
1191 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1192 for (int i = 0; i < spans.length; ++i) {
1193 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001194 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001195 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001196 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001197 }
1198 }
1199 }
1200 }
1201
satoke7c6998e2011-06-03 17:57:59 +09001202 @Override
satokf9f01002011-05-19 21:31:50 +09001203 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1204 synchronized (mMethodMap) {
1205 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1206 // TODO: Do not send the intent if the process of the targetImi is already dead.
1207 if (targetImi != null) {
1208 final String[] suggestions = span.getSuggestions();
1209 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001210 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001211 final Intent intent = new Intent();
1212 // Ensures that only a class in the original IME package will receive the
1213 // notification.
satok42c5a162011-05-26 16:46:14 +09001214 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001215 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1216 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1217 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1218 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1219 mContext.sendBroadcast(intent);
1220 return true;
1221 }
1222 }
1223 return false;
1224 }
1225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001227 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1228 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1229 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1230 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001232 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001233 // There is no input method selected, try to choose new applicable input method.
1234 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1235 id = Settings.Secure.getString(mContext.getContentResolver(),
1236 Settings.Secure.DEFAULT_INPUT_METHOD);
1237 }
1238 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 try {
satokab751aa2010-09-14 19:17:36 +09001240 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001243 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001244 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
satokf3db1af2010-11-23 13:34:33 +09001246 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001247 } else {
1248 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001249 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001250 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
satokab751aa2010-09-14 19:17:36 +09001254 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 InputMethodInfo info = mMethodMap.get(id);
1256 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001257 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001261 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001262 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1263 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001264 }
1265 if (subtype != mCurrentSubtype) {
1266 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001267 if (subtype != null) {
1268 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1269 }
satokcd7cd292010-11-20 15:46:23 +09001270 if (mCurMethod != null) {
1271 try {
satoke40dea02011-01-30 01:14:02 +09001272 final Configuration conf = mRes.getConfiguration();
1273 final boolean haveHardKeyboard = conf.keyboard
1274 != Configuration.KEYBOARD_NOKEYS;
1275 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001276 && conf.hardKeyboardHidden
1277 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001278 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1279 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1280 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001281 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001282 // If subtype is null, try to find the most applicable one from
1283 // getCurrentInputMethodSubtype.
1284 if (subtype == null) {
1285 subtype = getCurrentInputMethodSubtype();
1286 }
1287 mCurMethod.changeInputMethodSubtype(subtype);
1288 } catch (RemoteException e) {
1289 return;
satokab751aa2010-09-14 19:17:36 +09001290 }
1291 }
1292 }
1293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 return;
1295 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 final long ident = Binder.clearCallingIdentity();
1298 try {
satokab751aa2010-09-14 19:17:36 +09001299 // Set a subtype to this input method.
1300 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001301 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1302 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1303 // because mCurMethodId is stored as a history in
1304 // setSelectedInputMethodAndSubtypeLocked().
1305 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306
1307 if (ActivityManagerNative.isSystemReady()) {
1308 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001309 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 intent.putExtra("input_method_id", id);
1311 mContext.sendBroadcast(intent);
1312 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001313 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 } finally {
1315 Binder.restoreCallingIdentity(ident);
1316 }
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
satok42c5a162011-05-26 16:46:14 +09001319 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001320 public boolean showSoftInput(IInputMethodClient client, int flags,
1321 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001322 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 long ident = Binder.clearCallingIdentity();
1324 try {
1325 synchronized (mMethodMap) {
1326 if (mCurClient == null || client == null
1327 || mCurClient.client.asBinder() != client.asBinder()) {
1328 try {
1329 // We need to check if this is the current client with
1330 // focus in the window manager, to allow this call to
1331 // be made before input is started in it.
1332 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001333 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001334 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001340
Joe Onorato8a9b2202010-02-26 18:56:32 -08001341 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001342 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344 } finally {
1345 Binder.restoreCallingIdentity(ident);
1346 }
1347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
The Android Open Source Project4df24232009-03-05 14:34:35 -08001349 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 mShowRequested = true;
1351 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1352 mShowExplicitlyRequested = true;
1353 }
1354 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1355 mShowExplicitlyRequested = true;
1356 mShowForced = true;
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
Dianne Hackborncc278702009-09-02 23:07:23 -07001359 if (!mSystemReady) {
1360 return false;
1361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001362
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001365 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1366 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1367 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001369 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001371 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 // The client has asked to have the input method shown, but
1373 // we have been sitting here too long with a connection to the
1374 // service and no interface received, so let's disconnect/connect
1375 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001378 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 mContext.unbindService(this);
1380 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001382
The Android Open Source Project4df24232009-03-05 14:34:35 -08001383 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
satok42c5a162011-05-26 16:46:14 +09001386 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001387 public boolean hideSoftInput(IInputMethodClient client, int flags,
1388 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001389 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 long ident = Binder.clearCallingIdentity();
1391 try {
1392 synchronized (mMethodMap) {
1393 if (mCurClient == null || client == null
1394 || mCurClient.client.asBinder() != client.asBinder()) {
1395 try {
1396 // We need to check if this is the current client with
1397 // focus in the window manager, to allow this call to
1398 // be made before input is started in it.
1399 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001400 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1401 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001402 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001403 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001406 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001407 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
Joe Onorato8a9b2202010-02-26 18:56:32 -08001411 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001412 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 } finally {
1415 Binder.restoreCallingIdentity(ident);
1416 }
1417 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001418
The Android Open Source Project4df24232009-03-05 14:34:35 -08001419 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1421 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001422 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001424 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001427 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001429 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001433 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1434 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1435 res = true;
1436 } else {
1437 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 }
1439 mInputShown = false;
1440 mShowRequested = false;
1441 mShowExplicitlyRequested = false;
1442 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001443 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001445
satok42c5a162011-05-26 16:46:14 +09001446 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001447 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1449 boolean first, int windowFlags) {
1450 long ident = Binder.clearCallingIdentity();
1451 try {
1452 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001453 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 + " viewHasFocus=" + viewHasFocus
1455 + " isTextEditor=" + isTextEditor
1456 + " softInputMode=#" + Integer.toHexString(softInputMode)
1457 + " first=" + first + " flags=#"
1458 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (mCurClient == null || client == null
1461 || mCurClient.client.asBinder() != client.asBinder()) {
1462 try {
1463 // We need to check if this is the current client with
1464 // focus in the window manager, to allow this call to
1465 // be made before input is started in it.
1466 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 return;
1469 }
1470 } catch (RemoteException e) {
1471 }
1472 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001473
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001474 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001475 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001476 return;
1477 }
1478 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001480 // Should we auto-show the IME even if the caller has not
1481 // specified what should be done with it?
1482 // We only do this automatically if the window can resize
1483 // to accommodate the IME (so what the user sees will give
1484 // them good context without input information being obscured
1485 // by the IME) or if running on a large screen where there
1486 // is more room for the target window + IME.
1487 final boolean doAutoShow =
1488 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1489 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1490 || mRes.getConfiguration().isLayoutSizeAtLeast(
1491 Configuration.SCREENLAYOUT_SIZE_LARGE);
1492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1494 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001495 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1497 // There is no focus view, and this window will
1498 // be behind any soft input window, so hide the
1499 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001501 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001503 } else if (isTextEditor && doAutoShow && (softInputMode &
1504 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 // There is a focus view, and we are navigating forward
1506 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001507 // We only do this automatically if the window an resize
1508 // to accomodate the IME (so what the user sees will give
1509 // them good context without input information being obscured
1510 // by the IME) or if running on a large screen where there
1511 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001512 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001513 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515 break;
1516 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1517 // Do nothing.
1518 break;
1519 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1520 if ((softInputMode &
1521 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001522 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001523 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 break;
1526 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001527 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001528 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 break;
1530 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1531 if ((softInputMode &
1532 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001533 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001534 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 }
1536 break;
1537 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001538 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001539 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 break;
1541 }
1542 }
1543 } finally {
1544 Binder.restoreCallingIdentity(ident);
1545 }
1546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001547
satok42c5a162011-05-26 16:46:14 +09001548 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1550 synchronized (mMethodMap) {
1551 if (mCurClient == null || client == null
1552 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001553 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001554 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
satok440aab52010-11-25 09:43:11 +09001557 // Always call subtype picker, because subtype picker is a superset of input method
1558 // picker.
satokab751aa2010-09-14 19:17:36 +09001559 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1560 }
1561 }
1562
satok42c5a162011-05-26 16:46:14 +09001563 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001565 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1566 }
1567
satok42c5a162011-05-26 16:46:14 +09001568 @Override
satok28203512010-11-24 11:06:49 +09001569 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1570 synchronized (mMethodMap) {
1571 if (subtype != null) {
1572 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1573 mMethodMap.get(id), subtype.hashCode()));
1574 } else {
1575 setInputMethod(token, id);
1576 }
1577 }
satokab751aa2010-09-14 19:17:36 +09001578 }
1579
satok42c5a162011-05-26 16:46:14 +09001580 @Override
satokb416a71e2010-11-25 20:42:14 +09001581 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001582 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001583 synchronized (mMethodMap) {
1584 if (mCurClient == null || client == null
1585 || mCurClient.client.asBinder() != client.asBinder()) {
1586 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1587 }
satok7fee71f2010-12-17 18:54:26 +09001588 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1589 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001590 }
1591 }
1592
satok4fc87d62011-05-20 16:13:43 +09001593 @Override
satok735cf382010-11-11 20:40:09 +09001594 public boolean switchToLastInputMethod(IBinder token) {
1595 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001596 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001597 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001598 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001599 lastImi = mMethodMap.get(lastIme.first);
1600 } else {
1601 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001602 }
satok4fc87d62011-05-20 16:13:43 +09001603 String targetLastImiId = null;
1604 int subtypeId = NOT_A_SUBTYPE_ID;
1605 if (lastIme != null && lastImi != null) {
1606 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1607 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1608 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1609 : mCurrentSubtype.hashCode();
1610 // If the last IME is the same as the current IME and the last subtype is not
1611 // defined, there is no need to switch to the last IME.
1612 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1613 targetLastImiId = lastIme.first;
1614 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1615 }
1616 }
1617
1618 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1619 // This is a safety net. If the currentSubtype can't be added to the history
1620 // and the framework couldn't find the last ime, we will make the last ime be
1621 // the most applicable enabled keyboard subtype of the system imes.
1622 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1623 if (enabled != null) {
1624 final int N = enabled.size();
1625 final String locale = mCurrentSubtype == null
1626 ? mRes.getConfiguration().locale.toString()
1627 : mCurrentSubtype.getLocale();
1628 for (int i = 0; i < N; ++i) {
1629 final InputMethodInfo imi = enabled.get(i);
1630 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1631 InputMethodSubtype keyboardSubtype =
1632 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1633 SUBTYPE_MODE_KEYBOARD, locale, true);
1634 if (keyboardSubtype != null) {
1635 targetLastImiId = imi.getId();
1636 subtypeId = getSubtypeIdFromHashCode(
1637 imi, keyboardSubtype.hashCode());
1638 if(keyboardSubtype.getLocale().equals(locale)) {
1639 break;
1640 }
1641 }
1642 }
1643 }
1644 }
1645 }
1646
1647 if (!TextUtils.isEmpty(targetLastImiId)) {
1648 if (DEBUG) {
1649 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1650 + ", from: " + mCurMethodId + ", " + subtypeId);
1651 }
1652 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1653 return true;
1654 } else {
1655 return false;
1656 }
satok735cf382010-11-11 20:40:09 +09001657 }
1658 }
1659
satoke7c6998e2011-06-03 17:57:59 +09001660 @Override
satok68f1b782011-04-11 14:26:04 +09001661 public InputMethodSubtype getLastInputMethodSubtype() {
1662 synchronized (mMethodMap) {
1663 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1664 // TODO: Handle the case of the last IME with no subtypes
1665 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1666 || TextUtils.isEmpty(lastIme.second)) return null;
1667 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1668 if (lastImi == null) return null;
1669 try {
1670 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001671 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1672 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1673 return null;
1674 }
1675 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001676 } catch (NumberFormatException e) {
1677 return null;
1678 }
1679 }
1680 }
1681
satoke7c6998e2011-06-03 17:57:59 +09001682 @Override
satokee5e77c2011-09-02 18:50:15 +09001683 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001684 // By this IPC call, only a process which shares the same uid with the IME can add
1685 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001686 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001687 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001688 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001689 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001690 final PackageManager pm = mContext.getPackageManager();
1691 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1692 if (packageInfos != null) {
1693 final int packageNum = packageInfos.length;
1694 for (int i = 0; i < packageNum; ++i) {
1695 if (packageInfos[i].equals(imi.getPackageName())) {
1696 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001697 final long ident = Binder.clearCallingIdentity();
1698 try {
1699 buildInputMethodListLocked(mMethodList, mMethodMap);
1700 } finally {
1701 Binder.restoreCallingIdentity(ident);
1702 }
satokee5e77c2011-09-02 18:50:15 +09001703 return;
satok91e88122011-07-18 11:11:42 +09001704 }
1705 }
1706 }
satoke7c6998e2011-06-03 17:57:59 +09001707 }
satokee5e77c2011-09-02 18:50:15 +09001708 return;
satoke7c6998e2011-06-03 17:57:59 +09001709 }
1710
satok28203512010-11-24 11:06:49 +09001711 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 synchronized (mMethodMap) {
1713 if (token == null) {
1714 if (mContext.checkCallingOrSelfPermission(
1715 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1716 != PackageManager.PERMISSION_GRANTED) {
1717 throw new SecurityException(
1718 "Using null token requires permission "
1719 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1720 }
1721 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001722 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1723 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 return;
1725 }
1726
satokc5933802011-08-31 21:26:04 +09001727 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 try {
satokab751aa2010-09-14 19:17:36 +09001729 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 } finally {
1731 Binder.restoreCallingIdentity(ident);
1732 }
1733 }
1734 }
1735
satok42c5a162011-05-26 16:46:14 +09001736 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 public void hideMySoftInput(IBinder token, int flags) {
1738 synchronized (mMethodMap) {
1739 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001740 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1741 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 return;
1743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 long ident = Binder.clearCallingIdentity();
1745 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001746 hideCurrentInputLocked(flags, null);
1747 } finally {
1748 Binder.restoreCallingIdentity(ident);
1749 }
1750 }
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
satok42c5a162011-05-26 16:46:14 +09001753 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 public void showMySoftInput(IBinder token, int flags) {
1755 synchronized (mMethodMap) {
1756 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001757 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1758 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001759 return;
1760 }
1761 long ident = Binder.clearCallingIdentity();
1762 try {
1763 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 } finally {
1765 Binder.restoreCallingIdentity(ident);
1766 }
1767 }
1768 }
1769
1770 void setEnabledSessionInMainThread(SessionState session) {
1771 if (mEnabledSession != session) {
1772 if (mEnabledSession != null) {
1773 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001774 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 mEnabledSession.method.setSessionEnabled(
1776 mEnabledSession.session, false);
1777 } catch (RemoteException e) {
1778 }
1779 }
1780 mEnabledSession = session;
1781 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001782 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 session.method.setSessionEnabled(
1784 session.session, true);
1785 } catch (RemoteException e) {
1786 }
1787 }
1788 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001789
satok42c5a162011-05-26 16:46:14 +09001790 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 public boolean handleMessage(Message msg) {
1792 HandlerCaller.SomeArgs args;
1793 switch (msg.what) {
1794 case MSG_SHOW_IM_PICKER:
1795 showInputMethodMenu();
1796 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001797
satokab751aa2010-09-14 19:17:36 +09001798 case MSG_SHOW_IM_SUBTYPE_PICKER:
1799 showInputMethodSubtypeMenu();
1800 return true;
1801
satok47a44912010-10-06 16:03:58 +09001802 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001803 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001804 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001805 return true;
1806
1807 case MSG_SHOW_IM_CONFIG:
1808 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001809 return true;
1810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 case MSG_UNBIND_INPUT:
1814 try {
1815 ((IInputMethod)msg.obj).unbindInput();
1816 } catch (RemoteException e) {
1817 // There is nothing interesting about the method dying.
1818 }
1819 return true;
1820 case MSG_BIND_INPUT:
1821 args = (HandlerCaller.SomeArgs)msg.obj;
1822 try {
1823 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1824 } catch (RemoteException e) {
1825 }
1826 return true;
1827 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001828 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001830 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1831 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 } catch (RemoteException e) {
1833 }
1834 return true;
1835 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001838 ((IInputMethod)args.arg1).hideSoftInput(0,
1839 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 } catch (RemoteException e) {
1841 }
1842 return true;
1843 case MSG_ATTACH_TOKEN:
1844 args = (HandlerCaller.SomeArgs)msg.obj;
1845 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001846 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1848 } catch (RemoteException e) {
1849 }
1850 return true;
1851 case MSG_CREATE_SESSION:
1852 args = (HandlerCaller.SomeArgs)msg.obj;
1853 try {
1854 ((IInputMethod)args.arg1).createSession(
1855 (IInputMethodCallback)args.arg2);
1856 } catch (RemoteException e) {
1857 }
1858 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 case MSG_START_INPUT:
1862 args = (HandlerCaller.SomeArgs)msg.obj;
1863 try {
1864 SessionState session = (SessionState)args.arg1;
1865 setEnabledSessionInMainThread(session);
1866 session.method.startInput((IInputContext)args.arg2,
1867 (EditorInfo)args.arg3);
1868 } catch (RemoteException e) {
1869 }
1870 return true;
1871 case MSG_RESTART_INPUT:
1872 args = (HandlerCaller.SomeArgs)msg.obj;
1873 try {
1874 SessionState session = (SessionState)args.arg1;
1875 setEnabledSessionInMainThread(session);
1876 session.method.restartInput((IInputContext)args.arg2,
1877 (EditorInfo)args.arg3);
1878 } catch (RemoteException e) {
1879 }
1880 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 case MSG_UNBIND_METHOD:
1885 try {
1886 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1887 } catch (RemoteException e) {
1888 // There is nothing interesting about the last client dying.
1889 }
1890 return true;
1891 case MSG_BIND_METHOD:
1892 args = (HandlerCaller.SomeArgs)msg.obj;
1893 try {
1894 ((IInputMethodClient)args.arg1).onBindMethod(
1895 (InputBindResult)args.arg2);
1896 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001897 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 }
1899 return true;
1900 }
1901 return false;
1902 }
1903
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001904 private boolean isSystemIme(InputMethodInfo inputMethod) {
1905 return (inputMethod.getServiceInfo().applicationInfo.flags
1906 & ApplicationInfo.FLAG_SYSTEM) != 0;
1907 }
1908
Ken Wakasa586f0512011-01-20 22:31:01 +09001909 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1910 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1911 final int subtypeCount = imi.getSubtypeCount();
1912 for (int i = 0; i < subtypeCount; ++i) {
1913 subtypes.add(imi.getSubtypeAt(i));
1914 }
1915 return subtypes;
1916 }
1917
satoka86f5e42011-09-02 17:12:42 +09001918
1919 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1920 InputMethodInfo imi, String mode) {
1921 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1922 final int subtypeCount = imi.getSubtypeCount();
1923 for (int i = 0; i < subtypeCount; ++i) {
1924 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1925 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1926 subtypes.add(subtype);
1927 }
1928 }
1929 return subtypes;
1930 }
1931
satokdc9ddae2011-10-06 12:22:36 +09001932 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001933 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001934 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001935 // We'd prefer to fall back on a system IME, since that is safer.
1936 int i=enabled.size();
1937 while (i > 0) {
1938 i--;
satokdc9ddae2011-10-06 12:22:36 +09001939 final InputMethodInfo imi = enabled.get(i);
1940 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001941 break;
1942 }
1943 }
satokdc9ddae2011-10-06 12:22:36 +09001944 return enabled.get(i);
1945 }
1946 return null;
1947 }
1948
1949 private boolean chooseNewDefaultIMELocked() {
1950 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1951 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001952 if (DEBUG) {
1953 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1954 }
satok723a27e2010-11-11 14:58:11 +09001955 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001956 return true;
1957 }
1958
1959 return false;
1960 }
1961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1963 HashMap<String, InputMethodInfo> map) {
1964 list.clear();
1965 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001968 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001969 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1970 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1971 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1972 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973
1974 List<ResolveInfo> services = pm.queryIntentServices(
1975 new Intent(InputMethod.SERVICE_INTERFACE),
1976 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
satoke7c6998e2011-06-03 17:57:59 +09001978 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1979 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 for (int i = 0; i < services.size(); ++i) {
1981 ResolveInfo ri = services.get(i);
1982 ServiceInfo si = ri.serviceInfo;
1983 ComponentName compName = new ComponentName(si.packageName, si.name);
1984 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1985 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001986 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 + ": it does not require the permission "
1988 + android.Manifest.permission.BIND_INPUT_METHOD);
1989 continue;
1990 }
1991
Joe Onorato8a9b2202010-02-26 18:56:32 -08001992 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993
1994 try {
satoke7c6998e2011-06-03 17:57:59 +09001995 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001997 final String id = p.getId();
1998 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999
Amith Yamasanie861ec12010-03-24 21:39:27 -07002000 // System IMEs are enabled by default, unless there's a hard keyboard
2001 // and the system IME was explicitly disabled
2002 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2003 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002004 }
2005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002007 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002011 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002013 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
2015 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002016
2017 String defaultIme = Settings.Secure.getString(mContext
2018 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002019 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002020 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002021 updateFromSettingsLocked();
2022 }
2023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002027
satokab751aa2010-09-14 19:17:36 +09002028 private void showInputMethodMenu() {
2029 showInputMethodMenuInternal(false);
2030 }
2031
2032 private void showInputMethodSubtypeMenu() {
2033 showInputMethodMenuInternal(true);
2034 }
2035
satok217f5482010-12-15 05:19:19 +09002036 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002037 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002038 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002039 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2040 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002041 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002042 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002043 }
satok217f5482010-12-15 05:19:19 +09002044 mContext.startActivity(intent);
2045 }
2046
2047 private void showConfigureInputMethods() {
2048 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2049 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2050 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2051 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002052 mContext.startActivity(intent);
2053 }
2054
satokab751aa2010-09-14 19:17:36 +09002055 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002056 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002060 final boolean isScreenLocked = mKeyguardManager != null
2061 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 String lastInputMethodId = Settings.Secure.getString(context
2064 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002065 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002066 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002067
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002068 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002069 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2070 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002071 if (immis == null || immis.size() == 0) {
2072 return;
2073 }
2074
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002075 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076
Ken Wakasa761eb372011-03-04 19:06:18 +09002077 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2078 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2079 new Comparator<InputMethodInfo>() {
2080 @Override
2081 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2082 if (imi2 == null) return 0;
2083 if (imi1 == null) return 1;
2084 if (pm == null) {
2085 return imi1.getId().compareTo(imi2.getId());
2086 }
2087 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2088 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2089 return imiId1.toString().compareTo(imiId2.toString());
2090 }
2091 });
satok913a8922010-08-26 21:53:41 +09002092
Ken Wakasa761eb372011-03-04 19:06:18 +09002093 sortedImmis.putAll(immis);
2094
Ken Wakasa05dbb652011-08-22 15:22:43 +09002095 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002096
2097 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002098 if (imi == null) continue;
2099 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002100 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002101 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2102 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002103 }
satokbb4aa062011-01-19 21:40:27 +09002104 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002105 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002106 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002107 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002108 if (DEBUG) {
2109 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2110 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002111 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002112 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2113 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2114 // We show all enabled IMEs and subtypes when an IME is shown.
2115 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002116 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002117 final CharSequence subtypeLabel =
2118 subtype.overridesImplicitlyEnabledSubtype() ? null
2119 : subtype.getDisplayName(context, imi.getPackageName(),
2120 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002121 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2122
satok4a28bde2011-06-29 21:03:40 +09002123 // Removing this subtype from enabledSubtypeSet because we no longer
2124 // need to add an entry of this subtype to imList to avoid duplicated
2125 // entries.
2126 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002127 }
satokab751aa2010-09-14 19:17:36 +09002128 }
2129 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002130 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002131 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002132 }
satok913a8922010-08-26 21:53:41 +09002133
Ken Wakasa761eb372011-03-04 19:06:18 +09002134 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002135 mIms = new InputMethodInfo[N];
2136 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002137 int checkedItem = 0;
2138 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002139 final ImeSubtypeListItem item = imList.get(i);
2140 mIms[i] = item.mImi;
2141 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002142 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002143 int subtypeId = mSubtypeIds[i];
2144 if ((subtypeId == NOT_A_SUBTYPE_ID)
2145 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2146 || (subtypeId == lastInputMethodSubtypeId)) {
2147 checkedItem = i;
2148 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
satokab751aa2010-09-14 19:17:36 +09002151
Ken Wakasa05dbb652011-08-22 15:22:43 +09002152 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002153 com.android.internal.R.styleable.DialogPreference,
2154 com.android.internal.R.attr.alertDialogStyle, 0);
2155 mDialogBuilder = new AlertDialog.Builder(context)
2156 .setTitle(com.android.internal.R.string.select_input_method)
2157 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002158 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002159 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002161 }
2162 })
2163 .setIcon(a.getDrawable(
2164 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2165 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002166
Ken Wakasa05dbb652011-08-22 15:22:43 +09002167 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2168 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2169 checkedItem);
2170
2171 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002172 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002173 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002174 public void onClick(DialogInterface dialog, int which) {
2175 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002176 if (mIms == null || mIms.length <= which
2177 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002178 return;
2179 }
2180 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002181 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002182 hideInputMethodMenu();
2183 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002184 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002185 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002186 subtypeId = NOT_A_SUBTYPE_ID;
2187 }
2188 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002189 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002192 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193
satokbc81b692011-08-26 16:22:22 +09002194 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002195 mDialogBuilder.setPositiveButton(
2196 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002197 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002198 @Override
satok7f35c8c2010-10-07 21:13:11 +09002199 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002200 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002201 }
2202 });
2203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002205 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 mSwitchingDialog.getWindow().setType(
2207 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002208 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 mSwitchingDialog.show();
2210 }
2211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002212
Ken Wakasa05dbb652011-08-22 15:22:43 +09002213 private static class ImeSubtypeListItem {
2214 public final CharSequence mImeName;
2215 public final CharSequence mSubtypeName;
2216 public final InputMethodInfo mImi;
2217 public final int mSubtypeId;
2218 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2219 InputMethodInfo imi, int subtypeId) {
2220 mImeName = imeName;
2221 mSubtypeName = subtypeName;
2222 mImi = imi;
2223 mSubtypeId = subtypeId;
2224 }
2225 }
2226
2227 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2228 private final LayoutInflater mInflater;
2229 private final int mTextViewResourceId;
2230 private final List<ImeSubtypeListItem> mItemsList;
2231 private final int mCheckedItem;
2232 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2233 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2234 super(context, textViewResourceId, itemsList);
2235 mTextViewResourceId = textViewResourceId;
2236 mItemsList = itemsList;
2237 mCheckedItem = checkedItem;
2238 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2239 }
2240
2241 @Override
2242 public View getView(int position, View convertView, ViewGroup parent) {
2243 final View view = convertView != null ? convertView
2244 : mInflater.inflate(mTextViewResourceId, null);
2245 if (position < 0 || position >= mItemsList.size()) return view;
2246 final ImeSubtypeListItem item = mItemsList.get(position);
2247 final CharSequence imeName = item.mImeName;
2248 final CharSequence subtypeName = item.mSubtypeName;
2249 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2250 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2251 if (TextUtils.isEmpty(subtypeName)) {
2252 firstTextView.setText(imeName);
2253 secondTextView.setVisibility(View.GONE);
2254 } else {
2255 firstTextView.setText(subtypeName);
2256 secondTextView.setText(imeName);
2257 secondTextView.setVisibility(View.VISIBLE);
2258 }
2259 final RadioButton radioButton =
2260 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2261 radioButton.setChecked(position == mCheckedItem);
2262 return view;
2263 }
2264 }
2265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002267 synchronized (mMethodMap) {
2268 hideInputMethodMenuLocked();
2269 }
2270 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002271
The Android Open Source Project10592532009-03-18 17:39:46 -07002272 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002273 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274
The Android Open Source Project10592532009-03-18 17:39:46 -07002275 if (mSwitchingDialog != null) {
2276 mSwitchingDialog.dismiss();
2277 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002279
The Android Open Source Project10592532009-03-18 17:39:46 -07002280 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002281 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002285
satok42c5a162011-05-26 16:46:14 +09002286 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 public boolean setInputMethodEnabled(String id, boolean enabled) {
2288 synchronized (mMethodMap) {
2289 if (mContext.checkCallingOrSelfPermission(
2290 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2291 != PackageManager.PERMISSION_GRANTED) {
2292 throw new SecurityException(
2293 "Requires permission "
2294 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2295 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 long ident = Binder.clearCallingIdentity();
2298 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002299 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 } finally {
2301 Binder.restoreCallingIdentity(ident);
2302 }
2303 }
2304 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002305
2306 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2307 // Make sure this is a valid input method.
2308 InputMethodInfo imm = mMethodMap.get(id);
2309 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002310 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002311 }
2312
satokd87c2592010-09-29 11:52:06 +09002313 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2314 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002315
satokd87c2592010-09-29 11:52:06 +09002316 if (enabled) {
2317 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2318 if (pair.first.equals(id)) {
2319 // We are enabling this input method, but it is already enabled.
2320 // Nothing to do. The previous state was enabled.
2321 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002322 }
2323 }
satokd87c2592010-09-29 11:52:06 +09002324 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2325 // Previous state was disabled.
2326 return false;
2327 } else {
2328 StringBuilder builder = new StringBuilder();
2329 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2330 builder, enabledInputMethodsList, id)) {
2331 // Disabled input method is currently selected, switch to another one.
2332 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2333 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002334 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2335 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2336 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002337 }
2338 // Previous state was enabled.
2339 return true;
2340 } else {
2341 // We are disabling the input method but it is already disabled.
2342 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002343 return false;
2344 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002345 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002346 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002347
satok57ffc002011-01-25 00:11:47 +09002348 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2349 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002350 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002351 }
2352
satok723a27e2010-11-11 14:58:11 +09002353 private void saveCurrentInputMethodAndSubtypeToHistory() {
2354 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2355 if (mCurrentSubtype != null) {
2356 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2357 }
satok57ffc002011-01-25 00:11:47 +09002358 if (canAddToLastInputMethod(mCurrentSubtype)) {
2359 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2360 }
satokab751aa2010-09-14 19:17:36 +09002361 }
2362
satok723a27e2010-11-11 14:58:11 +09002363 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2364 boolean setSubtypeOnly) {
2365 // Update the history of InputMethod and Subtype
2366 saveCurrentInputMethodAndSubtypeToHistory();
2367
2368 // Set Subtype here
2369 if (imi == null || subtypeId < 0) {
2370 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002371 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002372 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002373 if (subtypeId < imi.getSubtypeCount()) {
2374 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2375 mSettings.putSelectedSubtype(subtype.hashCode());
2376 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002377 } else {
2378 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2379 mCurrentSubtype = null;
2380 }
satokab751aa2010-09-14 19:17:36 +09002381 }
satok723a27e2010-11-11 14:58:11 +09002382
2383 if (!setSubtypeOnly) {
2384 // Set InputMethod here
2385 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2386 }
2387 }
2388
2389 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2390 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2391 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2392 // newDefaultIme is empty when there is no candidate for the selected IME.
2393 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2394 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2395 if (subtypeHashCode != null) {
2396 try {
2397 lastSubtypeId = getSubtypeIdFromHashCode(
2398 imi, Integer.valueOf(subtypeHashCode));
2399 } catch (NumberFormatException e) {
2400 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2401 }
2402 }
2403 }
2404 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002405 }
2406
2407 private int getSelectedInputMethodSubtypeId(String id) {
2408 InputMethodInfo imi = mMethodMap.get(id);
2409 if (imi == null) {
2410 return NOT_A_SUBTYPE_ID;
2411 }
satokab751aa2010-09-14 19:17:36 +09002412 int subtypeId;
2413 try {
2414 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2415 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2416 } catch (SettingNotFoundException e) {
2417 return NOT_A_SUBTYPE_ID;
2418 }
satok723a27e2010-11-11 14:58:11 +09002419 return getSubtypeIdFromHashCode(imi, subtypeId);
2420 }
2421
2422 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002423 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002424 final int subtypeCount = imi.getSubtypeCount();
2425 for (int i = 0; i < subtypeCount; ++i) {
2426 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002427 if (subtypeHashCode == ims.hashCode()) {
2428 return i;
2429 }
satokab751aa2010-09-14 19:17:36 +09002430 }
2431 }
2432 return NOT_A_SUBTYPE_ID;
2433 }
2434
satoka86f5e42011-09-02 17:12:42 +09002435 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2436 Resources res, InputMethodInfo imi) {
2437 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002438 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002439 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002440 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002441 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002442 final int N = subtypes.size();
2443 boolean containsKeyboardSubtype = false;
2444 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002445 // scan overriding implicitly enabled subtypes.
2446 InputMethodSubtype subtype = subtypes.get(i);
2447 if (subtype.overridesImplicitlyEnabledSubtype()) {
2448 final String mode = subtype.getMode();
2449 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2450 applicableModeAndSubtypesMap.put(mode, subtype);
2451 }
2452 }
2453 }
2454 if (applicableModeAndSubtypesMap.size() > 0) {
2455 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2456 }
2457 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002458 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002459 final String locale = subtype.getLocale();
2460 final String mode = subtype.getMode();
2461 // When system locale starts with subtype's locale, that subtype will be applicable
2462 // for system locale
2463 // For instance, it's clearly applicable for cases like system locale = en_US and
2464 // subtype = en, but it is not necessarily considered applicable for cases like system
2465 // locale = en and subtype = en_US.
2466 // We just call systemLocale.startsWith(locale) in this function because there is no
2467 // need to find applicable subtypes aggressively unlike
2468 // findLastResortApplicableSubtypeLocked.
2469 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002470 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002471 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002472 if (applicableSubtype != null) {
2473 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2474 if (!systemLocale.equals(locale)) continue;
2475 }
satok3da92232011-01-11 22:46:30 +09002476 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002477 if (!containsKeyboardSubtype
2478 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2479 containsKeyboardSubtype = true;
2480 }
2481 }
2482 }
satok4a28bde2011-06-29 21:03:40 +09002483 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002484 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002485 if (!containsKeyboardSubtype) {
2486 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002487 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002488 if (lastResortKeyboardSubtype != null) {
2489 applicableSubtypes.add(lastResortKeyboardSubtype);
2490 }
2491 }
2492 return applicableSubtypes;
2493 }
2494
satok4e4569d2010-11-19 18:45:53 +09002495 /**
2496 * If there are no selected subtypes, tries finding the most applicable one according to the
2497 * given locale.
2498 * @param subtypes this function will search the most applicable subtype in subtypes
2499 * @param mode subtypes will be filtered by mode
2500 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002501 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2502 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002503 * @return the most applicable subtypeId
2504 */
satokdf31ae62011-01-15 06:19:44 +09002505 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2506 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002507 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002508 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002509 return null;
satok8fbb1e82010-11-02 23:15:58 +09002510 }
satok4e4569d2010-11-19 18:45:53 +09002511 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002512 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002513 }
satok8fbb1e82010-11-02 23:15:58 +09002514 final String language = locale.substring(0, 2);
2515 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002516 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002517 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002518 final int N = subtypes.size();
2519 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002520 InputMethodSubtype subtype = subtypes.get(i);
2521 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002522 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2523 // and all subtypes with all modes can be candidates.
2524 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002525 if (firstMatchedModeSubtype == null) {
2526 firstMatchedModeSubtype = subtype;
2527 }
satok9ef02832010-11-04 21:17:48 +09002528 if (locale.equals(subtypeLocale)) {
2529 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002530 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002531 break;
2532 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2533 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002534 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002535 partialMatchFound = true;
2536 }
satok8fbb1e82010-11-02 23:15:58 +09002537 }
2538 }
2539
satok7599a7fb2010-12-22 13:45:23 +09002540 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2541 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002542 }
2543
satok8fbb1e82010-11-02 23:15:58 +09002544 // The first subtype applicable to the system locale will be defined as the most applicable
2545 // subtype.
2546 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002547 if (applicableSubtype != null) {
2548 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2549 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2550 }
satok8fbb1e82010-11-02 23:15:58 +09002551 }
satokcd7cd292010-11-20 15:46:23 +09002552 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002553 }
2554
satok4e4569d2010-11-19 18:45:53 +09002555 // If there are no selected shortcuts, tries finding the most applicable ones.
2556 private Pair<InputMethodInfo, InputMethodSubtype>
2557 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2558 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2559 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002560 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002561 boolean foundInSystemIME = false;
2562
2563 // Search applicable subtype for each InputMethodInfo
2564 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002565 final String imiId = imi.getId();
2566 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2567 continue;
2568 }
satokcd7cd292010-11-20 15:46:23 +09002569 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002570 final List<InputMethodSubtype> enabledSubtypes =
2571 getEnabledInputMethodSubtypeList(imi, true);
2572 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002573 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002574 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002575 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002576 }
satokdf31ae62011-01-15 06:19:44 +09002577 // 2. Search by the system locale from enabledSubtypes.
2578 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002579 if (subtype == null) {
2580 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002581 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002582 }
satoka86f5e42011-09-02 17:12:42 +09002583 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2584 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2585 final ArrayList<InputMethodSubtype> subtypesForSearch =
2586 overridingImplicitlyEnabledSubtypes.isEmpty()
2587 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002588 // 4. Search by the current subtype's locale from all subtypes.
2589 if (subtype == null && mCurrentSubtype != null) {
2590 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002591 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002592 }
2593 // 5. Search by the system locale from all subtypes.
2594 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002595 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002596 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002597 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002598 }
satokcd7cd292010-11-20 15:46:23 +09002599 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002600 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002601 // The current input method is the most applicable IME.
2602 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002603 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002604 break;
satok7599a7fb2010-12-22 13:45:23 +09002605 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002606 // The system input method is 2nd applicable IME.
2607 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002608 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002609 if ((imi.getServiceInfo().applicationInfo.flags
2610 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2611 foundInSystemIME = true;
2612 }
satok4e4569d2010-11-19 18:45:53 +09002613 }
2614 }
2615 }
2616 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002617 if (mostApplicableIMI != null) {
2618 Slog.w(TAG, "Most applicable shortcut input method was:"
2619 + mostApplicableIMI.getId());
2620 if (mostApplicableSubtype != null) {
2621 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2622 + "," + mostApplicableSubtype.getMode() + ","
2623 + mostApplicableSubtype.getLocale());
2624 }
2625 }
satok4e4569d2010-11-19 18:45:53 +09002626 }
satokcd7cd292010-11-20 15:46:23 +09002627 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002628 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002629 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002630 } else {
2631 return null;
2632 }
2633 }
2634
satokab751aa2010-09-14 19:17:36 +09002635 /**
2636 * @return Return the current subtype of this input method.
2637 */
satok42c5a162011-05-26 16:46:14 +09002638 @Override
satokab751aa2010-09-14 19:17:36 +09002639 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002640 boolean subtypeIsSelected = false;
2641 try {
2642 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2643 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2644 } catch (SettingNotFoundException e) {
2645 }
satok3ef8b292010-11-23 06:06:29 +09002646 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002647 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002648 String lastInputMethodId = Settings.Secure.getString(
2649 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002650 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2651 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002652 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2653 if (imi != null) {
2654 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002655 // the most applicable subtype from explicitly or implicitly enabled
2656 // subtypes.
2657 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2658 getEnabledInputMethodSubtypeList(imi, true);
2659 // If there is only one explicitly or implicitly enabled subtype,
2660 // just returns it.
2661 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2662 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2663 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2664 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2665 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2666 SUBTYPE_MODE_KEYBOARD, null, true);
2667 if (mCurrentSubtype == null) {
2668 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2669 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2670 true);
2671 }
2672 }
satok4e4569d2010-11-19 18:45:53 +09002673 }
satokcd7cd292010-11-20 15:46:23 +09002674 } else {
satok3ef8b292010-11-23 06:06:29 +09002675 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002676 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002677 }
satok8fbb1e82010-11-02 23:15:58 +09002678 }
satok3ef8b292010-11-23 06:06:29 +09002679 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002680 }
satokab751aa2010-09-14 19:17:36 +09002681 }
2682
satokf3db1af2010-11-23 13:34:33 +09002683 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2684 InputMethodSubtype subtype) {
2685 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2686 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2687 } else {
2688 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2689 subtypes.add(subtype);
2690 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2691 }
2692 }
2693
satok4e4569d2010-11-19 18:45:53 +09002694 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002695 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002696 @Override
satok4e4569d2010-11-19 18:45:53 +09002697 public List getShortcutInputMethodsAndSubtypes() {
2698 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002699 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002700 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002701 // If there are no selected shortcut subtypes, the framework will try to find
2702 // the most applicable subtype from all subtypes whose mode is
2703 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002704 Pair<InputMethodInfo, InputMethodSubtype> info =
2705 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2706 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002707 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002708 ret.add(info.first);
2709 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002710 }
satok3da92232011-01-11 22:46:30 +09002711 return ret;
satokf3db1af2010-11-23 13:34:33 +09002712 }
satokf3db1af2010-11-23 13:34:33 +09002713 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2714 ret.add(imi);
2715 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2716 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002717 }
2718 }
satokf3db1af2010-11-23 13:34:33 +09002719 return ret;
satok4e4569d2010-11-19 18:45:53 +09002720 }
2721 }
2722
satok42c5a162011-05-26 16:46:14 +09002723 @Override
satokb66d2872010-11-10 01:04:04 +09002724 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2725 synchronized (mMethodMap) {
2726 if (subtype != null && mCurMethodId != null) {
2727 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2728 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2729 if (subtypeId != NOT_A_SUBTYPE_ID) {
2730 setInputMethodLocked(mCurMethodId, subtypeId);
2731 return true;
2732 }
2733 }
2734 return false;
2735 }
2736 }
2737
satokd87c2592010-09-29 11:52:06 +09002738 /**
2739 * Utility class for putting and getting settings for InputMethod
2740 * TODO: Move all putters and getters of settings to this class.
2741 */
2742 private static class InputMethodSettings {
2743 // The string for enabled input method is saved as follows:
2744 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2745 private static final char INPUT_METHOD_SEPARATER = ':';
2746 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002747 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002748 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2749
satok723a27e2010-11-11 14:58:11 +09002750 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002751 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2752
satokdf31ae62011-01-15 06:19:44 +09002753 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002754 private final ContentResolver mResolver;
2755 private final HashMap<String, InputMethodInfo> mMethodMap;
2756 private final ArrayList<InputMethodInfo> mMethodList;
2757
2758 private String mEnabledInputMethodsStrCache;
2759
2760 private static void buildEnabledInputMethodsSettingString(
2761 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2762 String id = pair.first;
2763 ArrayList<String> subtypes = pair.second;
2764 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002765 // Inputmethod and subtypes are saved in the settings as follows:
2766 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2767 for (String subtypeId: subtypes) {
2768 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002769 }
2770 }
2771
2772 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002773 Resources res, ContentResolver resolver,
2774 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2775 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002776 mResolver = resolver;
2777 mMethodMap = methodMap;
2778 mMethodList = methodList;
2779 }
2780
2781 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2782 return createEnabledInputMethodListLocked(
2783 getEnabledInputMethodsAndSubtypeListLocked());
2784 }
2785
satok7f35c8c2010-10-07 21:13:11 +09002786 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002787 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2788 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002789 getEnabledInputMethodsAndSubtypeListLocked());
2790 }
2791
satok67ddf9c2010-11-17 09:45:54 +09002792 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2793 InputMethodInfo imi) {
2794 List<Pair<String, ArrayList<String>>> imsList =
2795 getEnabledInputMethodsAndSubtypeListLocked();
2796 ArrayList<InputMethodSubtype> enabledSubtypes =
2797 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002798 if (imi != null) {
2799 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2800 InputMethodInfo info = mMethodMap.get(imsPair.first);
2801 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002802 final int subtypeCount = info.getSubtypeCount();
2803 for (int i = 0; i < subtypeCount; ++i) {
2804 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002805 for (String s: imsPair.second) {
2806 if (String.valueOf(ims.hashCode()).equals(s)) {
2807 enabledSubtypes.add(ims);
2808 }
satok67ddf9c2010-11-17 09:45:54 +09002809 }
2810 }
satok884ef9a2010-11-18 10:39:46 +09002811 break;
satok67ddf9c2010-11-17 09:45:54 +09002812 }
satok67ddf9c2010-11-17 09:45:54 +09002813 }
2814 }
2815 return enabledSubtypes;
2816 }
2817
satokd87c2592010-09-29 11:52:06 +09002818 // At the initial boot, the settings for input methods are not set,
2819 // so we need to enable IME in that case.
2820 public void enableAllIMEsIfThereIsNoEnabledIME() {
2821 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2822 StringBuilder sb = new StringBuilder();
2823 final int N = mMethodList.size();
2824 for (int i = 0; i < N; i++) {
2825 InputMethodInfo imi = mMethodList.get(i);
2826 Slog.i(TAG, "Adding: " + imi.getId());
2827 if (i > 0) sb.append(':');
2828 sb.append(imi.getId());
2829 }
2830 putEnabledInputMethodsStr(sb.toString());
2831 }
2832 }
2833
satokbb4aa062011-01-19 21:40:27 +09002834 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002835 ArrayList<Pair<String, ArrayList<String>>> imsList
2836 = new ArrayList<Pair<String, ArrayList<String>>>();
2837 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2838 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2839 return imsList;
2840 }
satok723a27e2010-11-11 14:58:11 +09002841 mInputMethodSplitter.setString(enabledInputMethodsStr);
2842 while (mInputMethodSplitter.hasNext()) {
2843 String nextImsStr = mInputMethodSplitter.next();
2844 mSubtypeSplitter.setString(nextImsStr);
2845 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002846 ArrayList<String> subtypeHashes = new ArrayList<String>();
2847 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002848 String imeId = mSubtypeSplitter.next();
2849 while (mSubtypeSplitter.hasNext()) {
2850 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002851 }
2852 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2853 }
2854 }
2855 return imsList;
2856 }
2857
2858 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2859 if (reloadInputMethodStr) {
2860 getEnabledInputMethodsStr();
2861 }
2862 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2863 // Add in the newly enabled input method.
2864 putEnabledInputMethodsStr(id);
2865 } else {
2866 putEnabledInputMethodsStr(
2867 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2868 }
2869 }
2870
2871 /**
2872 * Build and put a string of EnabledInputMethods with removing specified Id.
2873 * @return the specified id was removed or not.
2874 */
2875 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2876 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2877 boolean isRemoved = false;
2878 boolean needsAppendSeparator = false;
2879 for (Pair<String, ArrayList<String>> ims: imsList) {
2880 String curId = ims.first;
2881 if (curId.equals(id)) {
2882 // We are disabling this input method, and it is
2883 // currently enabled. Skip it to remove from the
2884 // new list.
2885 isRemoved = true;
2886 } else {
2887 if (needsAppendSeparator) {
2888 builder.append(INPUT_METHOD_SEPARATER);
2889 } else {
2890 needsAppendSeparator = true;
2891 }
2892 buildEnabledInputMethodsSettingString(builder, ims);
2893 }
2894 }
2895 if (isRemoved) {
2896 // Update the setting with the new list of input methods.
2897 putEnabledInputMethodsStr(builder.toString());
2898 }
2899 return isRemoved;
2900 }
2901
2902 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2903 List<Pair<String, ArrayList<String>>> imsList) {
2904 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2905 for (Pair<String, ArrayList<String>> ims: imsList) {
2906 InputMethodInfo info = mMethodMap.get(ims.first);
2907 if (info != null) {
2908 res.add(info);
2909 }
2910 }
2911 return res;
2912 }
2913
satok7f35c8c2010-10-07 21:13:11 +09002914 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002915 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002916 List<Pair<String, ArrayList<String>>> imsList) {
2917 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2918 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2919 for (Pair<String, ArrayList<String>> ims : imsList) {
2920 InputMethodInfo info = mMethodMap.get(ims.first);
2921 if (info != null) {
2922 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2923 }
2924 }
2925 return res;
2926 }
2927
satokd87c2592010-09-29 11:52:06 +09002928 private void putEnabledInputMethodsStr(String str) {
2929 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2930 mEnabledInputMethodsStrCache = str;
2931 }
2932
2933 private String getEnabledInputMethodsStr() {
2934 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2935 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002936 if (DEBUG) {
2937 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2938 }
satokd87c2592010-09-29 11:52:06 +09002939 return mEnabledInputMethodsStrCache;
2940 }
satok723a27e2010-11-11 14:58:11 +09002941
2942 private void saveSubtypeHistory(
2943 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2944 StringBuilder builder = new StringBuilder();
2945 boolean isImeAdded = false;
2946 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2947 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2948 newSubtypeId);
2949 isImeAdded = true;
2950 }
2951 for (Pair<String, String> ime: savedImes) {
2952 String imeId = ime.first;
2953 String subtypeId = ime.second;
2954 if (TextUtils.isEmpty(subtypeId)) {
2955 subtypeId = NOT_A_SUBTYPE_ID_STR;
2956 }
2957 if (isImeAdded) {
2958 builder.append(INPUT_METHOD_SEPARATER);
2959 } else {
2960 isImeAdded = true;
2961 }
2962 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2963 subtypeId);
2964 }
2965 // Remove the last INPUT_METHOD_SEPARATER
2966 putSubtypeHistoryStr(builder.toString());
2967 }
2968
2969 public void addSubtypeToHistory(String imeId, String subtypeId) {
2970 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2971 for (Pair<String, String> ime: subtypeHistory) {
2972 if (ime.first.equals(imeId)) {
2973 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002974 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002975 + ime.second);
2976 }
2977 // We should break here
2978 subtypeHistory.remove(ime);
2979 break;
2980 }
2981 }
satokbb4aa062011-01-19 21:40:27 +09002982 if (DEBUG) {
2983 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2984 }
satok723a27e2010-11-11 14:58:11 +09002985 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2986 }
2987
2988 private void putSubtypeHistoryStr(String str) {
2989 if (DEBUG) {
2990 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2991 }
2992 Settings.Secure.putString(
2993 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2994 }
2995
2996 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2997 // Gets the first one from the history
2998 return getLastSubtypeForInputMethodLockedInternal(null);
2999 }
3000
3001 public String getLastSubtypeForInputMethodLocked(String imeId) {
3002 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3003 if (ime != null) {
3004 return ime.second;
3005 } else {
3006 return null;
3007 }
3008 }
3009
3010 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3011 List<Pair<String, ArrayList<String>>> enabledImes =
3012 getEnabledInputMethodsAndSubtypeListLocked();
3013 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003014 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003015 final String imeInTheHistory = imeAndSubtype.first;
3016 // If imeId is empty, returns the first IME and subtype in the history
3017 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3018 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003019 final String subtypeHashCode =
3020 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3021 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003022 if (!TextUtils.isEmpty(subtypeHashCode)) {
3023 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003024 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003025 }
3026 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3027 }
3028 }
3029 }
3030 if (DEBUG) {
3031 Slog.d(TAG, "No enabled IME found in the history");
3032 }
3033 return null;
3034 }
3035
satokdf31ae62011-01-15 06:19:44 +09003036 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003037 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3038 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3039 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003040 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3041 if (explicitlyEnabledSubtypes.size() == 0) {
3042 // If there are no explicitly enabled subtypes, applicable subtypes are
3043 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003044 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003045 // If IME is enabled and no subtypes are enabled, applicable subtypes
3046 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003047 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003048 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003049 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003050 if (implicitlySelectedSubtypes != null) {
3051 final int N = implicitlySelectedSubtypes.size();
3052 for (int i = 0; i < N; ++i) {
3053 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3054 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3055 return subtypeHashCode;
3056 }
3057 }
3058 }
3059 }
3060 } else {
satokf6cafb62011-01-17 16:29:02 +09003061 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003062 if (s.equals(subtypeHashCode)) {
3063 // If both imeId and subtypeId are enabled, return subtypeId.
3064 return s;
3065 }
satok723a27e2010-11-11 14:58:11 +09003066 }
3067 }
3068 // If imeId was enabled but subtypeId was disabled.
3069 return NOT_A_SUBTYPE_ID_STR;
3070 }
3071 }
3072 // If both imeId and subtypeId are disabled, return null
3073 return null;
3074 }
3075
3076 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3077 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3078 final String subtypeHistoryStr = getSubtypeHistoryStr();
3079 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3080 return imsList;
3081 }
3082 mInputMethodSplitter.setString(subtypeHistoryStr);
3083 while (mInputMethodSplitter.hasNext()) {
3084 String nextImsStr = mInputMethodSplitter.next();
3085 mSubtypeSplitter.setString(nextImsStr);
3086 if (mSubtypeSplitter.hasNext()) {
3087 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3088 // The first element is ime id.
3089 String imeId = mSubtypeSplitter.next();
3090 while (mSubtypeSplitter.hasNext()) {
3091 subtypeId = mSubtypeSplitter.next();
3092 break;
3093 }
3094 imsList.add(new Pair<String, String>(imeId, subtypeId));
3095 }
3096 }
3097 return imsList;
3098 }
3099
3100 private String getSubtypeHistoryStr() {
3101 if (DEBUG) {
3102 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3103 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3104 }
3105 return Settings.Secure.getString(
3106 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3107 }
3108
3109 public void putSelectedInputMethod(String imeId) {
3110 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3111 }
3112
3113 public void putSelectedSubtype(int subtypeId) {
3114 Settings.Secure.putInt(
3115 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3116 }
satokd87c2592010-09-29 11:52:06 +09003117 }
3118
satoke7c6998e2011-06-03 17:57:59 +09003119 private static class InputMethodFileManager {
3120 private static final String SYSTEM_PATH = "system";
3121 private static final String INPUT_METHOD_PATH = "inputmethod";
3122 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3123 private static final String NODE_SUBTYPES = "subtypes";
3124 private static final String NODE_SUBTYPE = "subtype";
3125 private static final String NODE_IMI = "imi";
3126 private static final String ATTR_ID = "id";
3127 private static final String ATTR_LABEL = "label";
3128 private static final String ATTR_ICON = "icon";
3129 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3130 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3131 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3132 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3133 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3134 private final HashMap<String, InputMethodInfo> mMethodMap;
3135 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3136 new HashMap<String, List<InputMethodSubtype>>();
3137 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3138 if (methodMap == null) {
3139 throw new NullPointerException("methodMap is null");
3140 }
3141 mMethodMap = methodMap;
3142 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3143 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3144 if (!inputMethodDir.mkdirs()) {
3145 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3146 }
3147 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3148 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3149 if (!subtypeFile.exists()) {
3150 // If "subtypes.xml" doesn't exist, create a blank file.
3151 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3152 methodMap);
3153 } else {
3154 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3155 }
3156 }
3157
3158 private void deleteAllInputMethodSubtypes(String imiId) {
3159 synchronized (mMethodMap) {
3160 mSubtypesMap.remove(imiId);
3161 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3162 mMethodMap);
3163 }
3164 }
3165
3166 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003167 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003168 synchronized (mMethodMap) {
3169 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3170 final int N = additionalSubtypes.length;
3171 for (int i = 0; i < N; ++i) {
3172 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003173 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003174 subtypes.add(subtype);
3175 }
3176 }
satok4a28bde2011-06-29 21:03:40 +09003177 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003178 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3179 mMethodMap);
3180 }
3181 }
3182
3183 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3184 synchronized (mMethodMap) {
3185 return mSubtypesMap;
3186 }
3187 }
3188
3189 private static void writeAdditionalInputMethodSubtypes(
3190 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3191 HashMap<String, InputMethodInfo> methodMap) {
3192 // Safety net for the case that this function is called before methodMap is set.
3193 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3194 FileOutputStream fos = null;
3195 try {
3196 fos = subtypesFile.startWrite();
3197 final XmlSerializer out = new FastXmlSerializer();
3198 out.setOutput(fos, "utf-8");
3199 out.startDocument(null, true);
3200 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3201 out.startTag(null, NODE_SUBTYPES);
3202 for (String imiId : allSubtypes.keySet()) {
3203 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3204 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3205 continue;
3206 }
3207 out.startTag(null, NODE_IMI);
3208 out.attribute(null, ATTR_ID, imiId);
3209 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3210 final int N = subtypesList.size();
3211 for (int i = 0; i < N; ++i) {
3212 final InputMethodSubtype subtype = subtypesList.get(i);
3213 out.startTag(null, NODE_SUBTYPE);
3214 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3215 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3216 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3217 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3218 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3219 out.attribute(null, ATTR_IS_AUXILIARY,
3220 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3221 out.endTag(null, NODE_SUBTYPE);
3222 }
3223 out.endTag(null, NODE_IMI);
3224 }
3225 out.endTag(null, NODE_SUBTYPES);
3226 out.endDocument();
3227 subtypesFile.finishWrite(fos);
3228 } catch (java.io.IOException e) {
3229 Slog.w(TAG, "Error writing subtypes", e);
3230 if (fos != null) {
3231 subtypesFile.failWrite(fos);
3232 }
3233 }
3234 }
3235
3236 private static void readAdditionalInputMethodSubtypes(
3237 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3238 if (allSubtypes == null || subtypesFile == null) return;
3239 allSubtypes.clear();
3240 FileInputStream fis = null;
3241 try {
3242 fis = subtypesFile.openRead();
3243 final XmlPullParser parser = Xml.newPullParser();
3244 parser.setInput(fis, null);
3245 int type = parser.getEventType();
3246 // Skip parsing until START_TAG
3247 while ((type = parser.next()) != XmlPullParser.START_TAG
3248 && type != XmlPullParser.END_DOCUMENT) {}
3249 String firstNodeName = parser.getName();
3250 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3251 throw new XmlPullParserException("Xml doesn't start with subtypes");
3252 }
3253 final int depth =parser.getDepth();
3254 String currentImiId = null;
3255 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3256 while (((type = parser.next()) != XmlPullParser.END_TAG
3257 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3258 if (type != XmlPullParser.START_TAG)
3259 continue;
3260 final String nodeName = parser.getName();
3261 if (NODE_IMI.equals(nodeName)) {
3262 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3263 if (TextUtils.isEmpty(currentImiId)) {
3264 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3265 continue;
3266 }
3267 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3268 allSubtypes.put(currentImiId, tempSubtypesArray);
3269 } else if (NODE_SUBTYPE.equals(nodeName)) {
3270 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3271 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3272 continue;
3273 }
3274 final int icon = Integer.valueOf(
3275 parser.getAttributeValue(null, ATTR_ICON));
3276 final int label = Integer.valueOf(
3277 parser.getAttributeValue(null, ATTR_LABEL));
3278 final String imeSubtypeLocale =
3279 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3280 final String imeSubtypeMode =
3281 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3282 final String imeSubtypeExtraValue =
3283 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003284 final boolean isAuxiliary = "1".equals(String.valueOf(
3285 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003286 final InputMethodSubtype subtype =
3287 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3288 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3289 tempSubtypesArray.add(subtype);
3290 }
3291 }
3292 } catch (XmlPullParserException e) {
3293 Slog.w(TAG, "Error reading subtypes: " + e);
3294 return;
3295 } catch (java.io.IOException e) {
3296 Slog.w(TAG, "Error reading subtypes: " + e);
3297 return;
3298 } catch (NumberFormatException e) {
3299 Slog.w(TAG, "Error reading subtypes: " + e);
3300 return;
3301 } finally {
3302 if (fis != null) {
3303 try {
3304 fis.close();
3305 } catch (java.io.IOException e1) {
3306 Slog.w(TAG, "Failed to close.");
3307 }
3308 }
3309 }
3310 }
3311 }
3312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 @Override
3316 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3317 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3318 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3321 + Binder.getCallingPid()
3322 + ", uid=" + Binder.getCallingUid());
3323 return;
3324 }
3325
3326 IInputMethod method;
3327 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 synchronized (mMethodMap) {
3332 p.println("Current Input Method Manager state:");
3333 int N = mMethodList.size();
3334 p.println(" Input Methods:");
3335 for (int i=0; i<N; i++) {
3336 InputMethodInfo info = mMethodList.get(i);
3337 p.println(" InputMethod #" + i + ":");
3338 info.dump(p, " ");
3339 }
3340 p.println(" Clients:");
3341 for (ClientState ci : mClients.values()) {
3342 p.println(" Client " + ci + ":");
3343 p.println(" client=" + ci.client);
3344 p.println(" inputContext=" + ci.inputContext);
3345 p.println(" sessionRequested=" + ci.sessionRequested);
3346 p.println(" curSession=" + ci.curSession);
3347 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003348 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003350 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3351 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3353 + " mBoundToMethod=" + mBoundToMethod);
3354 p.println(" mCurToken=" + mCurToken);
3355 p.println(" mCurIntent=" + mCurIntent);
3356 method = mCurMethod;
3357 p.println(" mCurMethod=" + mCurMethod);
3358 p.println(" mEnabledSession=" + mEnabledSession);
3359 p.println(" mShowRequested=" + mShowRequested
3360 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3361 + " mShowForced=" + mShowForced
3362 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003363 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003365
Jeff Brownb88102f2010-09-08 11:49:43 -07003366 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 pw.flush();
3369 try {
3370 client.client.asBinder().dump(fd, args);
3371 } catch (RemoteException e) {
3372 p.println("Input method client dead: " + e);
3373 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003374 } else {
3375 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003377
Jeff Brownb88102f2010-09-08 11:49:43 -07003378 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 pw.flush();
3381 try {
3382 method.asBinder().dump(fd, args);
3383 } catch (RemoteException e) {
3384 p.println("Input method service dead: " + e);
3385 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003386 } else {
3387 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
3389 }
3390}