blob: 03d6b418f97d16661c6fa323f9636852843914bc [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;
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
378 hideInputMethodMenu();
379 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 // Inform the current client of the change in active status
385 try {
386 if (mCurClient != null && mCurClient.client != null) {
387 mCurClient.client.setActive(mScreenOn);
388 }
389 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800390 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + mCurClient.pid + " uid " + mCurClient.uid);
392 }
393 }
394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 class MyPackageMonitor extends PackageMonitor {
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
403 final int N = mMethodList.size();
404 if (curInputMethodId != null) {
405 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 InputMethodInfo imi = mMethodList.get(i);
407 if (imi.getId().equals(curInputMethodId)) {
408 for (String pkg : packages) {
409 if (imi.getPackageName().equals(pkg)) {
410 if (!doit) {
411 return true;
412 }
satok723a27e2010-11-11 14:58:11 +0900413 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 chooseNewDefaultIMELocked();
415 return true;
416 }
417 }
418 }
419 }
420 }
421 }
422 return false;
423 }
424
425 @Override
426 public void onSomePackagesChanged() {
427 synchronized (mMethodMap) {
428 InputMethodInfo curIm = null;
429 String curInputMethodId = Settings.Secure.getString(mContext
430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
434 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900435 final String imiId = imi.getId();
436 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 curIm = imi;
438 }
satoke7c6998e2011-06-03 17:57:59 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900441 if (isPackageModified(imi.getPackageName())) {
442 mFileManager.deleteAllInputMethodSubtypes(imiId);
443 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800446 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 + imi.getComponent());
448 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 buildInputMethodListLocked(mMethodList, mMethodMap);
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 int change = isPackageDisappearing(curIm.getPackageName());
459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 ServiceInfo si = null;
462 try {
463 si = mContext.getPackageManager().getServiceInfo(
464 curIm.getComponent(), 0);
465 } catch (PackageManager.NameNotFoundException ex) {
466 }
467 if (si == null) {
468 // Uh oh, current input method is no longer around!
469 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700472 updateImeWindowStatusLocked();
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
Dianne Hackborn661cd522011-08-22 00:26:20 -0700628 void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900629 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700630 }
631
satoke7c6998e2011-06-03 17:57:59 +0900632 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 public List<InputMethodInfo> getInputMethodList() {
634 synchronized (mMethodMap) {
635 return new ArrayList<InputMethodInfo>(mMethodList);
636 }
637 }
638
satoke7c6998e2011-06-03 17:57:59 +0900639 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public List<InputMethodInfo> getEnabledInputMethodList() {
641 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900642 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644 }
645
satokbb4aa062011-01-19 21:40:27 +0900646 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
647 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
648 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
649 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
650 for (InputMethodInfo imi: getEnabledInputMethodList()) {
651 enabledInputMethodAndSubtypes.put(
652 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
653 }
654 return enabledInputMethodAndSubtypes;
655 }
656
657 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
658 boolean allowsImplicitlySelectedSubtypes) {
659 if (imi == null && mCurMethodId != null) {
660 imi = mMethodMap.get(mCurMethodId);
661 }
satok7265d9b2011-02-14 15:47:30 +0900662 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900663 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900664 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900665 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900666 }
satok7265d9b2011-02-14 15:47:30 +0900667 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900668 }
669
satoke7c6998e2011-06-03 17:57:59 +0900670 @Override
satok16331c82010-12-20 23:48:46 +0900671 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
672 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900673 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900674 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900675 }
676 }
677
satoke7c6998e2011-06-03 17:57:59 +0900678 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 public void addClient(IInputMethodClient client,
680 IInputContext inputContext, int uid, int pid) {
681 synchronized (mMethodMap) {
682 mClients.put(client.asBinder(), new ClientState(client,
683 inputContext, uid, pid));
684 }
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
satoke7c6998e2011-06-03 17:57:59 +0900687 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 public void removeClient(IInputMethodClient client) {
689 synchronized (mMethodMap) {
690 mClients.remove(client.asBinder());
691 }
692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 void executeOrSendMessage(IInterface target, Message msg) {
695 if (target.asBinder() instanceof Binder) {
696 mCaller.sendMessage(msg);
697 } else {
698 handleMessage(msg);
699 msg.recycle();
700 }
701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800702
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700703 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800705 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 + mCurClient.client.asBinder());
707 if (mBoundToMethod) {
708 mBoundToMethod = false;
709 if (mCurMethod != null) {
710 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
711 MSG_UNBIND_INPUT, mCurMethod));
712 }
713 }
714 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
715 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
716 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 // Call setActive(false) on the old client
719 try {
720 mCurClient.client.setActive(false);
721 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800722 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 + mCurClient.pid + " uid " + mCurClient.uid);
724 }
725 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
The Android Open Source Project10592532009-03-18 17:39:46 -0700727 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 private int getImeShowFlags() {
732 int flags = 0;
733 if (mShowForced) {
734 flags |= InputMethod.SHOW_FORCED
735 | InputMethod.SHOW_EXPLICIT;
736 } else if (mShowExplicitlyRequested) {
737 flags |= InputMethod.SHOW_EXPLICIT;
738 }
739 return flags;
740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 private int getAppShowFlags() {
743 int flags = 0;
744 if (mShowForced) {
745 flags |= InputMethodManager.SHOW_FORCED;
746 } else if (!mShowExplicitlyRequested) {
747 flags |= InputMethodManager.SHOW_IMPLICIT;
748 }
749 return flags;
750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
753 if (!mBoundToMethod) {
754 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
755 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
756 mBoundToMethod = true;
757 }
758 final SessionState session = mCurClient.curSession;
759 if (initial) {
760 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
761 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
762 } else {
763 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
764 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
765 }
766 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800767 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800768 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 return needResult
771 ? new InputBindResult(session.session, mCurId, mCurSeq)
772 : null;
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 InputBindResult startInputLocked(IInputMethodClient client,
776 IInputContext inputContext, EditorInfo attribute,
777 boolean initial, boolean needResult) {
778 // If no method is currently selected, do nothing.
779 if (mCurMethodId == null) {
780 return mNoBinding;
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 ClientState cs = mClients.get(client.asBinder());
784 if (cs == null) {
785 throw new IllegalArgumentException("unknown client "
786 + client.asBinder());
787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 try {
790 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
791 // Check with the window manager to make sure this client actually
792 // has a window with focus. If not, reject. This is thread safe
793 // because if the focus changes some time before or after, the
794 // next client receiving focus that has any interest in input will
795 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800796 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
798 return null;
799 }
800 } catch (RemoteException e) {
801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if (mCurClient != cs) {
804 // If the client is changing, we need to switch over to the new
805 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700806 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800807 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 + cs.client.asBinder());
809
810 // If the screen is on, inform the new client it is active
811 if (mScreenOn) {
812 try {
813 cs.client.setActive(mScreenOn);
814 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800815 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 + cs.pid + " uid " + cs.uid);
817 }
818 }
819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 // Bump up the sequence for this client and attach it.
822 mCurSeq++;
823 if (mCurSeq <= 0) mCurSeq = 1;
824 mCurClient = cs;
825 mCurInputContext = inputContext;
826 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 // Check if the input method is changing.
829 if (mCurId != null && mCurId.equals(mCurMethodId)) {
830 if (cs.curSession != null) {
831 // Fast case: if we are already connected to the input method,
832 // then just return it.
833 return attachNewInputLocked(initial, needResult);
834 }
835 if (mHaveConnection) {
836 if (mCurMethod != null) {
837 if (!cs.sessionRequested) {
838 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
841 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900842 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844 // Return to client, and we will get back with it when
845 // we have had a session made for it.
846 return new InputBindResult(null, mCurId, mCurSeq);
847 } else if (SystemClock.uptimeMillis()
848 < (mLastBindTime+TIME_TO_RECONNECT)) {
849 // In this case we have connected to the service, but
850 // don't yet have its interface. If it hasn't been too
851 // long since we did the connection, we'll return to
852 // the client and wait to get the service interface so
853 // we can report back. If it has been too long, we want
854 // to fall through so we can try a disconnect/reconnect
855 // to see if we can get back in touch with the service.
856 return new InputBindResult(null, mCurId, mCurSeq);
857 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
859 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
861 }
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 return startInputInnerLocked();
865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800866
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700867 InputBindResult startInputInnerLocked() {
868 if (mCurMethodId == null) {
869 return mNoBinding;
870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700872 if (!mSystemReady) {
873 // If the system is not yet ready, we shouldn't be running third
874 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700875 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700876 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 InputMethodInfo info = mMethodMap.get(mCurMethodId);
879 if (info == null) {
880 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
881 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700883 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
886 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700887 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
888 com.android.internal.R.string.input_method_binding_label);
889 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
890 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
892 mLastBindTime = SystemClock.uptimeMillis();
893 mHaveConnection = true;
894 mCurId = info.getId();
895 mCurToken = new Binder();
896 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800897 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 mIWindowManager.addWindowToken(mCurToken,
899 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
900 } catch (RemoteException e) {
901 }
902 return new InputBindResult(null, mCurId, mCurSeq);
903 } else {
904 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800905 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 + mCurIntent);
907 }
908 return null;
909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
satoke7c6998e2011-06-03 17:57:59 +0900911 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 public InputBindResult startInput(IInputMethodClient client,
913 IInputContext inputContext, EditorInfo attribute,
914 boolean initial, boolean needResult) {
915 synchronized (mMethodMap) {
916 final long ident = Binder.clearCallingIdentity();
917 try {
918 return startInputLocked(client, inputContext, attribute,
919 initial, needResult);
920 } finally {
921 Binder.restoreCallingIdentity(ident);
922 }
923 }
924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800925
satoke7c6998e2011-06-03 17:57:59 +0900926 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 public void finishInput(IInputMethodClient client) {
928 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
satoke7c6998e2011-06-03 17:57:59 +0900930 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 public void onServiceConnected(ComponentName name, IBinder service) {
932 synchronized (mMethodMap) {
933 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
934 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800936 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700937 unbindCurrentMethodLocked(false);
938 return;
939 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800940 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700941 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
942 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800944 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700945 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700947 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900948 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
950 }
951 }
952 }
953
954 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
955 synchronized (mMethodMap) {
956 if (mCurMethod != null && method != null
957 && mCurMethod.asBinder() == method.asBinder()) {
958 if (mCurClient != null) {
959 mCurClient.curSession = new SessionState(mCurClient,
960 method, session);
961 mCurClient.sessionRequested = false;
962 InputBindResult res = attachNewInputLocked(true, true);
963 if (res.method != null) {
964 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
965 MSG_BIND_METHOD, mCurClient.client, res));
966 }
967 }
968 }
969 }
970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800971
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700972 void unbindCurrentMethodLocked(boolean reportToClient) {
973 if (mHaveConnection) {
974 mContext.unbindService(this);
975 mHaveConnection = false;
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700978 if (mCurToken != null) {
979 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800980 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700981 mIWindowManager.removeWindowToken(mCurToken);
982 } catch (RemoteException e) {
983 }
984 mCurToken = null;
985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
The Android Open Source Project10592532009-03-18 17:39:46 -0700987 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700988 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700990 if (reportToClient && mCurClient != null) {
991 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
992 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
993 }
994 }
995
Devin Taylor0c33ed22010-02-23 13:26:46 -0600996 private void finishSession(SessionState sessionState) {
997 if (sessionState != null && sessionState.session != null) {
998 try {
999 sessionState.session.finishSession();
1000 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001001 Slog.w(TAG, "Session failed to close due to remote exception", e);
satokdbf29502011-08-25 15:28:23 +09001002 mImeWindowVis = 0;
1003 updateImeWindowStatusLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001004 }
1005 }
1006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001008 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 if (mCurMethod != null) {
1010 for (ClientState cs : mClients.values()) {
1011 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001012 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 cs.curSession = null;
1014 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001015
1016 finishSession(mEnabledSession);
1017 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 mCurMethod = null;
1019 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001020 if (mStatusBar != null) {
1021 mStatusBar.setIconVisibility("ime", false);
1022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
satoke7c6998e2011-06-03 17:57:59 +09001025 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 public void onServiceDisconnected(ComponentName name) {
1027 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001028 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 + " mCurIntent=" + mCurIntent);
1030 if (mCurMethod != null && mCurIntent != null
1031 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001032 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 // We consider this to be a new bind attempt, since the system
1034 // should now try to restart the service for us.
1035 mLastBindTime = SystemClock.uptimeMillis();
1036 mShowRequested = mInputShown;
1037 mInputShown = false;
1038 if (mCurClient != null) {
1039 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1040 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1041 }
1042 }
1043 }
1044 }
1045
satokf9f01002011-05-19 21:31:50 +09001046 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001048 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 long ident = Binder.clearCallingIdentity();
1050 try {
1051 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001052 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 return;
1054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 synchronized (mMethodMap) {
1057 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001059 if (mStatusBar != null) {
1060 mStatusBar.setIconVisibility("ime", false);
1061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001063 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001064 CharSequence contentDescription = null;
1065 try {
1066 PackageManager packageManager = mContext.getPackageManager();
1067 contentDescription = packageManager.getApplicationLabel(
1068 packageManager.getApplicationInfo(packageName, 0));
1069 } catch (NameNotFoundException nnfe) {
1070 /* ignore */
1071 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001072 if (mStatusBar != null) {
1073 mStatusBar.setIcon("ime", packageName, iconId, 0,
1074 contentDescription != null
1075 ? contentDescription.toString() : null);
1076 mStatusBar.setIconVisibility("ime", true);
1077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 }
1079 }
1080 } finally {
1081 Binder.restoreCallingIdentity(ident);
1082 }
1083 }
1084
satok7cfc0ed2011-06-20 21:29:36 +09001085 private boolean needsToShowImeSwitchOngoingNotification() {
1086 if (!mShowOngoingImeSwitcherForPhones) return false;
1087 synchronized (mMethodMap) {
1088 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1089 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001090 if (N > 2) return true;
1091 if (N < 1) return false;
1092 int nonAuxCount = 0;
1093 int auxCount = 0;
1094 InputMethodSubtype nonAuxSubtype = null;
1095 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001096 for(int i = 0; i < N; ++i) {
1097 final InputMethodInfo imi = imis.get(i);
1098 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1099 imi, true);
1100 final int subtypeCount = subtypes.size();
1101 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001102 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001103 } else {
1104 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001105 final InputMethodSubtype subtype = subtypes.get(j);
1106 if (!subtype.isAuxiliary()) {
1107 ++nonAuxCount;
1108 nonAuxSubtype = subtype;
1109 } else {
1110 ++auxCount;
1111 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001112 }
1113 }
1114 }
satok7cfc0ed2011-06-20 21:29:36 +09001115 }
satokb6359412011-06-28 17:47:41 +09001116 if (nonAuxCount > 1 || auxCount > 1) {
1117 return true;
1118 } else if (nonAuxCount == 1 && auxCount == 1) {
1119 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001120 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1121 || auxSubtype.overridesImplicitlyEnabledSubtype()
1122 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001123 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1124 return false;
1125 }
1126 return true;
1127 }
1128 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001129 }
satok7cfc0ed2011-06-20 21:29:36 +09001130 }
1131
satokdbf29502011-08-25 15:28:23 +09001132 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001133 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001134 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001135 int uid = Binder.getCallingUid();
1136 long ident = Binder.clearCallingIdentity();
1137 try {
1138 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001139 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001140 return;
1141 }
1142
1143 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001144 mImeWindowVis = vis;
1145 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001146 if (mStatusBar != null) {
1147 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1148 }
satok7cfc0ed2011-06-20 21:29:36 +09001149 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001150 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1151 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001152 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001153 final CharSequence title = mRes.getText(
1154 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001155 final CharSequence imiLabel = imi.loadLabel(pm);
1156 final CharSequence summary = mCurrentSubtype != null
1157 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1158 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1159 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001160 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001161 : imiLabel;
1162
satok7cfc0ed2011-06-20 21:29:36 +09001163 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001164 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001165 if (mNotificationManager != null) {
1166 mNotificationManager.notify(
1167 com.android.internal.R.string.select_input_method,
1168 mImeSwitcherNotification);
1169 mNotificationShown = true;
1170 }
satok7cfc0ed2011-06-20 21:29:36 +09001171 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001172 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001173 mNotificationManager.cancel(
1174 com.android.internal.R.string.select_input_method);
1175 mNotificationShown = false;
1176 }
1177 }
satok06487a52010-10-29 11:37:18 +09001178 }
1179 } finally {
1180 Binder.restoreCallingIdentity(ident);
1181 }
1182 }
1183
satoke7c6998e2011-06-03 17:57:59 +09001184 @Override
satokf9f01002011-05-19 21:31:50 +09001185 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1186 synchronized (mMethodMap) {
1187 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1188 for (int i = 0; i < spans.length; ++i) {
1189 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001190 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001191 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001192 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001193 }
1194 }
1195 }
1196 }
1197
satoke7c6998e2011-06-03 17:57:59 +09001198 @Override
satokf9f01002011-05-19 21:31:50 +09001199 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1200 synchronized (mMethodMap) {
1201 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1202 // TODO: Do not send the intent if the process of the targetImi is already dead.
1203 if (targetImi != null) {
1204 final String[] suggestions = span.getSuggestions();
1205 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001206 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001207 final Intent intent = new Intent();
1208 // Ensures that only a class in the original IME package will receive the
1209 // notification.
satok42c5a162011-05-26 16:46:14 +09001210 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001211 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1212 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1213 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1214 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1215 mContext.sendBroadcast(intent);
1216 return true;
1217 }
1218 }
1219 return false;
1220 }
1221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001223 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1224 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1225 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1226 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001228 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001229 // There is no input method selected, try to choose new applicable input method.
1230 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1231 id = Settings.Secure.getString(mContext.getContentResolver(),
1232 Settings.Secure.DEFAULT_INPUT_METHOD);
1233 }
1234 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 try {
satokab751aa2010-09-14 19:17:36 +09001236 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001238 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001239 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001240 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
satokf3db1af2010-11-23 13:34:33 +09001242 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001243 } else {
1244 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001245 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001246 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
satokab751aa2010-09-14 19:17:36 +09001250 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 InputMethodInfo info = mMethodMap.get(id);
1252 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001253 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001257 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001258 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1259 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001260 }
1261 if (subtype != mCurrentSubtype) {
1262 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001263 if (subtype != null) {
1264 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1265 }
satokcd7cd292010-11-20 15:46:23 +09001266 if (mCurMethod != null) {
1267 try {
satoke40dea02011-01-30 01:14:02 +09001268 final Configuration conf = mRes.getConfiguration();
1269 final boolean haveHardKeyboard = conf.keyboard
1270 != Configuration.KEYBOARD_NOKEYS;
1271 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001272 && conf.hardKeyboardHidden
1273 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001274 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1275 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1276 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001277 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001278 // If subtype is null, try to find the most applicable one from
1279 // getCurrentInputMethodSubtype.
1280 if (subtype == null) {
1281 subtype = getCurrentInputMethodSubtype();
1282 }
1283 mCurMethod.changeInputMethodSubtype(subtype);
1284 } catch (RemoteException e) {
1285 return;
satokab751aa2010-09-14 19:17:36 +09001286 }
1287 }
1288 }
1289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 return;
1291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 final long ident = Binder.clearCallingIdentity();
1294 try {
satokab751aa2010-09-14 19:17:36 +09001295 // Set a subtype to this input method.
1296 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001297 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1298 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1299 // because mCurMethodId is stored as a history in
1300 // setSelectedInputMethodAndSubtypeLocked().
1301 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302
1303 if (ActivityManagerNative.isSystemReady()) {
1304 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001305 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 intent.putExtra("input_method_id", id);
1307 mContext.sendBroadcast(intent);
1308 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001309 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 } finally {
1311 Binder.restoreCallingIdentity(ident);
1312 }
1313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
satok42c5a162011-05-26 16:46:14 +09001315 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001316 public boolean showSoftInput(IInputMethodClient client, int flags,
1317 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001318 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 long ident = Binder.clearCallingIdentity();
1320 try {
1321 synchronized (mMethodMap) {
1322 if (mCurClient == null || client == null
1323 || mCurClient.client.asBinder() != client.asBinder()) {
1324 try {
1325 // We need to check if this is the current client with
1326 // focus in the window manager, to allow this call to
1327 // be made before input is started in it.
1328 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001329 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 }
1332 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001333 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
Joe Onorato8a9b2202010-02-26 18:56:32 -08001337 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001338 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340 } finally {
1341 Binder.restoreCallingIdentity(ident);
1342 }
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
The Android Open Source Project4df24232009-03-05 14:34:35 -08001345 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 mShowRequested = true;
1347 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1348 mShowExplicitlyRequested = true;
1349 }
1350 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1351 mShowExplicitlyRequested = true;
1352 mShowForced = true;
1353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001354
Dianne Hackborncc278702009-09-02 23:07:23 -07001355 if (!mSystemReady) {
1356 return false;
1357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001361 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1362 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1363 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001365 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001367 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 // The client has asked to have the input method shown, but
1369 // we have been sitting here too long with a connection to the
1370 // service and no interface received, so let's disconnect/connect
1371 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001374 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 mContext.unbindService(this);
1376 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001378
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001381
satok42c5a162011-05-26 16:46:14 +09001382 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001383 public boolean hideSoftInput(IInputMethodClient client, int flags,
1384 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001385 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 long ident = Binder.clearCallingIdentity();
1387 try {
1388 synchronized (mMethodMap) {
1389 if (mCurClient == null || client == null
1390 || mCurClient.client.asBinder() != client.asBinder()) {
1391 try {
1392 // We need to check if this is the current client with
1393 // focus in the window manager, to allow this call to
1394 // be made before input is started in it.
1395 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001396 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1397 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001398 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001399 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001403 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001404 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001405 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001408
Joe Onorato8a9b2202010-02-26 18:56:32 -08001409 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001410 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412 } finally {
1413 Binder.restoreCallingIdentity(ident);
1414 }
1415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
The Android Open Source Project4df24232009-03-05 14:34:35 -08001417 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1419 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001420 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001422 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001425 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001427 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001429 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1432 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1433 res = true;
1434 } else {
1435 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437 mInputShown = false;
1438 mShowRequested = false;
1439 mShowExplicitlyRequested = false;
1440 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001441 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001443
satok42c5a162011-05-26 16:46:14 +09001444 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001445 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1447 boolean first, int windowFlags) {
1448 long ident = Binder.clearCallingIdentity();
1449 try {
1450 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001451 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 + " viewHasFocus=" + viewHasFocus
1453 + " isTextEditor=" + isTextEditor
1454 + " softInputMode=#" + Integer.toHexString(softInputMode)
1455 + " first=" + first + " flags=#"
1456 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 if (mCurClient == null || client == null
1459 || mCurClient.client.asBinder() != client.asBinder()) {
1460 try {
1461 // We need to check if this is the current client with
1462 // focus in the window manager, to allow this call to
1463 // be made before input is started in it.
1464 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001465 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 return;
1467 }
1468 } catch (RemoteException e) {
1469 }
1470 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001471
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001472 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001473 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001474 return;
1475 }
1476 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001477
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001478 // Should we auto-show the IME even if the caller has not
1479 // specified what should be done with it?
1480 // We only do this automatically if the window can resize
1481 // to accommodate the IME (so what the user sees will give
1482 // them good context without input information being obscured
1483 // by the IME) or if running on a large screen where there
1484 // is more room for the target window + IME.
1485 final boolean doAutoShow =
1486 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1487 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1488 || mRes.getConfiguration().isLayoutSizeAtLeast(
1489 Configuration.SCREENLAYOUT_SIZE_LARGE);
1490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1492 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001493 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1495 // There is no focus view, and this window will
1496 // be behind any soft input window, so hide the
1497 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001498 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001499 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001501 } else if (isTextEditor && doAutoShow && (softInputMode &
1502 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 // There is a focus view, and we are navigating forward
1504 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001505 // We only do this automatically if the window an resize
1506 // to accomodate the IME (so what the user sees will give
1507 // them good context without input information being obscured
1508 // by the IME) or if running on a large screen where there
1509 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001511 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513 break;
1514 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1515 // Do nothing.
1516 break;
1517 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1518 if ((softInputMode &
1519 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001520 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001521 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 break;
1524 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001525 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001526 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 break;
1528 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1529 if ((softInputMode &
1530 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001531 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001532 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 break;
1535 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001536 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001537 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 break;
1539 }
1540 }
1541 } finally {
1542 Binder.restoreCallingIdentity(ident);
1543 }
1544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
satok42c5a162011-05-26 16:46:14 +09001546 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1548 synchronized (mMethodMap) {
1549 if (mCurClient == null || client == null
1550 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001551 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001552 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554
satok440aab52010-11-25 09:43:11 +09001555 // Always call subtype picker, because subtype picker is a superset of input method
1556 // picker.
satokab751aa2010-09-14 19:17:36 +09001557 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1558 }
1559 }
1560
satok42c5a162011-05-26 16:46:14 +09001561 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001563 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1564 }
1565
satok42c5a162011-05-26 16:46:14 +09001566 @Override
satok28203512010-11-24 11:06:49 +09001567 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1568 synchronized (mMethodMap) {
1569 if (subtype != null) {
1570 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1571 mMethodMap.get(id), subtype.hashCode()));
1572 } else {
1573 setInputMethod(token, id);
1574 }
1575 }
satokab751aa2010-09-14 19:17:36 +09001576 }
1577
satok42c5a162011-05-26 16:46:14 +09001578 @Override
satokb416a712010-11-25 20:42:14 +09001579 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001580 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001581 synchronized (mMethodMap) {
1582 if (mCurClient == null || client == null
1583 || mCurClient.client.asBinder() != client.asBinder()) {
1584 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1585 }
satok7fee71f2010-12-17 18:54:26 +09001586 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1587 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001588 }
1589 }
1590
satok4fc87d62011-05-20 16:13:43 +09001591 @Override
satok735cf382010-11-11 20:40:09 +09001592 public boolean switchToLastInputMethod(IBinder token) {
1593 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001594 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001595 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001596 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001597 lastImi = mMethodMap.get(lastIme.first);
1598 } else {
1599 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001600 }
satok4fc87d62011-05-20 16:13:43 +09001601 String targetLastImiId = null;
1602 int subtypeId = NOT_A_SUBTYPE_ID;
1603 if (lastIme != null && lastImi != null) {
1604 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1605 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1606 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1607 : mCurrentSubtype.hashCode();
1608 // If the last IME is the same as the current IME and the last subtype is not
1609 // defined, there is no need to switch to the last IME.
1610 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1611 targetLastImiId = lastIme.first;
1612 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1613 }
1614 }
1615
1616 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1617 // This is a safety net. If the currentSubtype can't be added to the history
1618 // and the framework couldn't find the last ime, we will make the last ime be
1619 // the most applicable enabled keyboard subtype of the system imes.
1620 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1621 if (enabled != null) {
1622 final int N = enabled.size();
1623 final String locale = mCurrentSubtype == null
1624 ? mRes.getConfiguration().locale.toString()
1625 : mCurrentSubtype.getLocale();
1626 for (int i = 0; i < N; ++i) {
1627 final InputMethodInfo imi = enabled.get(i);
1628 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1629 InputMethodSubtype keyboardSubtype =
1630 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1631 SUBTYPE_MODE_KEYBOARD, locale, true);
1632 if (keyboardSubtype != null) {
1633 targetLastImiId = imi.getId();
1634 subtypeId = getSubtypeIdFromHashCode(
1635 imi, keyboardSubtype.hashCode());
1636 if(keyboardSubtype.getLocale().equals(locale)) {
1637 break;
1638 }
1639 }
1640 }
1641 }
1642 }
1643 }
1644
1645 if (!TextUtils.isEmpty(targetLastImiId)) {
1646 if (DEBUG) {
1647 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1648 + ", from: " + mCurMethodId + ", " + subtypeId);
1649 }
1650 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1651 return true;
1652 } else {
1653 return false;
1654 }
satok735cf382010-11-11 20:40:09 +09001655 }
1656 }
1657
satoke7c6998e2011-06-03 17:57:59 +09001658 @Override
satok68f1b782011-04-11 14:26:04 +09001659 public InputMethodSubtype getLastInputMethodSubtype() {
1660 synchronized (mMethodMap) {
1661 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1662 // TODO: Handle the case of the last IME with no subtypes
1663 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1664 || TextUtils.isEmpty(lastIme.second)) return null;
1665 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1666 if (lastImi == null) return null;
1667 try {
1668 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001669 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1670 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1671 return null;
1672 }
1673 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001674 } catch (NumberFormatException e) {
1675 return null;
1676 }
1677 }
1678 }
1679
satoke7c6998e2011-06-03 17:57:59 +09001680 @Override
satokee5e77c2011-09-02 18:50:15 +09001681 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001682 // By this IPC call, only a process which shares the same uid with the IME can add
1683 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001684 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001685 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001686 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001687 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001688 final PackageManager pm = mContext.getPackageManager();
1689 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1690 if (packageInfos != null) {
1691 final int packageNum = packageInfos.length;
1692 for (int i = 0; i < packageNum; ++i) {
1693 if (packageInfos[i].equals(imi.getPackageName())) {
1694 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001695 final long ident = Binder.clearCallingIdentity();
1696 try {
1697 buildInputMethodListLocked(mMethodList, mMethodMap);
1698 } finally {
1699 Binder.restoreCallingIdentity(ident);
1700 }
satokee5e77c2011-09-02 18:50:15 +09001701 return;
satok91e88122011-07-18 11:11:42 +09001702 }
1703 }
1704 }
satoke7c6998e2011-06-03 17:57:59 +09001705 }
satokee5e77c2011-09-02 18:50:15 +09001706 return;
satoke7c6998e2011-06-03 17:57:59 +09001707 }
1708
satok28203512010-11-24 11:06:49 +09001709 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 synchronized (mMethodMap) {
1711 if (token == null) {
1712 if (mContext.checkCallingOrSelfPermission(
1713 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1714 != PackageManager.PERMISSION_GRANTED) {
1715 throw new SecurityException(
1716 "Using null token requires permission "
1717 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1718 }
1719 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001720 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1721 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 return;
1723 }
1724
satokc5933802011-08-31 21:26:04 +09001725 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 try {
satokab751aa2010-09-14 19:17:36 +09001727 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 } finally {
1729 Binder.restoreCallingIdentity(ident);
1730 }
1731 }
1732 }
1733
satok42c5a162011-05-26 16:46:14 +09001734 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 public void hideMySoftInput(IBinder token, int flags) {
1736 synchronized (mMethodMap) {
1737 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001738 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1739 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 return;
1741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 long ident = Binder.clearCallingIdentity();
1743 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001744 hideCurrentInputLocked(flags, null);
1745 } finally {
1746 Binder.restoreCallingIdentity(ident);
1747 }
1748 }
1749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
satok42c5a162011-05-26 16:46:14 +09001751 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001752 public void showMySoftInput(IBinder token, int flags) {
1753 synchronized (mMethodMap) {
1754 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001755 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1756 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 return;
1758 }
1759 long ident = Binder.clearCallingIdentity();
1760 try {
1761 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 } finally {
1763 Binder.restoreCallingIdentity(ident);
1764 }
1765 }
1766 }
1767
1768 void setEnabledSessionInMainThread(SessionState session) {
1769 if (mEnabledSession != session) {
1770 if (mEnabledSession != null) {
1771 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001772 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 mEnabledSession.method.setSessionEnabled(
1774 mEnabledSession.session, false);
1775 } catch (RemoteException e) {
1776 }
1777 }
1778 mEnabledSession = session;
1779 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001780 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 session.method.setSessionEnabled(
1782 session.session, true);
1783 } catch (RemoteException e) {
1784 }
1785 }
1786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
satok42c5a162011-05-26 16:46:14 +09001788 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 public boolean handleMessage(Message msg) {
1790 HandlerCaller.SomeArgs args;
1791 switch (msg.what) {
1792 case MSG_SHOW_IM_PICKER:
1793 showInputMethodMenu();
1794 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001795
satokab751aa2010-09-14 19:17:36 +09001796 case MSG_SHOW_IM_SUBTYPE_PICKER:
1797 showInputMethodSubtypeMenu();
1798 return true;
1799
satok47a44912010-10-06 16:03:58 +09001800 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001801 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001802 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001803 return true;
1804
1805 case MSG_SHOW_IM_CONFIG:
1806 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001807 return true;
1808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 case MSG_UNBIND_INPUT:
1812 try {
1813 ((IInputMethod)msg.obj).unbindInput();
1814 } catch (RemoteException e) {
1815 // There is nothing interesting about the method dying.
1816 }
1817 return true;
1818 case MSG_BIND_INPUT:
1819 args = (HandlerCaller.SomeArgs)msg.obj;
1820 try {
1821 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1822 } catch (RemoteException e) {
1823 }
1824 return true;
1825 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001826 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001828 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1829 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 } catch (RemoteException e) {
1831 }
1832 return true;
1833 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001834 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 ((IInputMethod)args.arg1).hideSoftInput(0,
1837 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 } catch (RemoteException e) {
1839 }
1840 return true;
1841 case MSG_ATTACH_TOKEN:
1842 args = (HandlerCaller.SomeArgs)msg.obj;
1843 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001844 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1846 } catch (RemoteException e) {
1847 }
1848 return true;
1849 case MSG_CREATE_SESSION:
1850 args = (HandlerCaller.SomeArgs)msg.obj;
1851 try {
1852 ((IInputMethod)args.arg1).createSession(
1853 (IInputMethodCallback)args.arg2);
1854 } catch (RemoteException e) {
1855 }
1856 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 case MSG_START_INPUT:
1860 args = (HandlerCaller.SomeArgs)msg.obj;
1861 try {
1862 SessionState session = (SessionState)args.arg1;
1863 setEnabledSessionInMainThread(session);
1864 session.method.startInput((IInputContext)args.arg2,
1865 (EditorInfo)args.arg3);
1866 } catch (RemoteException e) {
1867 }
1868 return true;
1869 case MSG_RESTART_INPUT:
1870 args = (HandlerCaller.SomeArgs)msg.obj;
1871 try {
1872 SessionState session = (SessionState)args.arg1;
1873 setEnabledSessionInMainThread(session);
1874 session.method.restartInput((IInputContext)args.arg2,
1875 (EditorInfo)args.arg3);
1876 } catch (RemoteException e) {
1877 }
1878 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 case MSG_UNBIND_METHOD:
1883 try {
1884 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1885 } catch (RemoteException e) {
1886 // There is nothing interesting about the last client dying.
1887 }
1888 return true;
1889 case MSG_BIND_METHOD:
1890 args = (HandlerCaller.SomeArgs)msg.obj;
1891 try {
1892 ((IInputMethodClient)args.arg1).onBindMethod(
1893 (InputBindResult)args.arg2);
1894 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001895 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897 return true;
1898 }
1899 return false;
1900 }
1901
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001902 private boolean isSystemIme(InputMethodInfo inputMethod) {
1903 return (inputMethod.getServiceInfo().applicationInfo.flags
1904 & ApplicationInfo.FLAG_SYSTEM) != 0;
1905 }
1906
Ken Wakasa586f0512011-01-20 22:31:01 +09001907 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1908 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1909 final int subtypeCount = imi.getSubtypeCount();
1910 for (int i = 0; i < subtypeCount; ++i) {
1911 subtypes.add(imi.getSubtypeAt(i));
1912 }
1913 return subtypes;
1914 }
1915
satoka86f5e42011-09-02 17:12:42 +09001916
1917 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1918 InputMethodInfo imi, String mode) {
1919 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1920 final int subtypeCount = imi.getSubtypeCount();
1921 for (int i = 0; i < subtypeCount; ++i) {
1922 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1923 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1924 subtypes.add(subtype);
1925 }
1926 }
1927 return subtypes;
1928 }
1929
satokdc9ddae2011-10-06 12:22:36 +09001930 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001931 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001932 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001933 // We'd prefer to fall back on a system IME, since that is safer.
1934 int i=enabled.size();
1935 while (i > 0) {
1936 i--;
satokdc9ddae2011-10-06 12:22:36 +09001937 final InputMethodInfo imi = enabled.get(i);
1938 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001939 break;
1940 }
1941 }
satokdc9ddae2011-10-06 12:22:36 +09001942 return enabled.get(i);
1943 }
1944 return null;
1945 }
1946
1947 private boolean chooseNewDefaultIMELocked() {
1948 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1949 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001950 if (DEBUG) {
1951 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1952 }
satok723a27e2010-11-11 14:58:11 +09001953 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001954 return true;
1955 }
1956
1957 return false;
1958 }
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1961 HashMap<String, InputMethodInfo> map) {
1962 list.clear();
1963 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001966 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001967 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1968 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1969 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1970 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971
1972 List<ResolveInfo> services = pm.queryIntentServices(
1973 new Intent(InputMethod.SERVICE_INTERFACE),
1974 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
satoke7c6998e2011-06-03 17:57:59 +09001976 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1977 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 for (int i = 0; i < services.size(); ++i) {
1979 ResolveInfo ri = services.get(i);
1980 ServiceInfo si = ri.serviceInfo;
1981 ComponentName compName = new ComponentName(si.packageName, si.name);
1982 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1983 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001984 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 + ": it does not require the permission "
1986 + android.Manifest.permission.BIND_INPUT_METHOD);
1987 continue;
1988 }
1989
Joe Onorato8a9b2202010-02-26 18:56:32 -08001990 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991
1992 try {
satoke7c6998e2011-06-03 17:57:59 +09001993 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001995 final String id = p.getId();
1996 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997
Amith Yamasanie861ec12010-03-24 21:39:27 -07001998 // System IMEs are enabled by default, unless there's a hard keyboard
1999 // and the system IME was explicitly disabled
2000 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2001 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002002 }
2003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002005 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002009 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 } catch (IOException 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 }
2013 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002014
2015 String defaultIme = Settings.Secure.getString(mContext
2016 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002017 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002018 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002019 updateFromSettingsLocked();
2020 }
2021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002025
satokab751aa2010-09-14 19:17:36 +09002026 private void showInputMethodMenu() {
2027 showInputMethodMenuInternal(false);
2028 }
2029
2030 private void showInputMethodSubtypeMenu() {
2031 showInputMethodMenuInternal(true);
2032 }
2033
satok217f5482010-12-15 05:19:19 +09002034 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002035 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002036 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002037 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2038 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002039 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002040 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002041 }
satok217f5482010-12-15 05:19:19 +09002042 mContext.startActivity(intent);
2043 }
2044
2045 private void showConfigureInputMethods() {
2046 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2047 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2048 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2049 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002050 mContext.startActivity(intent);
2051 }
2052
satokab751aa2010-09-14 19:17:36 +09002053 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002054 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002058 final boolean isScreenLocked = mKeyguardManager != null
2059 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 String lastInputMethodId = Settings.Secure.getString(context
2062 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002063 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002064 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002065
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002066 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002067 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2068 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002069 if (immis == null || immis.size() == 0) {
2070 return;
2071 }
2072
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002073 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074
Ken Wakasa761eb372011-03-04 19:06:18 +09002075 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2076 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2077 new Comparator<InputMethodInfo>() {
2078 @Override
2079 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2080 if (imi2 == null) return 0;
2081 if (imi1 == null) return 1;
2082 if (pm == null) {
2083 return imi1.getId().compareTo(imi2.getId());
2084 }
2085 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2086 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2087 return imiId1.toString().compareTo(imiId2.toString());
2088 }
2089 });
satok913a8922010-08-26 21:53:41 +09002090
Ken Wakasa761eb372011-03-04 19:06:18 +09002091 sortedImmis.putAll(immis);
2092
Ken Wakasa05dbb652011-08-22 15:22:43 +09002093 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002094
2095 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002096 if (imi == null) continue;
2097 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002098 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002099 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2100 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002101 }
satokbb4aa062011-01-19 21:40:27 +09002102 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002103 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002104 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002105 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002106 if (DEBUG) {
2107 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2108 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002109 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002110 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2111 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2112 // We show all enabled IMEs and subtypes when an IME is shown.
2113 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002114 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002115 final CharSequence subtypeLabel =
2116 subtype.overridesImplicitlyEnabledSubtype() ? null
2117 : subtype.getDisplayName(context, imi.getPackageName(),
2118 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002119 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2120
satok4a28bde2011-06-29 21:03:40 +09002121 // Removing this subtype from enabledSubtypeSet because we no longer
2122 // need to add an entry of this subtype to imList to avoid duplicated
2123 // entries.
2124 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002125 }
satokab751aa2010-09-14 19:17:36 +09002126 }
2127 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002128 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002129 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002130 }
satok913a8922010-08-26 21:53:41 +09002131
Ken Wakasa761eb372011-03-04 19:06:18 +09002132 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002133 mIms = new InputMethodInfo[N];
2134 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002135 int checkedItem = 0;
2136 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002137 final ImeSubtypeListItem item = imList.get(i);
2138 mIms[i] = item.mImi;
2139 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002140 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002141 int subtypeId = mSubtypeIds[i];
2142 if ((subtypeId == NOT_A_SUBTYPE_ID)
2143 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2144 || (subtypeId == lastInputMethodSubtypeId)) {
2145 checkedItem = i;
2146 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
satokab751aa2010-09-14 19:17:36 +09002149
Ken Wakasa05dbb652011-08-22 15:22:43 +09002150 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002151 com.android.internal.R.styleable.DialogPreference,
2152 com.android.internal.R.attr.alertDialogStyle, 0);
2153 mDialogBuilder = new AlertDialog.Builder(context)
2154 .setTitle(com.android.internal.R.string.select_input_method)
2155 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002156 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002157 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002159 }
2160 })
2161 .setIcon(a.getDrawable(
2162 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2163 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002164
Ken Wakasa05dbb652011-08-22 15:22:43 +09002165 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2166 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2167 checkedItem);
2168
2169 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002170 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002171 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002172 public void onClick(DialogInterface dialog, int which) {
2173 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002174 if (mIms == null || mIms.length <= which
2175 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002176 return;
2177 }
2178 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002179 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002180 hideInputMethodMenu();
2181 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002182 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002183 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002184 subtypeId = NOT_A_SUBTYPE_ID;
2185 }
2186 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002187 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002190 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191
satokbc81b692011-08-26 16:22:22 +09002192 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002193 mDialogBuilder.setPositiveButton(
2194 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002195 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002196 @Override
satok7f35c8c2010-10-07 21:13:11 +09002197 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002198 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002199 }
2200 });
2201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002203 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 mSwitchingDialog.getWindow().setType(
2205 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002206 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 mSwitchingDialog.show();
2208 }
2209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002210
Ken Wakasa05dbb652011-08-22 15:22:43 +09002211 private static class ImeSubtypeListItem {
2212 public final CharSequence mImeName;
2213 public final CharSequence mSubtypeName;
2214 public final InputMethodInfo mImi;
2215 public final int mSubtypeId;
2216 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2217 InputMethodInfo imi, int subtypeId) {
2218 mImeName = imeName;
2219 mSubtypeName = subtypeName;
2220 mImi = imi;
2221 mSubtypeId = subtypeId;
2222 }
2223 }
2224
2225 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2226 private final LayoutInflater mInflater;
2227 private final int mTextViewResourceId;
2228 private final List<ImeSubtypeListItem> mItemsList;
2229 private final int mCheckedItem;
2230 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2231 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2232 super(context, textViewResourceId, itemsList);
2233 mTextViewResourceId = textViewResourceId;
2234 mItemsList = itemsList;
2235 mCheckedItem = checkedItem;
2236 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2237 }
2238
2239 @Override
2240 public View getView(int position, View convertView, ViewGroup parent) {
2241 final View view = convertView != null ? convertView
2242 : mInflater.inflate(mTextViewResourceId, null);
2243 if (position < 0 || position >= mItemsList.size()) return view;
2244 final ImeSubtypeListItem item = mItemsList.get(position);
2245 final CharSequence imeName = item.mImeName;
2246 final CharSequence subtypeName = item.mSubtypeName;
2247 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2248 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2249 if (TextUtils.isEmpty(subtypeName)) {
2250 firstTextView.setText(imeName);
2251 secondTextView.setVisibility(View.GONE);
2252 } else {
2253 firstTextView.setText(subtypeName);
2254 secondTextView.setText(imeName);
2255 secondTextView.setVisibility(View.VISIBLE);
2256 }
2257 final RadioButton radioButton =
2258 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2259 radioButton.setChecked(position == mCheckedItem);
2260 return view;
2261 }
2262 }
2263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002265 synchronized (mMethodMap) {
2266 hideInputMethodMenuLocked();
2267 }
2268 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002269
The Android Open Source Project10592532009-03-18 17:39:46 -07002270 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002271 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272
The Android Open Source Project10592532009-03-18 17:39:46 -07002273 if (mSwitchingDialog != null) {
2274 mSwitchingDialog.dismiss();
2275 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002277
The Android Open Source Project10592532009-03-18 17:39:46 -07002278 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002279 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002283
satok42c5a162011-05-26 16:46:14 +09002284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public boolean setInputMethodEnabled(String id, boolean enabled) {
2286 synchronized (mMethodMap) {
2287 if (mContext.checkCallingOrSelfPermission(
2288 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2289 != PackageManager.PERMISSION_GRANTED) {
2290 throw new SecurityException(
2291 "Requires permission "
2292 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2293 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 long ident = Binder.clearCallingIdentity();
2296 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002297 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 } finally {
2299 Binder.restoreCallingIdentity(ident);
2300 }
2301 }
2302 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002303
2304 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2305 // Make sure this is a valid input method.
2306 InputMethodInfo imm = mMethodMap.get(id);
2307 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002308 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002309 }
2310
satokd87c2592010-09-29 11:52:06 +09002311 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2312 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002313
satokd87c2592010-09-29 11:52:06 +09002314 if (enabled) {
2315 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2316 if (pair.first.equals(id)) {
2317 // We are enabling this input method, but it is already enabled.
2318 // Nothing to do. The previous state was enabled.
2319 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002320 }
2321 }
satokd87c2592010-09-29 11:52:06 +09002322 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2323 // Previous state was disabled.
2324 return false;
2325 } else {
2326 StringBuilder builder = new StringBuilder();
2327 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2328 builder, enabledInputMethodsList, id)) {
2329 // Disabled input method is currently selected, switch to another one.
2330 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2331 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002332 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2333 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2334 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002335 }
2336 // Previous state was enabled.
2337 return true;
2338 } else {
2339 // We are disabling the input method but it is already disabled.
2340 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002341 return false;
2342 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002343 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002344 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002345
satok57ffc002011-01-25 00:11:47 +09002346 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2347 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002348 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002349 }
2350
satok723a27e2010-11-11 14:58:11 +09002351 private void saveCurrentInputMethodAndSubtypeToHistory() {
2352 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2353 if (mCurrentSubtype != null) {
2354 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2355 }
satok57ffc002011-01-25 00:11:47 +09002356 if (canAddToLastInputMethod(mCurrentSubtype)) {
2357 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2358 }
satokab751aa2010-09-14 19:17:36 +09002359 }
2360
satok723a27e2010-11-11 14:58:11 +09002361 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2362 boolean setSubtypeOnly) {
2363 // Update the history of InputMethod and Subtype
2364 saveCurrentInputMethodAndSubtypeToHistory();
2365
2366 // Set Subtype here
2367 if (imi == null || subtypeId < 0) {
2368 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002369 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002370 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002371 if (subtypeId < imi.getSubtypeCount()) {
2372 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2373 mSettings.putSelectedSubtype(subtype.hashCode());
2374 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002375 } else {
2376 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2377 mCurrentSubtype = null;
2378 }
satokab751aa2010-09-14 19:17:36 +09002379 }
satok723a27e2010-11-11 14:58:11 +09002380
2381 if (!setSubtypeOnly) {
2382 // Set InputMethod here
2383 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2384 }
2385 }
2386
2387 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2388 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2389 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2390 // newDefaultIme is empty when there is no candidate for the selected IME.
2391 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2392 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2393 if (subtypeHashCode != null) {
2394 try {
2395 lastSubtypeId = getSubtypeIdFromHashCode(
2396 imi, Integer.valueOf(subtypeHashCode));
2397 } catch (NumberFormatException e) {
2398 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2399 }
2400 }
2401 }
2402 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002403 }
2404
2405 private int getSelectedInputMethodSubtypeId(String id) {
2406 InputMethodInfo imi = mMethodMap.get(id);
2407 if (imi == null) {
2408 return NOT_A_SUBTYPE_ID;
2409 }
satokab751aa2010-09-14 19:17:36 +09002410 int subtypeId;
2411 try {
2412 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2413 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2414 } catch (SettingNotFoundException e) {
2415 return NOT_A_SUBTYPE_ID;
2416 }
satok723a27e2010-11-11 14:58:11 +09002417 return getSubtypeIdFromHashCode(imi, subtypeId);
2418 }
2419
2420 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002421 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002422 final int subtypeCount = imi.getSubtypeCount();
2423 for (int i = 0; i < subtypeCount; ++i) {
2424 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002425 if (subtypeHashCode == ims.hashCode()) {
2426 return i;
2427 }
satokab751aa2010-09-14 19:17:36 +09002428 }
2429 }
2430 return NOT_A_SUBTYPE_ID;
2431 }
2432
satoka86f5e42011-09-02 17:12:42 +09002433 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2434 Resources res, InputMethodInfo imi) {
2435 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002436 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002437 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002438 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002439 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002440 final int N = subtypes.size();
2441 boolean containsKeyboardSubtype = false;
2442 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002443 // scan overriding implicitly enabled subtypes.
2444 InputMethodSubtype subtype = subtypes.get(i);
2445 if (subtype.overridesImplicitlyEnabledSubtype()) {
2446 final String mode = subtype.getMode();
2447 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2448 applicableModeAndSubtypesMap.put(mode, subtype);
2449 }
2450 }
2451 }
2452 if (applicableModeAndSubtypesMap.size() > 0) {
2453 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2454 }
2455 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002456 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002457 final String locale = subtype.getLocale();
2458 final String mode = subtype.getMode();
2459 // When system locale starts with subtype's locale, that subtype will be applicable
2460 // for system locale
2461 // For instance, it's clearly applicable for cases like system locale = en_US and
2462 // subtype = en, but it is not necessarily considered applicable for cases like system
2463 // locale = en and subtype = en_US.
2464 // We just call systemLocale.startsWith(locale) in this function because there is no
2465 // need to find applicable subtypes aggressively unlike
2466 // findLastResortApplicableSubtypeLocked.
2467 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002468 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002469 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002470 if (applicableSubtype != null) {
2471 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2472 if (!systemLocale.equals(locale)) continue;
2473 }
satok3da92232011-01-11 22:46:30 +09002474 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002475 if (!containsKeyboardSubtype
2476 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2477 containsKeyboardSubtype = true;
2478 }
2479 }
2480 }
satok4a28bde2011-06-29 21:03:40 +09002481 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002482 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002483 if (!containsKeyboardSubtype) {
2484 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002485 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002486 if (lastResortKeyboardSubtype != null) {
2487 applicableSubtypes.add(lastResortKeyboardSubtype);
2488 }
2489 }
2490 return applicableSubtypes;
2491 }
2492
satok4e4569d2010-11-19 18:45:53 +09002493 /**
2494 * If there are no selected subtypes, tries finding the most applicable one according to the
2495 * given locale.
2496 * @param subtypes this function will search the most applicable subtype in subtypes
2497 * @param mode subtypes will be filtered by mode
2498 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002499 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2500 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002501 * @return the most applicable subtypeId
2502 */
satokdf31ae62011-01-15 06:19:44 +09002503 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2504 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002505 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002506 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002507 return null;
satok8fbb1e82010-11-02 23:15:58 +09002508 }
satok4e4569d2010-11-19 18:45:53 +09002509 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002510 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002511 }
satok8fbb1e82010-11-02 23:15:58 +09002512 final String language = locale.substring(0, 2);
2513 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002514 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002515 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002516 final int N = subtypes.size();
2517 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002518 InputMethodSubtype subtype = subtypes.get(i);
2519 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002520 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2521 // and all subtypes with all modes can be candidates.
2522 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002523 if (firstMatchedModeSubtype == null) {
2524 firstMatchedModeSubtype = subtype;
2525 }
satok9ef02832010-11-04 21:17:48 +09002526 if (locale.equals(subtypeLocale)) {
2527 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002528 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002529 break;
2530 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2531 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002532 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002533 partialMatchFound = true;
2534 }
satok8fbb1e82010-11-02 23:15:58 +09002535 }
2536 }
2537
satok7599a7f2010-12-22 13:45:23 +09002538 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2539 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002540 }
2541
satok8fbb1e82010-11-02 23:15:58 +09002542 // The first subtype applicable to the system locale will be defined as the most applicable
2543 // subtype.
2544 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002545 if (applicableSubtype != null) {
2546 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2547 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2548 }
satok8fbb1e82010-11-02 23:15:58 +09002549 }
satokcd7cd292010-11-20 15:46:23 +09002550 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002551 }
2552
satok4e4569d2010-11-19 18:45:53 +09002553 // If there are no selected shortcuts, tries finding the most applicable ones.
2554 private Pair<InputMethodInfo, InputMethodSubtype>
2555 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2556 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2557 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002558 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002559 boolean foundInSystemIME = false;
2560
2561 // Search applicable subtype for each InputMethodInfo
2562 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002563 final String imiId = imi.getId();
2564 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2565 continue;
2566 }
satokcd7cd292010-11-20 15:46:23 +09002567 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002568 final List<InputMethodSubtype> enabledSubtypes =
2569 getEnabledInputMethodSubtypeList(imi, true);
2570 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002571 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002572 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002573 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002574 }
satokdf31ae62011-01-15 06:19:44 +09002575 // 2. Search by the system locale from enabledSubtypes.
2576 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002577 if (subtype == null) {
2578 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002579 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002580 }
satoka86f5e42011-09-02 17:12:42 +09002581 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2582 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2583 final ArrayList<InputMethodSubtype> subtypesForSearch =
2584 overridingImplicitlyEnabledSubtypes.isEmpty()
2585 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002586 // 4. Search by the current subtype's locale from all subtypes.
2587 if (subtype == null && mCurrentSubtype != null) {
2588 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002589 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002590 }
2591 // 5. Search by the system locale from all subtypes.
2592 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002593 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002594 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002595 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002596 }
satokcd7cd292010-11-20 15:46:23 +09002597 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002598 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002599 // The current input method is the most applicable IME.
2600 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002601 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002602 break;
satok7599a7f2010-12-22 13:45:23 +09002603 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002604 // The system input method is 2nd applicable IME.
2605 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002606 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002607 if ((imi.getServiceInfo().applicationInfo.flags
2608 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2609 foundInSystemIME = true;
2610 }
satok4e4569d2010-11-19 18:45:53 +09002611 }
2612 }
2613 }
2614 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002615 if (mostApplicableIMI != null) {
2616 Slog.w(TAG, "Most applicable shortcut input method was:"
2617 + mostApplicableIMI.getId());
2618 if (mostApplicableSubtype != null) {
2619 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2620 + "," + mostApplicableSubtype.getMode() + ","
2621 + mostApplicableSubtype.getLocale());
2622 }
2623 }
satok4e4569d2010-11-19 18:45:53 +09002624 }
satokcd7cd292010-11-20 15:46:23 +09002625 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002626 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002627 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002628 } else {
2629 return null;
2630 }
2631 }
2632
satokab751aa2010-09-14 19:17:36 +09002633 /**
2634 * @return Return the current subtype of this input method.
2635 */
satok42c5a162011-05-26 16:46:14 +09002636 @Override
satokab751aa2010-09-14 19:17:36 +09002637 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002638 boolean subtypeIsSelected = false;
2639 try {
2640 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2641 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2642 } catch (SettingNotFoundException e) {
2643 }
satok3ef8b292010-11-23 06:06:29 +09002644 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002645 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002646 String lastInputMethodId = Settings.Secure.getString(
2647 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002648 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2649 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002650 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2651 if (imi != null) {
2652 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002653 // the most applicable subtype from explicitly or implicitly enabled
2654 // subtypes.
2655 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2656 getEnabledInputMethodSubtypeList(imi, true);
2657 // If there is only one explicitly or implicitly enabled subtype,
2658 // just returns it.
2659 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2660 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2661 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2662 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2663 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2664 SUBTYPE_MODE_KEYBOARD, null, true);
2665 if (mCurrentSubtype == null) {
2666 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2667 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2668 true);
2669 }
2670 }
satok4e4569d2010-11-19 18:45:53 +09002671 }
satokcd7cd292010-11-20 15:46:23 +09002672 } else {
satok3ef8b292010-11-23 06:06:29 +09002673 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002674 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002675 }
satok8fbb1e82010-11-02 23:15:58 +09002676 }
satok3ef8b292010-11-23 06:06:29 +09002677 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002678 }
satokab751aa2010-09-14 19:17:36 +09002679 }
2680
satokf3db1af2010-11-23 13:34:33 +09002681 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2682 InputMethodSubtype subtype) {
2683 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2684 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2685 } else {
2686 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2687 subtypes.add(subtype);
2688 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2689 }
2690 }
2691
satok4e4569d2010-11-19 18:45:53 +09002692 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002693 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002694 @Override
satok4e4569d2010-11-19 18:45:53 +09002695 public List getShortcutInputMethodsAndSubtypes() {
2696 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002697 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002698 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002699 // If there are no selected shortcut subtypes, the framework will try to find
2700 // the most applicable subtype from all subtypes whose mode is
2701 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002702 Pair<InputMethodInfo, InputMethodSubtype> info =
2703 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2704 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002705 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002706 ret.add(info.first);
2707 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002708 }
satok3da92232011-01-11 22:46:30 +09002709 return ret;
satokf3db1af2010-11-23 13:34:33 +09002710 }
satokf3db1af2010-11-23 13:34:33 +09002711 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2712 ret.add(imi);
2713 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2714 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002715 }
2716 }
satokf3db1af2010-11-23 13:34:33 +09002717 return ret;
satok4e4569d2010-11-19 18:45:53 +09002718 }
2719 }
2720
satok42c5a162011-05-26 16:46:14 +09002721 @Override
satokb66d2872010-11-10 01:04:04 +09002722 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2723 synchronized (mMethodMap) {
2724 if (subtype != null && mCurMethodId != null) {
2725 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2726 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2727 if (subtypeId != NOT_A_SUBTYPE_ID) {
2728 setInputMethodLocked(mCurMethodId, subtypeId);
2729 return true;
2730 }
2731 }
2732 return false;
2733 }
2734 }
2735
satokd87c2592010-09-29 11:52:06 +09002736 /**
2737 * Utility class for putting and getting settings for InputMethod
2738 * TODO: Move all putters and getters of settings to this class.
2739 */
2740 private static class InputMethodSettings {
2741 // The string for enabled input method is saved as follows:
2742 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2743 private static final char INPUT_METHOD_SEPARATER = ':';
2744 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002745 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002746 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2747
satok723a27e2010-11-11 14:58:11 +09002748 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002749 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2750
satokdf31ae62011-01-15 06:19:44 +09002751 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002752 private final ContentResolver mResolver;
2753 private final HashMap<String, InputMethodInfo> mMethodMap;
2754 private final ArrayList<InputMethodInfo> mMethodList;
2755
2756 private String mEnabledInputMethodsStrCache;
2757
2758 private static void buildEnabledInputMethodsSettingString(
2759 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2760 String id = pair.first;
2761 ArrayList<String> subtypes = pair.second;
2762 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002763 // Inputmethod and subtypes are saved in the settings as follows:
2764 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2765 for (String subtypeId: subtypes) {
2766 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002767 }
2768 }
2769
2770 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002771 Resources res, ContentResolver resolver,
2772 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2773 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002774 mResolver = resolver;
2775 mMethodMap = methodMap;
2776 mMethodList = methodList;
2777 }
2778
2779 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2780 return createEnabledInputMethodListLocked(
2781 getEnabledInputMethodsAndSubtypeListLocked());
2782 }
2783
satok7f35c8c2010-10-07 21:13:11 +09002784 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002785 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2786 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002787 getEnabledInputMethodsAndSubtypeListLocked());
2788 }
2789
satok67ddf9c2010-11-17 09:45:54 +09002790 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2791 InputMethodInfo imi) {
2792 List<Pair<String, ArrayList<String>>> imsList =
2793 getEnabledInputMethodsAndSubtypeListLocked();
2794 ArrayList<InputMethodSubtype> enabledSubtypes =
2795 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002796 if (imi != null) {
2797 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2798 InputMethodInfo info = mMethodMap.get(imsPair.first);
2799 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002800 final int subtypeCount = info.getSubtypeCount();
2801 for (int i = 0; i < subtypeCount; ++i) {
2802 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002803 for (String s: imsPair.second) {
2804 if (String.valueOf(ims.hashCode()).equals(s)) {
2805 enabledSubtypes.add(ims);
2806 }
satok67ddf9c2010-11-17 09:45:54 +09002807 }
2808 }
satok884ef9a2010-11-18 10:39:46 +09002809 break;
satok67ddf9c2010-11-17 09:45:54 +09002810 }
satok67ddf9c2010-11-17 09:45:54 +09002811 }
2812 }
2813 return enabledSubtypes;
2814 }
2815
satokd87c2592010-09-29 11:52:06 +09002816 // At the initial boot, the settings for input methods are not set,
2817 // so we need to enable IME in that case.
2818 public void enableAllIMEsIfThereIsNoEnabledIME() {
2819 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2820 StringBuilder sb = new StringBuilder();
2821 final int N = mMethodList.size();
2822 for (int i = 0; i < N; i++) {
2823 InputMethodInfo imi = mMethodList.get(i);
2824 Slog.i(TAG, "Adding: " + imi.getId());
2825 if (i > 0) sb.append(':');
2826 sb.append(imi.getId());
2827 }
2828 putEnabledInputMethodsStr(sb.toString());
2829 }
2830 }
2831
satokbb4aa062011-01-19 21:40:27 +09002832 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002833 ArrayList<Pair<String, ArrayList<String>>> imsList
2834 = new ArrayList<Pair<String, ArrayList<String>>>();
2835 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2836 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2837 return imsList;
2838 }
satok723a27e2010-11-11 14:58:11 +09002839 mInputMethodSplitter.setString(enabledInputMethodsStr);
2840 while (mInputMethodSplitter.hasNext()) {
2841 String nextImsStr = mInputMethodSplitter.next();
2842 mSubtypeSplitter.setString(nextImsStr);
2843 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002844 ArrayList<String> subtypeHashes = new ArrayList<String>();
2845 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002846 String imeId = mSubtypeSplitter.next();
2847 while (mSubtypeSplitter.hasNext()) {
2848 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002849 }
2850 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2851 }
2852 }
2853 return imsList;
2854 }
2855
2856 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2857 if (reloadInputMethodStr) {
2858 getEnabledInputMethodsStr();
2859 }
2860 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2861 // Add in the newly enabled input method.
2862 putEnabledInputMethodsStr(id);
2863 } else {
2864 putEnabledInputMethodsStr(
2865 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2866 }
2867 }
2868
2869 /**
2870 * Build and put a string of EnabledInputMethods with removing specified Id.
2871 * @return the specified id was removed or not.
2872 */
2873 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2874 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2875 boolean isRemoved = false;
2876 boolean needsAppendSeparator = false;
2877 for (Pair<String, ArrayList<String>> ims: imsList) {
2878 String curId = ims.first;
2879 if (curId.equals(id)) {
2880 // We are disabling this input method, and it is
2881 // currently enabled. Skip it to remove from the
2882 // new list.
2883 isRemoved = true;
2884 } else {
2885 if (needsAppendSeparator) {
2886 builder.append(INPUT_METHOD_SEPARATER);
2887 } else {
2888 needsAppendSeparator = true;
2889 }
2890 buildEnabledInputMethodsSettingString(builder, ims);
2891 }
2892 }
2893 if (isRemoved) {
2894 // Update the setting with the new list of input methods.
2895 putEnabledInputMethodsStr(builder.toString());
2896 }
2897 return isRemoved;
2898 }
2899
2900 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2901 List<Pair<String, ArrayList<String>>> imsList) {
2902 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2903 for (Pair<String, ArrayList<String>> ims: imsList) {
2904 InputMethodInfo info = mMethodMap.get(ims.first);
2905 if (info != null) {
2906 res.add(info);
2907 }
2908 }
2909 return res;
2910 }
2911
satok7f35c8c2010-10-07 21:13:11 +09002912 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002913 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002914 List<Pair<String, ArrayList<String>>> imsList) {
2915 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2916 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2917 for (Pair<String, ArrayList<String>> ims : imsList) {
2918 InputMethodInfo info = mMethodMap.get(ims.first);
2919 if (info != null) {
2920 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2921 }
2922 }
2923 return res;
2924 }
2925
satokd87c2592010-09-29 11:52:06 +09002926 private void putEnabledInputMethodsStr(String str) {
2927 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2928 mEnabledInputMethodsStrCache = str;
2929 }
2930
2931 private String getEnabledInputMethodsStr() {
2932 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2933 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002934 if (DEBUG) {
2935 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2936 }
satokd87c2592010-09-29 11:52:06 +09002937 return mEnabledInputMethodsStrCache;
2938 }
satok723a27e2010-11-11 14:58:11 +09002939
2940 private void saveSubtypeHistory(
2941 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2942 StringBuilder builder = new StringBuilder();
2943 boolean isImeAdded = false;
2944 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2945 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2946 newSubtypeId);
2947 isImeAdded = true;
2948 }
2949 for (Pair<String, String> ime: savedImes) {
2950 String imeId = ime.first;
2951 String subtypeId = ime.second;
2952 if (TextUtils.isEmpty(subtypeId)) {
2953 subtypeId = NOT_A_SUBTYPE_ID_STR;
2954 }
2955 if (isImeAdded) {
2956 builder.append(INPUT_METHOD_SEPARATER);
2957 } else {
2958 isImeAdded = true;
2959 }
2960 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2961 subtypeId);
2962 }
2963 // Remove the last INPUT_METHOD_SEPARATER
2964 putSubtypeHistoryStr(builder.toString());
2965 }
2966
2967 public void addSubtypeToHistory(String imeId, String subtypeId) {
2968 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2969 for (Pair<String, String> ime: subtypeHistory) {
2970 if (ime.first.equals(imeId)) {
2971 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002972 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002973 + ime.second);
2974 }
2975 // We should break here
2976 subtypeHistory.remove(ime);
2977 break;
2978 }
2979 }
satokbb4aa062011-01-19 21:40:27 +09002980 if (DEBUG) {
2981 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2982 }
satok723a27e2010-11-11 14:58:11 +09002983 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2984 }
2985
2986 private void putSubtypeHistoryStr(String str) {
2987 if (DEBUG) {
2988 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2989 }
2990 Settings.Secure.putString(
2991 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2992 }
2993
2994 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2995 // Gets the first one from the history
2996 return getLastSubtypeForInputMethodLockedInternal(null);
2997 }
2998
2999 public String getLastSubtypeForInputMethodLocked(String imeId) {
3000 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3001 if (ime != null) {
3002 return ime.second;
3003 } else {
3004 return null;
3005 }
3006 }
3007
3008 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3009 List<Pair<String, ArrayList<String>>> enabledImes =
3010 getEnabledInputMethodsAndSubtypeListLocked();
3011 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003012 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003013 final String imeInTheHistory = imeAndSubtype.first;
3014 // If imeId is empty, returns the first IME and subtype in the history
3015 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3016 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003017 final String subtypeHashCode =
3018 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3019 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003020 if (!TextUtils.isEmpty(subtypeHashCode)) {
3021 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003022 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003023 }
3024 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3025 }
3026 }
3027 }
3028 if (DEBUG) {
3029 Slog.d(TAG, "No enabled IME found in the history");
3030 }
3031 return null;
3032 }
3033
satokdf31ae62011-01-15 06:19:44 +09003034 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003035 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3036 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3037 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003038 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3039 if (explicitlyEnabledSubtypes.size() == 0) {
3040 // If there are no explicitly enabled subtypes, applicable subtypes are
3041 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003042 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003043 // If IME is enabled and no subtypes are enabled, applicable subtypes
3044 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003045 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003046 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003047 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003048 if (implicitlySelectedSubtypes != null) {
3049 final int N = implicitlySelectedSubtypes.size();
3050 for (int i = 0; i < N; ++i) {
3051 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3052 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3053 return subtypeHashCode;
3054 }
3055 }
3056 }
3057 }
3058 } else {
satokf6cafb62011-01-17 16:29:02 +09003059 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003060 if (s.equals(subtypeHashCode)) {
3061 // If both imeId and subtypeId are enabled, return subtypeId.
3062 return s;
3063 }
satok723a27e2010-11-11 14:58:11 +09003064 }
3065 }
3066 // If imeId was enabled but subtypeId was disabled.
3067 return NOT_A_SUBTYPE_ID_STR;
3068 }
3069 }
3070 // If both imeId and subtypeId are disabled, return null
3071 return null;
3072 }
3073
3074 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3075 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3076 final String subtypeHistoryStr = getSubtypeHistoryStr();
3077 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3078 return imsList;
3079 }
3080 mInputMethodSplitter.setString(subtypeHistoryStr);
3081 while (mInputMethodSplitter.hasNext()) {
3082 String nextImsStr = mInputMethodSplitter.next();
3083 mSubtypeSplitter.setString(nextImsStr);
3084 if (mSubtypeSplitter.hasNext()) {
3085 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3086 // The first element is ime id.
3087 String imeId = mSubtypeSplitter.next();
3088 while (mSubtypeSplitter.hasNext()) {
3089 subtypeId = mSubtypeSplitter.next();
3090 break;
3091 }
3092 imsList.add(new Pair<String, String>(imeId, subtypeId));
3093 }
3094 }
3095 return imsList;
3096 }
3097
3098 private String getSubtypeHistoryStr() {
3099 if (DEBUG) {
3100 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3101 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3102 }
3103 return Settings.Secure.getString(
3104 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3105 }
3106
3107 public void putSelectedInputMethod(String imeId) {
3108 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3109 }
3110
3111 public void putSelectedSubtype(int subtypeId) {
3112 Settings.Secure.putInt(
3113 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3114 }
satokd87c2592010-09-29 11:52:06 +09003115 }
3116
satoke7c6998e2011-06-03 17:57:59 +09003117 private static class InputMethodFileManager {
3118 private static final String SYSTEM_PATH = "system";
3119 private static final String INPUT_METHOD_PATH = "inputmethod";
3120 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3121 private static final String NODE_SUBTYPES = "subtypes";
3122 private static final String NODE_SUBTYPE = "subtype";
3123 private static final String NODE_IMI = "imi";
3124 private static final String ATTR_ID = "id";
3125 private static final String ATTR_LABEL = "label";
3126 private static final String ATTR_ICON = "icon";
3127 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3128 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3129 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3130 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3131 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3132 private final HashMap<String, InputMethodInfo> mMethodMap;
3133 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3134 new HashMap<String, List<InputMethodSubtype>>();
3135 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3136 if (methodMap == null) {
3137 throw new NullPointerException("methodMap is null");
3138 }
3139 mMethodMap = methodMap;
3140 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3141 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3142 if (!inputMethodDir.mkdirs()) {
3143 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3144 }
3145 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3146 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3147 if (!subtypeFile.exists()) {
3148 // If "subtypes.xml" doesn't exist, create a blank file.
3149 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3150 methodMap);
3151 } else {
3152 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3153 }
3154 }
3155
3156 private void deleteAllInputMethodSubtypes(String imiId) {
3157 synchronized (mMethodMap) {
3158 mSubtypesMap.remove(imiId);
3159 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3160 mMethodMap);
3161 }
3162 }
3163
3164 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003165 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003166 synchronized (mMethodMap) {
3167 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3168 final int N = additionalSubtypes.length;
3169 for (int i = 0; i < N; ++i) {
3170 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003171 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003172 subtypes.add(subtype);
3173 }
3174 }
satok4a28bde2011-06-29 21:03:40 +09003175 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003176 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3177 mMethodMap);
3178 }
3179 }
3180
3181 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3182 synchronized (mMethodMap) {
3183 return mSubtypesMap;
3184 }
3185 }
3186
3187 private static void writeAdditionalInputMethodSubtypes(
3188 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3189 HashMap<String, InputMethodInfo> methodMap) {
3190 // Safety net for the case that this function is called before methodMap is set.
3191 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3192 FileOutputStream fos = null;
3193 try {
3194 fos = subtypesFile.startWrite();
3195 final XmlSerializer out = new FastXmlSerializer();
3196 out.setOutput(fos, "utf-8");
3197 out.startDocument(null, true);
3198 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3199 out.startTag(null, NODE_SUBTYPES);
3200 for (String imiId : allSubtypes.keySet()) {
3201 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3202 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3203 continue;
3204 }
3205 out.startTag(null, NODE_IMI);
3206 out.attribute(null, ATTR_ID, imiId);
3207 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3208 final int N = subtypesList.size();
3209 for (int i = 0; i < N; ++i) {
3210 final InputMethodSubtype subtype = subtypesList.get(i);
3211 out.startTag(null, NODE_SUBTYPE);
3212 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3213 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3214 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3215 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3216 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3217 out.attribute(null, ATTR_IS_AUXILIARY,
3218 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3219 out.endTag(null, NODE_SUBTYPE);
3220 }
3221 out.endTag(null, NODE_IMI);
3222 }
3223 out.endTag(null, NODE_SUBTYPES);
3224 out.endDocument();
3225 subtypesFile.finishWrite(fos);
3226 } catch (java.io.IOException e) {
3227 Slog.w(TAG, "Error writing subtypes", e);
3228 if (fos != null) {
3229 subtypesFile.failWrite(fos);
3230 }
3231 }
3232 }
3233
3234 private static void readAdditionalInputMethodSubtypes(
3235 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3236 if (allSubtypes == null || subtypesFile == null) return;
3237 allSubtypes.clear();
3238 FileInputStream fis = null;
3239 try {
3240 fis = subtypesFile.openRead();
3241 final XmlPullParser parser = Xml.newPullParser();
3242 parser.setInput(fis, null);
3243 int type = parser.getEventType();
3244 // Skip parsing until START_TAG
3245 while ((type = parser.next()) != XmlPullParser.START_TAG
3246 && type != XmlPullParser.END_DOCUMENT) {}
3247 String firstNodeName = parser.getName();
3248 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3249 throw new XmlPullParserException("Xml doesn't start with subtypes");
3250 }
3251 final int depth =parser.getDepth();
3252 String currentImiId = null;
3253 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3254 while (((type = parser.next()) != XmlPullParser.END_TAG
3255 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3256 if (type != XmlPullParser.START_TAG)
3257 continue;
3258 final String nodeName = parser.getName();
3259 if (NODE_IMI.equals(nodeName)) {
3260 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3261 if (TextUtils.isEmpty(currentImiId)) {
3262 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3263 continue;
3264 }
3265 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3266 allSubtypes.put(currentImiId, tempSubtypesArray);
3267 } else if (NODE_SUBTYPE.equals(nodeName)) {
3268 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3269 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3270 continue;
3271 }
3272 final int icon = Integer.valueOf(
3273 parser.getAttributeValue(null, ATTR_ICON));
3274 final int label = Integer.valueOf(
3275 parser.getAttributeValue(null, ATTR_LABEL));
3276 final String imeSubtypeLocale =
3277 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3278 final String imeSubtypeMode =
3279 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3280 final String imeSubtypeExtraValue =
3281 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003282 final boolean isAuxiliary = "1".equals(String.valueOf(
3283 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003284 final InputMethodSubtype subtype =
3285 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3286 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3287 tempSubtypesArray.add(subtype);
3288 }
3289 }
3290 } catch (XmlPullParserException e) {
3291 Slog.w(TAG, "Error reading subtypes: " + e);
3292 return;
3293 } catch (java.io.IOException e) {
3294 Slog.w(TAG, "Error reading subtypes: " + e);
3295 return;
3296 } catch (NumberFormatException e) {
3297 Slog.w(TAG, "Error reading subtypes: " + e);
3298 return;
3299 } finally {
3300 if (fis != null) {
3301 try {
3302 fis.close();
3303 } catch (java.io.IOException e1) {
3304 Slog.w(TAG, "Failed to close.");
3305 }
3306 }
3307 }
3308 }
3309 }
3310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 @Override
3314 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3315 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3316 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3319 + Binder.getCallingPid()
3320 + ", uid=" + Binder.getCallingUid());
3321 return;
3322 }
3323
3324 IInputMethod method;
3325 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 synchronized (mMethodMap) {
3330 p.println("Current Input Method Manager state:");
3331 int N = mMethodList.size();
3332 p.println(" Input Methods:");
3333 for (int i=0; i<N; i++) {
3334 InputMethodInfo info = mMethodList.get(i);
3335 p.println(" InputMethod #" + i + ":");
3336 info.dump(p, " ");
3337 }
3338 p.println(" Clients:");
3339 for (ClientState ci : mClients.values()) {
3340 p.println(" Client " + ci + ":");
3341 p.println(" client=" + ci.client);
3342 p.println(" inputContext=" + ci.inputContext);
3343 p.println(" sessionRequested=" + ci.sessionRequested);
3344 p.println(" curSession=" + ci.curSession);
3345 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003346 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003348 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3349 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3351 + " mBoundToMethod=" + mBoundToMethod);
3352 p.println(" mCurToken=" + mCurToken);
3353 p.println(" mCurIntent=" + mCurIntent);
3354 method = mCurMethod;
3355 p.println(" mCurMethod=" + mCurMethod);
3356 p.println(" mEnabledSession=" + mEnabledSession);
3357 p.println(" mShowRequested=" + mShowRequested
3358 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3359 + " mShowForced=" + mShowForced
3360 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003361 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003363
Jeff Brownb88102f2010-09-08 11:49:43 -07003364 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 pw.flush();
3367 try {
3368 client.client.asBinder().dump(fd, args);
3369 } catch (RemoteException e) {
3370 p.println("Input method client dead: " + e);
3371 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003372 } else {
3373 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003375
Jeff Brownb88102f2010-09-08 11:49:43 -07003376 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 pw.flush();
3379 try {
3380 method.asBinder().dump(fd, args);
3381 } catch (RemoteException e) {
3382 p.println("Input method service dead: " + e);
3383 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003384 } else {
3385 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 }
3387 }
3388}