blob: 6aa95c1f1f5545d0a99a9bdb6937550e2e6725e1 [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;
83import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090084import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.inputmethod.InputBinding;
86import android.view.inputmethod.InputMethod;
87import android.view.inputmethod.InputMethodInfo;
88import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090089import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
satoke7c6998e2011-06-03 17:57:59 +090091import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090093import java.io.FileInputStream;
94import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.IOException;
96import java.io.PrintWriter;
97import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +090098import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900100import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.util.List;
satok913a8922010-08-26 21:53:41 +0900102import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104/**
105 * This class provides a system service that manages input methods.
106 */
107public class InputMethodManagerService extends IInputMethodManager.Stub
108 implements ServiceConnection, Handler.Callback {
109 static final boolean DEBUG = false;
110 static final String TAG = "InputManagerService";
111
112 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900113 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900114 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900115 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 static final int MSG_UNBIND_INPUT = 1000;
118 static final int MSG_BIND_INPUT = 1010;
119 static final int MSG_SHOW_SOFT_INPUT = 1020;
120 static final int MSG_HIDE_SOFT_INPUT = 1030;
121 static final int MSG_ATTACH_TOKEN = 1040;
122 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 static final int MSG_START_INPUT = 2000;
125 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 static final int MSG_UNBIND_METHOD = 3000;
128 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
satokf9f01002011-05-19 21:31:50 +0900132 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
133
satokab751aa2010-09-14 19:17:36 +0900134 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900135 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900136 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
137 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900138 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800141 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900143 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final IWindowManager mIWindowManager;
146 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900147 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 // All known input methods. mMethodMap also serves as the global
152 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900153 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
154 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900155 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
156 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
satok7cfc0ed2011-06-20 21:29:36 +0900158 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700159 private NotificationManager mNotificationManager;
160 private KeyguardManager mKeyguardManager;
161 private StatusBarManagerService mStatusBar;
162 private Notification mImeSwitcherNotification;
163 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900164 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900165 private boolean mNotificationShown;
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 class SessionState {
168 final ClientState client;
169 final IInputMethod method;
170 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 @Override
173 public String toString() {
174 return "SessionState{uid " + client.uid + " pid " + client.pid
175 + " method " + Integer.toHexString(
176 System.identityHashCode(method))
177 + " session " + Integer.toHexString(
178 System.identityHashCode(session))
179 + "}";
180 }
181
182 SessionState(ClientState _client, IInputMethod _method,
183 IInputMethodSession _session) {
184 client = _client;
185 method = _method;
186 session = _session;
187 }
188 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 class ClientState {
191 final IInputMethodClient client;
192 final IInputContext inputContext;
193 final int uid;
194 final int pid;
195 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 boolean sessionRequested;
198 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 @Override
201 public String toString() {
202 return "ClientState{" + Integer.toHexString(
203 System.identityHashCode(this)) + " uid " + uid
204 + " pid " + pid + "}";
205 }
206
207 ClientState(IInputMethodClient _client, IInputContext _inputContext,
208 int _uid, int _pid) {
209 client = _client;
210 inputContext = _inputContext;
211 uid = _uid;
212 pid = _pid;
213 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
214 }
215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 final HashMap<IBinder, ClientState> mClients
218 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700221 * Set once the system is ready to run third party code.
222 */
223 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700225 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * Id of the currently selected input method.
227 */
228 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 /**
231 * The current binding sequence number, incremented every time there is
232 * a new bind performed.
233 */
234 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The client that is currently bound to an input method.
238 */
239 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700242 * The last window token that gained focus.
243 */
244 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * The input context last provided by the current client.
248 */
249 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * The attributes last provided by the current client.
253 */
254 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * The input method ID of the input method service that we are currently
258 * connected to or in the process of connecting to.
259 */
260 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
satokab751aa2010-09-14 19:17:36 +0900263 * The current subtype of the current input method.
264 */
265 private InputMethodSubtype mCurrentSubtype;
266
satok4e4569d2010-11-19 18:45:53 +0900267 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900268 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
269 mShortcutInputMethodsAndSubtypes =
270 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900271
272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * Set to true if our ServiceConnection is currently actively bound to
274 * a service (whether or not we have gotten its IBinder back yet).
275 */
276 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 /**
279 * Set if the client has asked for the input method to be shown.
280 */
281 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Set if we were explicitly told to show the input method.
285 */
286 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Set if we were forced to be shown.
290 */
291 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Set if we last told the input method to show itself.
295 */
296 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * The Intent used to connect to the current input method.
300 */
301 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * The token we have made for the currently active input method, to
305 * identify it in the future.
306 */
307 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * If non-null, this is the input method service we are currently connected
311 * to.
312 */
313 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * Time that we last initiated a bind to the input method, to determine
317 * if we should try to disconnect and reconnect to it.
318 */
319 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Have we called mCurMethod.bindInput()?
323 */
324 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Currently enabled session. Only touched by service thread, not
328 * protected by a lock.
329 */
330 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * True if the screen is on. The value is true initially.
334 */
335 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
Joe Onorato857fd9b2011-01-27 15:08:35 -0800337 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
338 int mImeWindowVis;
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 AlertDialog.Builder mDialogBuilder;
341 AlertDialog mSwitchingDialog;
342 InputMethodInfo[] mIms;
343 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900344 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 class SettingsObserver extends ContentObserver {
347 SettingsObserver(Handler handler) {
348 super(handler);
349 ContentResolver resolver = mContext.getContentResolver();
350 resolver.registerContentObserver(Settings.Secure.getUriFor(
351 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900352 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900353 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
354 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900355 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 @Override public void onChange(boolean selfChange) {
359 synchronized (mMethodMap) {
360 updateFromSettingsLocked();
361 }
362 }
363 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
366 @Override
367 public void onReceive(Context context, Intent intent) {
368 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
369 mScreenOn = true;
370 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
371 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700372 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
373 hideInputMethodMenu();
374 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800376 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378
379 // Inform the current client of the change in active status
380 try {
381 if (mCurClient != null && mCurClient.client != null) {
382 mCurClient.client.setActive(mScreenOn);
383 }
384 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800385 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 + mCurClient.pid + " uid " + mCurClient.uid);
387 }
388 }
389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800391 class MyPackageMonitor extends PackageMonitor {
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800394 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
398 final int N = mMethodList.size();
399 if (curInputMethodId != null) {
400 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 InputMethodInfo imi = mMethodList.get(i);
402 if (imi.getId().equals(curInputMethodId)) {
403 for (String pkg : packages) {
404 if (imi.getPackageName().equals(pkg)) {
405 if (!doit) {
406 return true;
407 }
satok723a27e2010-11-11 14:58:11 +0900408 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800409 chooseNewDefaultIMELocked();
410 return true;
411 }
412 }
413 }
414 }
415 }
416 }
417 return false;
418 }
419
420 @Override
421 public void onSomePackagesChanged() {
422 synchronized (mMethodMap) {
423 InputMethodInfo curIm = null;
424 String curInputMethodId = Settings.Secure.getString(mContext
425 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
426 final int N = mMethodList.size();
427 if (curInputMethodId != null) {
428 for (int i=0; i<N; i++) {
429 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900430 final String imiId = imi.getId();
431 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 curIm = imi;
433 }
satoke7c6998e2011-06-03 17:57:59 +0900434
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800435 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900436 if (isPackageModified(imi.getPackageName())) {
437 mFileManager.deleteAllInputMethodSubtypes(imiId);
438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 if (change == PACKAGE_TEMPORARY_CHANGE
440 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800441 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 + imi.getComponent());
443 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 buildInputMethodListLocked(mMethodList, mMethodMap);
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800452 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 int change = isPackageDisappearing(curIm.getPackageName());
454 if (change == PACKAGE_TEMPORARY_CHANGE
455 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800456 ServiceInfo si = null;
457 try {
458 si = mContext.getPackageManager().getServiceInfo(
459 curIm.getComponent(), 0);
460 } catch (PackageManager.NameNotFoundException ex) {
461 }
462 if (si == null) {
463 // Uh oh, current input method is no longer around!
464 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800465 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800466 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700467 updateImeWindowStatusLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800468 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800469 changed = true;
470 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800471 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900472 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800473 }
474 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800476 }
satokab751aa2010-09-14 19:17:36 +0900477
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 if (curIm == null) {
479 // We currently don't have a default input method... is
480 // one now available?
481 changed = chooseNewDefaultIMELocked();
482 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800483
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 if (changed) {
485 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
488 }
489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 class MethodCallback extends IInputMethodCallback.Stub {
492 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 MethodCallback(IInputMethod method) {
495 mMethod = method;
496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
satoke7c6998e2011-06-03 17:57:59 +0900498 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public void finishedEvent(int seq, boolean handled) throws RemoteException {
500 }
501
satoke7c6998e2011-06-03 17:57:59 +0900502 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 public void sessionCreated(IInputMethodSession session) throws RemoteException {
504 onSessionCreated(mMethod, session);
505 }
506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800507
Dianne Hackborn661cd522011-08-22 00:26:20 -0700508 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800510 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 mHandler = new Handler(this);
512 mIWindowManager = IWindowManager.Stub.asInterface(
513 ServiceManager.getService(Context.WINDOW_SERVICE));
514 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public void executeMessage(Message msg) {
517 handleMessage(msg);
518 }
519 });
satok7cfc0ed2011-06-20 21:29:36 +0900520
satok7cfc0ed2011-06-20 21:29:36 +0900521 mImeSwitcherNotification = new Notification();
522 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
523 mImeSwitcherNotification.when = 0;
524 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
525 mImeSwitcherNotification.tickerText = null;
526 mImeSwitcherNotification.defaults = 0; // please be quiet
527 mImeSwitcherNotification.sound = null;
528 mImeSwitcherNotification.vibrate = null;
529 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900530 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900531
532 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900533
satoke7c6998e2011-06-03 17:57:59 +0900534 synchronized (mMethodMap) {
535 mFileManager = new InputMethodFileManager(mMethodMap);
536 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800537
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800538 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 IntentFilter screenOnOffFilt = new IntentFilter();
541 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
542 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700543 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
satok7cfc0ed2011-06-20 21:29:36 +0900546 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900547
satokd87c2592010-09-29 11:52:06 +0900548 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900549 mSettings = new InputMethodSettings(
550 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900552 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
satokd87c2592010-09-29 11:52:06 +0900554 if (TextUtils.isEmpty(Settings.Secure.getString(
555 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900557 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
559 try {
satokd87c2592010-09-29 11:52:06 +0900560 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 imi.getPackageName(), 0).getResources();
562 if (res.getBoolean(imi.getIsDefaultResourceId())) {
563 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800564 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 } catch (PackageManager.NameNotFoundException ex) {
567 } catch (Resources.NotFoundException ex) {
568 }
569 }
570 }
satokd87c2592010-09-29 11:52:06 +0900571 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800573 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900576 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 mSettingsObserver = new SettingsObserver(mHandler);
581 updateFromSettingsLocked();
582 }
583
584 @Override
585 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
586 throws RemoteException {
587 try {
588 return super.onTransact(code, data, reply, flags);
589 } catch (RuntimeException e) {
590 // The input method manager only throws security exceptions, so let's
591 // log all others.
592 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800593 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595 throw e;
596 }
597 }
598
Dianne Hackborn661cd522011-08-22 00:26:20 -0700599 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700600 synchronized (mMethodMap) {
601 if (!mSystemReady) {
602 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700603 mKeyguardManager = (KeyguardManager)
604 mContext.getSystemService(Context.KEYGUARD_SERVICE);
605 mNotificationManager = (NotificationManager)
606 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
607 mStatusBar = statusBar;
608 statusBar.setIconVisibility("ime", false);
609 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900610 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
611 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700612 try {
613 startInputInnerLocked();
614 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800615 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700616 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700617 }
618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800620
Dianne Hackborn661cd522011-08-22 00:26:20 -0700621 void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900622 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700623 }
624
satoke7c6998e2011-06-03 17:57:59 +0900625 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 public List<InputMethodInfo> getInputMethodList() {
627 synchronized (mMethodMap) {
628 return new ArrayList<InputMethodInfo>(mMethodList);
629 }
630 }
631
satoke7c6998e2011-06-03 17:57:59 +0900632 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 public List<InputMethodInfo> getEnabledInputMethodList() {
634 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900635 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637 }
638
satokbb4aa062011-01-19 21:40:27 +0900639 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
640 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
641 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
642 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
643 for (InputMethodInfo imi: getEnabledInputMethodList()) {
644 enabledInputMethodAndSubtypes.put(
645 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
646 }
647 return enabledInputMethodAndSubtypes;
648 }
649
650 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
651 boolean allowsImplicitlySelectedSubtypes) {
652 if (imi == null && mCurMethodId != null) {
653 imi = mMethodMap.get(mCurMethodId);
654 }
satok7265d9b2011-02-14 15:47:30 +0900655 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900656 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900657 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900658 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900659 }
satok7265d9b2011-02-14 15:47:30 +0900660 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900661 }
662
satoke7c6998e2011-06-03 17:57:59 +0900663 @Override
satok16331c82010-12-20 23:48:46 +0900664 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
665 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900666 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900667 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900668 }
669 }
670
satoke7c6998e2011-06-03 17:57:59 +0900671 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 public void addClient(IInputMethodClient client,
673 IInputContext inputContext, int uid, int pid) {
674 synchronized (mMethodMap) {
675 mClients.put(client.asBinder(), new ClientState(client,
676 inputContext, uid, pid));
677 }
678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
satoke7c6998e2011-06-03 17:57:59 +0900680 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 public void removeClient(IInputMethodClient client) {
682 synchronized (mMethodMap) {
683 mClients.remove(client.asBinder());
684 }
685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 void executeOrSendMessage(IInterface target, Message msg) {
688 if (target.asBinder() instanceof Binder) {
689 mCaller.sendMessage(msg);
690 } else {
691 handleMessage(msg);
692 msg.recycle();
693 }
694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700696 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 + mCurClient.client.asBinder());
700 if (mBoundToMethod) {
701 mBoundToMethod = false;
702 if (mCurMethod != null) {
703 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
704 MSG_UNBIND_INPUT, mCurMethod));
705 }
706 }
707 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
708 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
709 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 // Call setActive(false) on the old client
712 try {
713 mCurClient.client.setActive(false);
714 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800715 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 + mCurClient.pid + " uid " + mCurClient.uid);
717 }
718 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
The Android Open Source Project10592532009-03-18 17:39:46 -0700720 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 private int getImeShowFlags() {
725 int flags = 0;
726 if (mShowForced) {
727 flags |= InputMethod.SHOW_FORCED
728 | InputMethod.SHOW_EXPLICIT;
729 } else if (mShowExplicitlyRequested) {
730 flags |= InputMethod.SHOW_EXPLICIT;
731 }
732 return flags;
733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 private int getAppShowFlags() {
736 int flags = 0;
737 if (mShowForced) {
738 flags |= InputMethodManager.SHOW_FORCED;
739 } else if (!mShowExplicitlyRequested) {
740 flags |= InputMethodManager.SHOW_IMPLICIT;
741 }
742 return flags;
743 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
746 if (!mBoundToMethod) {
747 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
748 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
749 mBoundToMethod = true;
750 }
751 final SessionState session = mCurClient.curSession;
752 if (initial) {
753 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
754 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
755 } else {
756 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
757 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
758 }
759 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800760 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800761 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 return needResult
764 ? new InputBindResult(session.session, mCurId, mCurSeq)
765 : null;
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 InputBindResult startInputLocked(IInputMethodClient client,
769 IInputContext inputContext, EditorInfo attribute,
770 boolean initial, boolean needResult) {
771 // If no method is currently selected, do nothing.
772 if (mCurMethodId == null) {
773 return mNoBinding;
774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 ClientState cs = mClients.get(client.asBinder());
777 if (cs == null) {
778 throw new IllegalArgumentException("unknown client "
779 + client.asBinder());
780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 try {
783 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
784 // Check with the window manager to make sure this client actually
785 // has a window with focus. If not, reject. This is thread safe
786 // because if the focus changes some time before or after, the
787 // next client receiving focus that has any interest in input will
788 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800789 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
791 return null;
792 }
793 } catch (RemoteException e) {
794 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 if (mCurClient != cs) {
797 // If the client is changing, we need to switch over to the new
798 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700799 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 + cs.client.asBinder());
802
803 // If the screen is on, inform the new client it is active
804 if (mScreenOn) {
805 try {
806 cs.client.setActive(mScreenOn);
807 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800808 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 + cs.pid + " uid " + cs.uid);
810 }
811 }
812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // Bump up the sequence for this client and attach it.
815 mCurSeq++;
816 if (mCurSeq <= 0) mCurSeq = 1;
817 mCurClient = cs;
818 mCurInputContext = inputContext;
819 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 // Check if the input method is changing.
822 if (mCurId != null && mCurId.equals(mCurMethodId)) {
823 if (cs.curSession != null) {
824 // Fast case: if we are already connected to the input method,
825 // then just return it.
826 return attachNewInputLocked(initial, needResult);
827 }
828 if (mHaveConnection) {
829 if (mCurMethod != null) {
830 if (!cs.sessionRequested) {
831 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800832 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
834 MSG_CREATE_SESSION, mCurMethod,
835 new MethodCallback(mCurMethod)));
836 }
837 // Return to client, and we will get back with it when
838 // we have had a session made for it.
839 return new InputBindResult(null, mCurId, mCurSeq);
840 } else if (SystemClock.uptimeMillis()
841 < (mLastBindTime+TIME_TO_RECONNECT)) {
842 // In this case we have connected to the service, but
843 // don't yet have its interface. If it hasn't been too
844 // long since we did the connection, we'll return to
845 // the client and wait to get the service interface so
846 // we can report back. If it has been too long, we want
847 // to fall through so we can try a disconnect/reconnect
848 // to see if we can get back in touch with the service.
849 return new InputBindResult(null, mCurId, mCurSeq);
850 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
852 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
854 }
855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700857 return startInputInnerLocked();
858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700860 InputBindResult startInputInnerLocked() {
861 if (mCurMethodId == null) {
862 return mNoBinding;
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 if (!mSystemReady) {
866 // If the system is not yet ready, we shouldn't be running third
867 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700868 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 InputMethodInfo info = mMethodMap.get(mCurMethodId);
872 if (info == null) {
873 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
874 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700876 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
879 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700880 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
881 com.android.internal.R.string.input_method_binding_label);
882 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
883 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
885 mLastBindTime = SystemClock.uptimeMillis();
886 mHaveConnection = true;
887 mCurId = info.getId();
888 mCurToken = new Binder();
889 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800890 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 mIWindowManager.addWindowToken(mCurToken,
892 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
893 } catch (RemoteException e) {
894 }
895 return new InputBindResult(null, mCurId, mCurSeq);
896 } else {
897 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 + mCurIntent);
900 }
901 return null;
902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
satoke7c6998e2011-06-03 17:57:59 +0900904 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public InputBindResult startInput(IInputMethodClient client,
906 IInputContext inputContext, EditorInfo attribute,
907 boolean initial, boolean needResult) {
908 synchronized (mMethodMap) {
909 final long ident = Binder.clearCallingIdentity();
910 try {
911 return startInputLocked(client, inputContext, attribute,
912 initial, needResult);
913 } finally {
914 Binder.restoreCallingIdentity(ident);
915 }
916 }
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
satoke7c6998e2011-06-03 17:57:59 +0900919 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void finishInput(IInputMethodClient client) {
921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
satoke7c6998e2011-06-03 17:57:59 +0900923 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 public void onServiceConnected(ComponentName name, IBinder service) {
925 synchronized (mMethodMap) {
926 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
927 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700928 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800929 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700930 unbindCurrentMethodLocked(false);
931 return;
932 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800933 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700934 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
935 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800937 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700938 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700940 MSG_CREATE_SESSION, mCurMethod,
941 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 }
943 }
944 }
945 }
946
947 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
948 synchronized (mMethodMap) {
949 if (mCurMethod != null && method != null
950 && mCurMethod.asBinder() == method.asBinder()) {
951 if (mCurClient != null) {
952 mCurClient.curSession = new SessionState(mCurClient,
953 method, session);
954 mCurClient.sessionRequested = false;
955 InputBindResult res = attachNewInputLocked(true, true);
956 if (res.method != null) {
957 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
958 MSG_BIND_METHOD, mCurClient.client, res));
959 }
960 }
961 }
962 }
963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800964
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700965 void unbindCurrentMethodLocked(boolean reportToClient) {
966 if (mHaveConnection) {
967 mContext.unbindService(this);
968 mHaveConnection = false;
969 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800970
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700971 if (mCurToken != null) {
972 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800973 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700974 mIWindowManager.removeWindowToken(mCurToken);
975 } catch (RemoteException e) {
976 }
977 mCurToken = null;
978 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Project10592532009-03-18 17:39:46 -0700980 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700981 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800982
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700983 if (reportToClient && mCurClient != null) {
984 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
985 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
986 }
987 }
988
Devin Taylor0c33ed22010-02-23 13:26:46 -0600989 private void finishSession(SessionState sessionState) {
990 if (sessionState != null && sessionState.session != null) {
991 try {
992 sessionState.session.finishSession();
993 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700994 Slog.w(TAG, "Session failed to close due to remote exception", e);
satokdbf29502011-08-25 15:28:23 +0900995 mImeWindowVis = 0;
996 updateImeWindowStatusLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -0600997 }
998 }
999 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001000
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001001 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 if (mCurMethod != null) {
1003 for (ClientState cs : mClients.values()) {
1004 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001005 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 cs.curSession = null;
1007 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001008
1009 finishSession(mEnabledSession);
1010 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 mCurMethod = null;
1012 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001013 if (mStatusBar != null) {
1014 mStatusBar.setIconVisibility("ime", false);
1015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001017
satoke7c6998e2011-06-03 17:57:59 +09001018 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 public void onServiceDisconnected(ComponentName name) {
1020 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001021 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 + " mCurIntent=" + mCurIntent);
1023 if (mCurMethod != null && mCurIntent != null
1024 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001025 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 // We consider this to be a new bind attempt, since the system
1027 // should now try to restart the service for us.
1028 mLastBindTime = SystemClock.uptimeMillis();
1029 mShowRequested = mInputShown;
1030 mInputShown = false;
1031 if (mCurClient != null) {
1032 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1033 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1034 }
1035 }
1036 }
1037 }
1038
satokf9f01002011-05-19 21:31:50 +09001039 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001041 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 long ident = Binder.clearCallingIdentity();
1043 try {
1044 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001045 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 return;
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 synchronized (mMethodMap) {
1050 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001052 if (mStatusBar != null) {
1053 mStatusBar.setIconVisibility("ime", false);
1054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001057 CharSequence contentDescription = null;
1058 try {
1059 PackageManager packageManager = mContext.getPackageManager();
1060 contentDescription = packageManager.getApplicationLabel(
1061 packageManager.getApplicationInfo(packageName, 0));
1062 } catch (NameNotFoundException nnfe) {
1063 /* ignore */
1064 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001065 if (mStatusBar != null) {
1066 mStatusBar.setIcon("ime", packageName, iconId, 0,
1067 contentDescription != null
1068 ? contentDescription.toString() : null);
1069 mStatusBar.setIconVisibility("ime", true);
1070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 }
1073 } finally {
1074 Binder.restoreCallingIdentity(ident);
1075 }
1076 }
1077
satok7cfc0ed2011-06-20 21:29:36 +09001078 private boolean needsToShowImeSwitchOngoingNotification() {
1079 if (!mShowOngoingImeSwitcherForPhones) return false;
1080 synchronized (mMethodMap) {
1081 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1082 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001083 if (N > 2) return true;
1084 if (N < 1) return false;
1085 int nonAuxCount = 0;
1086 int auxCount = 0;
1087 InputMethodSubtype nonAuxSubtype = null;
1088 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001089 for(int i = 0; i < N; ++i) {
1090 final InputMethodInfo imi = imis.get(i);
1091 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1092 imi, true);
1093 final int subtypeCount = subtypes.size();
1094 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001095 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001096 } else {
1097 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001098 final InputMethodSubtype subtype = subtypes.get(j);
1099 if (!subtype.isAuxiliary()) {
1100 ++nonAuxCount;
1101 nonAuxSubtype = subtype;
1102 } else {
1103 ++auxCount;
1104 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001105 }
1106 }
1107 }
satok7cfc0ed2011-06-20 21:29:36 +09001108 }
satokb6359412011-06-28 17:47:41 +09001109 if (nonAuxCount > 1 || auxCount > 1) {
1110 return true;
1111 } else if (nonAuxCount == 1 && auxCount == 1) {
1112 if (nonAuxSubtype != null && auxSubtype != null
1113 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1114 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1115 return false;
1116 }
1117 return true;
1118 }
1119 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001120 }
satok7cfc0ed2011-06-20 21:29:36 +09001121 }
1122
satokdbf29502011-08-25 15:28:23 +09001123 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001124 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001125 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001126 int uid = Binder.getCallingUid();
1127 long ident = Binder.clearCallingIdentity();
1128 try {
1129 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001130 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001131 return;
1132 }
1133
1134 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001135 mImeWindowVis = vis;
1136 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001137 if (mStatusBar != null) {
1138 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1139 }
satok7cfc0ed2011-06-20 21:29:36 +09001140 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001141 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1142 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001143 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001144 final CharSequence title = mRes.getText(
1145 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001146 final CharSequence imiLabel = imi.loadLabel(pm);
1147 final CharSequence summary = mCurrentSubtype != null
1148 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1149 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1150 (TextUtils.isEmpty(imiLabel) ?
1151 "" : " (" + imiLabel + ")"))
1152 : imiLabel;
1153
satok7cfc0ed2011-06-20 21:29:36 +09001154 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001155 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001156 if (mNotificationManager != null) {
1157 mNotificationManager.notify(
1158 com.android.internal.R.string.select_input_method,
1159 mImeSwitcherNotification);
1160 mNotificationShown = true;
1161 }
satok7cfc0ed2011-06-20 21:29:36 +09001162 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001163 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001164 mNotificationManager.cancel(
1165 com.android.internal.R.string.select_input_method);
1166 mNotificationShown = false;
1167 }
1168 }
satok06487a52010-10-29 11:37:18 +09001169 }
1170 } finally {
1171 Binder.restoreCallingIdentity(ident);
1172 }
1173 }
1174
satoke7c6998e2011-06-03 17:57:59 +09001175 @Override
satokf9f01002011-05-19 21:31:50 +09001176 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1177 synchronized (mMethodMap) {
1178 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1179 for (int i = 0; i < spans.length; ++i) {
1180 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001181 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001182 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001183 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001184 }
1185 }
1186 }
1187 }
1188
satoke7c6998e2011-06-03 17:57:59 +09001189 @Override
satokf9f01002011-05-19 21:31:50 +09001190 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1191 synchronized (mMethodMap) {
1192 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1193 // TODO: Do not send the intent if the process of the targetImi is already dead.
1194 if (targetImi != null) {
1195 final String[] suggestions = span.getSuggestions();
1196 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001197 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001198 final Intent intent = new Intent();
1199 // Ensures that only a class in the original IME package will receive the
1200 // notification.
satok42c5a162011-05-26 16:46:14 +09001201 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001202 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1203 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1204 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1205 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1206 mContext.sendBroadcast(intent);
1207 return true;
1208 }
1209 }
1210 return false;
1211 }
1212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001214 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1215 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1216 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1217 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001219 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001220 // There is no input method selected, try to choose new applicable input method.
1221 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1222 id = Settings.Secure.getString(mContext.getContentResolver(),
1223 Settings.Secure.DEFAULT_INPUT_METHOD);
1224 }
1225 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 try {
satokab751aa2010-09-14 19:17:36 +09001227 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001229 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001230 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001231 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 }
satokf3db1af2010-11-23 13:34:33 +09001233 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001234 } else {
1235 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001236 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001237 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 }
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
satokab751aa2010-09-14 19:17:36 +09001241 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 InputMethodInfo info = mMethodMap.get(id);
1243 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001244 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001248 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001249 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1250 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001251 }
1252 if (subtype != mCurrentSubtype) {
1253 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001254 if (subtype != null) {
1255 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1256 }
satokcd7cd292010-11-20 15:46:23 +09001257 if (mCurMethod != null) {
1258 try {
satoke40dea02011-01-30 01:14:02 +09001259 final Configuration conf = mRes.getConfiguration();
1260 final boolean haveHardKeyboard = conf.keyboard
1261 != Configuration.KEYBOARD_NOKEYS;
1262 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001263 && conf.hardKeyboardHidden
1264 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001265 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1266 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1267 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001268 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001269 // If subtype is null, try to find the most applicable one from
1270 // getCurrentInputMethodSubtype.
1271 if (subtype == null) {
1272 subtype = getCurrentInputMethodSubtype();
1273 }
1274 mCurMethod.changeInputMethodSubtype(subtype);
1275 } catch (RemoteException e) {
1276 return;
satokab751aa2010-09-14 19:17:36 +09001277 }
1278 }
1279 }
1280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 return;
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 final long ident = Binder.clearCallingIdentity();
1285 try {
satokab751aa2010-09-14 19:17:36 +09001286 // Set a subtype to this input method.
1287 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001288 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1289 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1290 // because mCurMethodId is stored as a history in
1291 // setSelectedInputMethodAndSubtypeLocked().
1292 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293
1294 if (ActivityManagerNative.isSystemReady()) {
1295 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001296 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 intent.putExtra("input_method_id", id);
1298 mContext.sendBroadcast(intent);
1299 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001300 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 } finally {
1302 Binder.restoreCallingIdentity(ident);
1303 }
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
satok42c5a162011-05-26 16:46:14 +09001306 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001307 public boolean showSoftInput(IInputMethodClient client, int flags,
1308 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001309 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 long ident = Binder.clearCallingIdentity();
1311 try {
1312 synchronized (mMethodMap) {
1313 if (mCurClient == null || client == null
1314 || mCurClient.client.asBinder() != client.asBinder()) {
1315 try {
1316 // We need to check if this is the current client with
1317 // focus in the window manager, to allow this call to
1318 // be made before input is started in it.
1319 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001320 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001321 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001327
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001329 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 }
1331 } finally {
1332 Binder.restoreCallingIdentity(ident);
1333 }
1334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 mShowRequested = true;
1338 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1339 mShowExplicitlyRequested = true;
1340 }
1341 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1342 mShowExplicitlyRequested = true;
1343 mShowForced = true;
1344 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001345
Dianne Hackborncc278702009-09-02 23:07:23 -07001346 if (!mSystemReady) {
1347 return false;
1348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
The Android Open Source Project4df24232009-03-05 14:34:35 -08001350 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001352 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1353 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1354 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001356 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 } else if (mHaveConnection && SystemClock.uptimeMillis()
1358 < (mLastBindTime+TIME_TO_RECONNECT)) {
1359 // The client has asked to have the input method shown, but
1360 // we have been sitting here too long with a connection to the
1361 // service and no interface received, so let's disconnect/connect
1362 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001363 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 SystemClock.uptimeMillis()-mLastBindTime,1);
1365 mContext.unbindService(this);
1366 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
The Android Open Source Project4df24232009-03-05 14:34:35 -08001369 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
satok42c5a162011-05-26 16:46:14 +09001372 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001373 public boolean hideSoftInput(IInputMethodClient client, int flags,
1374 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001375 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 long ident = Binder.clearCallingIdentity();
1377 try {
1378 synchronized (mMethodMap) {
1379 if (mCurClient == null || client == null
1380 || mCurClient.client.asBinder() != client.asBinder()) {
1381 try {
1382 // We need to check if this is the current client with
1383 // focus in the window manager, to allow this call to
1384 // be made before input is started in it.
1385 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001386 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1387 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001388 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001389 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001390 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001393 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001394 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001395 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
Joe Onorato8a9b2202010-02-26 18:56:32 -08001399 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 } finally {
1403 Binder.restoreCallingIdentity(ident);
1404 }
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
The Android Open Source Project4df24232009-03-05 14:34:35 -08001407 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1409 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001410 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001412 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001415 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001417 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001419 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001421 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1422 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1423 res = true;
1424 } else {
1425 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 mInputShown = false;
1428 mShowRequested = false;
1429 mShowExplicitlyRequested = false;
1430 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001431 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
satok42c5a162011-05-26 16:46:14 +09001434 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001435 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1437 boolean first, int windowFlags) {
1438 long ident = Binder.clearCallingIdentity();
1439 try {
1440 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001441 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 + " viewHasFocus=" + viewHasFocus
1443 + " isTextEditor=" + isTextEditor
1444 + " softInputMode=#" + Integer.toHexString(softInputMode)
1445 + " first=" + first + " flags=#"
1446 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 if (mCurClient == null || client == null
1449 || mCurClient.client.asBinder() != client.asBinder()) {
1450 try {
1451 // We need to check if this is the current client with
1452 // focus in the window manager, to allow this call to
1453 // be made before input is started in it.
1454 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 return;
1457 }
1458 } catch (RemoteException e) {
1459 }
1460 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001461
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001462 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001463 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001464 return;
1465 }
1466 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001467
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001468 // Should we auto-show the IME even if the caller has not
1469 // specified what should be done with it?
1470 // We only do this automatically if the window can resize
1471 // to accommodate the IME (so what the user sees will give
1472 // them good context without input information being obscured
1473 // by the IME) or if running on a large screen where there
1474 // is more room for the target window + IME.
1475 final boolean doAutoShow =
1476 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1477 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1478 || mRes.getConfiguration().isLayoutSizeAtLeast(
1479 Configuration.SCREENLAYOUT_SIZE_LARGE);
1480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1482 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001483 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1485 // There is no focus view, and this window will
1486 // be behind any soft input window, so hide the
1487 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001489 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001491 } else if (isTextEditor && doAutoShow && (softInputMode &
1492 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 // There is a focus view, and we are navigating forward
1494 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001495 // We only do this automatically if the window an resize
1496 // to accomodate the IME (so what the user sees will give
1497 // them good context without input information being obscured
1498 // by the IME) or if running on a large screen where there
1499 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001501 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
1503 break;
1504 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1505 // Do nothing.
1506 break;
1507 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1508 if ((softInputMode &
1509 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001511 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513 break;
1514 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001515 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001516 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 break;
1518 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1519 if ((softInputMode &
1520 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001522 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524 break;
1525 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001526 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001527 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 break;
1529 }
1530 }
1531 } finally {
1532 Binder.restoreCallingIdentity(ident);
1533 }
1534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
satok42c5a162011-05-26 16:46:14 +09001536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1538 synchronized (mMethodMap) {
1539 if (mCurClient == null || client == null
1540 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001541 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001542 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 }
1544
satok440aab52010-11-25 09:43:11 +09001545 // Always call subtype picker, because subtype picker is a superset of input method
1546 // picker.
satokab751aa2010-09-14 19:17:36 +09001547 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1548 }
1549 }
1550
satok42c5a162011-05-26 16:46:14 +09001551 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001553 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1554 }
1555
satok42c5a162011-05-26 16:46:14 +09001556 @Override
satok28203512010-11-24 11:06:49 +09001557 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1558 synchronized (mMethodMap) {
1559 if (subtype != null) {
1560 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1561 mMethodMap.get(id), subtype.hashCode()));
1562 } else {
1563 setInputMethod(token, id);
1564 }
1565 }
satokab751aa2010-09-14 19:17:36 +09001566 }
1567
satok42c5a162011-05-26 16:46:14 +09001568 @Override
satokb416a712010-11-25 20:42:14 +09001569 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001570 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001571 synchronized (mMethodMap) {
1572 if (mCurClient == null || client == null
1573 || mCurClient.client.asBinder() != client.asBinder()) {
1574 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1575 }
satok7fee71f2010-12-17 18:54:26 +09001576 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1577 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001578 }
1579 }
1580
satok4fc87d62011-05-20 16:13:43 +09001581 @Override
satok735cf382010-11-11 20:40:09 +09001582 public boolean switchToLastInputMethod(IBinder token) {
1583 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001584 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001585 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001586 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001587 lastImi = mMethodMap.get(lastIme.first);
1588 } else {
1589 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001590 }
satok4fc87d62011-05-20 16:13:43 +09001591 String targetLastImiId = null;
1592 int subtypeId = NOT_A_SUBTYPE_ID;
1593 if (lastIme != null && lastImi != null) {
1594 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1595 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1596 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1597 : mCurrentSubtype.hashCode();
1598 // If the last IME is the same as the current IME and the last subtype is not
1599 // defined, there is no need to switch to the last IME.
1600 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1601 targetLastImiId = lastIme.first;
1602 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1603 }
1604 }
1605
1606 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1607 // This is a safety net. If the currentSubtype can't be added to the history
1608 // and the framework couldn't find the last ime, we will make the last ime be
1609 // the most applicable enabled keyboard subtype of the system imes.
1610 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1611 if (enabled != null) {
1612 final int N = enabled.size();
1613 final String locale = mCurrentSubtype == null
1614 ? mRes.getConfiguration().locale.toString()
1615 : mCurrentSubtype.getLocale();
1616 for (int i = 0; i < N; ++i) {
1617 final InputMethodInfo imi = enabled.get(i);
1618 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1619 InputMethodSubtype keyboardSubtype =
1620 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1621 SUBTYPE_MODE_KEYBOARD, locale, true);
1622 if (keyboardSubtype != null) {
1623 targetLastImiId = imi.getId();
1624 subtypeId = getSubtypeIdFromHashCode(
1625 imi, keyboardSubtype.hashCode());
1626 if(keyboardSubtype.getLocale().equals(locale)) {
1627 break;
1628 }
1629 }
1630 }
1631 }
1632 }
1633 }
1634
1635 if (!TextUtils.isEmpty(targetLastImiId)) {
1636 if (DEBUG) {
1637 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1638 + ", from: " + mCurMethodId + ", " + subtypeId);
1639 }
1640 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1641 return true;
1642 } else {
1643 return false;
1644 }
satok735cf382010-11-11 20:40:09 +09001645 }
1646 }
1647
satoke7c6998e2011-06-03 17:57:59 +09001648 @Override
satok68f1b782011-04-11 14:26:04 +09001649 public InputMethodSubtype getLastInputMethodSubtype() {
1650 synchronized (mMethodMap) {
1651 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1652 // TODO: Handle the case of the last IME with no subtypes
1653 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1654 || TextUtils.isEmpty(lastIme.second)) return null;
1655 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1656 if (lastImi == null) return null;
1657 try {
1658 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001659 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1660 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1661 return null;
1662 }
1663 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001664 } catch (NumberFormatException e) {
1665 return null;
1666 }
1667 }
1668 }
1669
satoke7c6998e2011-06-03 17:57:59 +09001670 @Override
satok91e88122011-07-18 11:11:42 +09001671 public boolean setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
1672 // By this IPC call, only a process which shares the same uid with the IME can add
1673 // additional input method subtypes to the IME.
1674 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return false;
satoke7c6998e2011-06-03 17:57:59 +09001675 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001676 final InputMethodInfo imi = mMethodMap.get(imiId);
satoke7c6998e2011-06-03 17:57:59 +09001677 if (imi == null) return false;
satok91e88122011-07-18 11:11:42 +09001678 final PackageManager pm = mContext.getPackageManager();
1679 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1680 if (packageInfos != null) {
1681 final int packageNum = packageInfos.length;
1682 for (int i = 0; i < packageNum; ++i) {
1683 if (packageInfos[i].equals(imi.getPackageName())) {
1684 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001685 final long ident = Binder.clearCallingIdentity();
1686 try {
1687 buildInputMethodListLocked(mMethodList, mMethodMap);
1688 } finally {
1689 Binder.restoreCallingIdentity(ident);
1690 }
satok91e88122011-07-18 11:11:42 +09001691 return true;
1692 }
1693 }
1694 }
satoke7c6998e2011-06-03 17:57:59 +09001695 }
satok91e88122011-07-18 11:11:42 +09001696 return false;
satoke7c6998e2011-06-03 17:57:59 +09001697 }
1698
satok28203512010-11-24 11:06:49 +09001699 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 synchronized (mMethodMap) {
1701 if (token == null) {
1702 if (mContext.checkCallingOrSelfPermission(
1703 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1704 != PackageManager.PERMISSION_GRANTED) {
1705 throw new SecurityException(
1706 "Using null token requires permission "
1707 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1708 }
1709 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001710 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1711 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 return;
1713 }
1714
satokc5933802011-08-31 21:26:04 +09001715 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 try {
satokab751aa2010-09-14 19:17:36 +09001717 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 } finally {
1719 Binder.restoreCallingIdentity(ident);
1720 }
1721 }
1722 }
1723
satok42c5a162011-05-26 16:46:14 +09001724 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 public void hideMySoftInput(IBinder token, int flags) {
1726 synchronized (mMethodMap) {
1727 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001728 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1729 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 return;
1731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 long ident = Binder.clearCallingIdentity();
1733 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001734 hideCurrentInputLocked(flags, null);
1735 } finally {
1736 Binder.restoreCallingIdentity(ident);
1737 }
1738 }
1739 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
satok42c5a162011-05-26 16:46:14 +09001741 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 public void showMySoftInput(IBinder token, int flags) {
1743 synchronized (mMethodMap) {
1744 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001745 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1746 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001747 return;
1748 }
1749 long ident = Binder.clearCallingIdentity();
1750 try {
1751 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 } finally {
1753 Binder.restoreCallingIdentity(ident);
1754 }
1755 }
1756 }
1757
1758 void setEnabledSessionInMainThread(SessionState session) {
1759 if (mEnabledSession != session) {
1760 if (mEnabledSession != null) {
1761 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001762 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 mEnabledSession.method.setSessionEnabled(
1764 mEnabledSession.session, false);
1765 } catch (RemoteException e) {
1766 }
1767 }
1768 mEnabledSession = session;
1769 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001770 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 session.method.setSessionEnabled(
1772 session.session, true);
1773 } catch (RemoteException e) {
1774 }
1775 }
1776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001777
satok42c5a162011-05-26 16:46:14 +09001778 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 public boolean handleMessage(Message msg) {
1780 HandlerCaller.SomeArgs args;
1781 switch (msg.what) {
1782 case MSG_SHOW_IM_PICKER:
1783 showInputMethodMenu();
1784 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785
satokab751aa2010-09-14 19:17:36 +09001786 case MSG_SHOW_IM_SUBTYPE_PICKER:
1787 showInputMethodSubtypeMenu();
1788 return true;
1789
satok47a44912010-10-06 16:03:58 +09001790 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001791 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001792 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001793 return true;
1794
1795 case MSG_SHOW_IM_CONFIG:
1796 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001797 return true;
1798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 case MSG_UNBIND_INPUT:
1802 try {
1803 ((IInputMethod)msg.obj).unbindInput();
1804 } catch (RemoteException e) {
1805 // There is nothing interesting about the method dying.
1806 }
1807 return true;
1808 case MSG_BIND_INPUT:
1809 args = (HandlerCaller.SomeArgs)msg.obj;
1810 try {
1811 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1812 } catch (RemoteException e) {
1813 }
1814 return true;
1815 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001816 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001818 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1819 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 } catch (RemoteException e) {
1821 }
1822 return true;
1823 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001824 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001826 ((IInputMethod)args.arg1).hideSoftInput(0,
1827 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 } catch (RemoteException e) {
1829 }
1830 return true;
1831 case MSG_ATTACH_TOKEN:
1832 args = (HandlerCaller.SomeArgs)msg.obj;
1833 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001834 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1836 } catch (RemoteException e) {
1837 }
1838 return true;
1839 case MSG_CREATE_SESSION:
1840 args = (HandlerCaller.SomeArgs)msg.obj;
1841 try {
1842 ((IInputMethod)args.arg1).createSession(
1843 (IInputMethodCallback)args.arg2);
1844 } catch (RemoteException e) {
1845 }
1846 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 case MSG_START_INPUT:
1850 args = (HandlerCaller.SomeArgs)msg.obj;
1851 try {
1852 SessionState session = (SessionState)args.arg1;
1853 setEnabledSessionInMainThread(session);
1854 session.method.startInput((IInputContext)args.arg2,
1855 (EditorInfo)args.arg3);
1856 } catch (RemoteException e) {
1857 }
1858 return true;
1859 case MSG_RESTART_INPUT:
1860 args = (HandlerCaller.SomeArgs)msg.obj;
1861 try {
1862 SessionState session = (SessionState)args.arg1;
1863 setEnabledSessionInMainThread(session);
1864 session.method.restartInput((IInputContext)args.arg2,
1865 (EditorInfo)args.arg3);
1866 } catch (RemoteException e) {
1867 }
1868 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 case MSG_UNBIND_METHOD:
1873 try {
1874 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1875 } catch (RemoteException e) {
1876 // There is nothing interesting about the last client dying.
1877 }
1878 return true;
1879 case MSG_BIND_METHOD:
1880 args = (HandlerCaller.SomeArgs)msg.obj;
1881 try {
1882 ((IInputMethodClient)args.arg1).onBindMethod(
1883 (InputBindResult)args.arg2);
1884 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001885 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887 return true;
1888 }
1889 return false;
1890 }
1891
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001892 private boolean isSystemIme(InputMethodInfo inputMethod) {
1893 return (inputMethod.getServiceInfo().applicationInfo.flags
1894 & ApplicationInfo.FLAG_SYSTEM) != 0;
1895 }
1896
Ken Wakasa586f0512011-01-20 22:31:01 +09001897 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1898 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1899 final int subtypeCount = imi.getSubtypeCount();
1900 for (int i = 0; i < subtypeCount; ++i) {
1901 subtypes.add(imi.getSubtypeAt(i));
1902 }
1903 return subtypes;
1904 }
1905
satoka86f5e42011-09-02 17:12:42 +09001906
1907 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1908 InputMethodInfo imi, String mode) {
1909 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1910 final int subtypeCount = imi.getSubtypeCount();
1911 for (int i = 0; i < subtypeCount; ++i) {
1912 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1913 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1914 subtypes.add(subtype);
1915 }
1916 }
1917 return subtypes;
1918 }
1919
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001920 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001921 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001922 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001923 // We'd prefer to fall back on a system IME, since that is safer.
1924 int i=enabled.size();
1925 while (i > 0) {
1926 i--;
1927 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1928 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1929 break;
1930 }
1931 }
satokab751aa2010-09-14 19:17:36 +09001932 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001933 if (DEBUG) {
1934 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1935 }
satok723a27e2010-11-11 14:58:11 +09001936 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001937 return true;
1938 }
1939
1940 return false;
1941 }
1942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1944 HashMap<String, InputMethodInfo> map) {
1945 list.clear();
1946 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001949 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001950 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1951 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1952 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1953 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954
1955 List<ResolveInfo> services = pm.queryIntentServices(
1956 new Intent(InputMethod.SERVICE_INTERFACE),
1957 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001958
satoke7c6998e2011-06-03 17:57:59 +09001959 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1960 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 for (int i = 0; i < services.size(); ++i) {
1962 ResolveInfo ri = services.get(i);
1963 ServiceInfo si = ri.serviceInfo;
1964 ComponentName compName = new ComponentName(si.packageName, si.name);
1965 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1966 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001967 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 + ": it does not require the permission "
1969 + android.Manifest.permission.BIND_INPUT_METHOD);
1970 continue;
1971 }
1972
Joe Onorato8a9b2202010-02-26 18:56:32 -08001973 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974
1975 try {
satoke7c6998e2011-06-03 17:57:59 +09001976 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001978 final String id = p.getId();
1979 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980
Amith Yamasanie861ec12010-03-24 21:39:27 -07001981 // System IMEs are enabled by default, unless there's a hard keyboard
1982 // and the system IME was explicitly disabled
1983 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1984 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001985 }
1986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001992 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001994 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 }
1996 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001997
1998 String defaultIme = Settings.Secure.getString(mContext
1999 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002000 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002001 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002002 updateFromSettingsLocked();
2003 }
2004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
satokab751aa2010-09-14 19:17:36 +09002009 private void showInputMethodMenu() {
2010 showInputMethodMenuInternal(false);
2011 }
2012
2013 private void showInputMethodSubtypeMenu() {
2014 showInputMethodMenuInternal(true);
2015 }
2016
satok217f5482010-12-15 05:19:19 +09002017 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002018 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002019 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002020 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2021 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002022 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002023 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002024 }
satok217f5482010-12-15 05:19:19 +09002025 mContext.startActivity(intent);
2026 }
2027
2028 private void showConfigureInputMethods() {
2029 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2030 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2031 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2032 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002033 mContext.startActivity(intent);
2034 }
2035
satokab751aa2010-09-14 19:17:36 +09002036 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002037 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002041 final boolean isScreenLocked = mKeyguardManager != null
2042 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 String lastInputMethodId = Settings.Secure.getString(context
2045 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002046 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002047 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002048
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002049 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002050 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2051 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002052 if (immis == null || immis.size() == 0) {
2053 return;
2054 }
2055
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002056 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057
Ken Wakasa761eb372011-03-04 19:06:18 +09002058 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2059 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2060 new Comparator<InputMethodInfo>() {
2061 @Override
2062 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2063 if (imi2 == null) return 0;
2064 if (imi1 == null) return 1;
2065 if (pm == null) {
2066 return imi1.getId().compareTo(imi2.getId());
2067 }
2068 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2069 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2070 return imiId1.toString().compareTo(imiId2.toString());
2071 }
2072 });
satok913a8922010-08-26 21:53:41 +09002073
Ken Wakasa761eb372011-03-04 19:06:18 +09002074 sortedImmis.putAll(immis);
2075
2076 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2077 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2078
2079 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002080 if (imi == null) continue;
2081 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002082 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002083 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2084 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002085 }
satokbb4aa062011-01-19 21:40:27 +09002086 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002087 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002088 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002089 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002090 if (DEBUG) {
2091 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2092 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002093 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002094 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2095 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2096 // We show all enabled IMEs and subtypes when an IME is shown.
2097 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002098 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002099 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002100 final String mode = subtype.getMode();
2101 title = TextUtils.concat(subtype.getDisplayName(context,
2102 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2103 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002104 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2105 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002106 // Removing this subtype from enabledSubtypeSet because we no longer
2107 // need to add an entry of this subtype to imList to avoid duplicated
2108 // entries.
2109 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002110 }
satokab751aa2010-09-14 19:17:36 +09002111 }
2112 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002113 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2114 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002115 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002116 }
satok913a8922010-08-26 21:53:41 +09002117
Ken Wakasa761eb372011-03-04 19:06:18 +09002118 final int N = imList.size();
2119 mItems = new CharSequence[N];
2120 for (int i = 0; i < N; ++i) {
2121 mItems[i] = imList.get(i).first;
2122 }
satokab751aa2010-09-14 19:17:36 +09002123 mIms = new InputMethodInfo[N];
2124 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002125 int checkedItem = 0;
2126 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002127 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002128 mIms[i] = value.first;
2129 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002130 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002131 int subtypeId = mSubtypeIds[i];
2132 if ((subtypeId == NOT_A_SUBTYPE_ID)
2133 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2134 || (subtypeId == lastInputMethodSubtypeId)) {
2135 checkedItem = i;
2136 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
satokab751aa2010-09-14 19:17:36 +09002139
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002140 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002141 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002142 public void onClick(DialogInterface dialog, int which) {
2143 hideInputMethodMenu();
2144 }
2145 };
satokd87c2592010-09-29 11:52:06 +09002146
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002147 TypedArray a = context.obtainStyledAttributes(null,
2148 com.android.internal.R.styleable.DialogPreference,
2149 com.android.internal.R.attr.alertDialogStyle, 0);
2150 mDialogBuilder = new AlertDialog.Builder(context)
2151 .setTitle(com.android.internal.R.string.select_input_method)
2152 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002153 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002154 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002156 }
2157 })
2158 .setIcon(a.getDrawable(
2159 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2160 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002161
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002162 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2163 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002164 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002165 public void onClick(DialogInterface dialog, int which) {
2166 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002167 if (mIms == null || mIms.length <= which
2168 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002169 return;
2170 }
2171 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002172 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002173 hideInputMethodMenu();
2174 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002175 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002176 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002177 subtypeId = NOT_A_SUBTYPE_ID;
2178 }
2179 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002180 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002183 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184
satokbc81b692011-08-26 16:22:22 +09002185 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002186 mDialogBuilder.setPositiveButton(
2187 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002188 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002189 @Override
satok7f35c8c2010-10-07 21:13:11 +09002190 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002191 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002192 }
2193 });
2194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002196 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 mSwitchingDialog.getWindow().setType(
2198 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002199 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 mSwitchingDialog.show();
2201 }
2202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002205 synchronized (mMethodMap) {
2206 hideInputMethodMenuLocked();
2207 }
2208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002209
The Android Open Source Project10592532009-03-18 17:39:46 -07002210 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002211 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212
The Android Open Source Project10592532009-03-18 17:39:46 -07002213 if (mSwitchingDialog != null) {
2214 mSwitchingDialog.dismiss();
2215 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
The Android Open Source Project10592532009-03-18 17:39:46 -07002218 mDialogBuilder = null;
2219 mItems = null;
2220 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002224
satok42c5a162011-05-26 16:46:14 +09002225 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 public boolean setInputMethodEnabled(String id, boolean enabled) {
2227 synchronized (mMethodMap) {
2228 if (mContext.checkCallingOrSelfPermission(
2229 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2230 != PackageManager.PERMISSION_GRANTED) {
2231 throw new SecurityException(
2232 "Requires permission "
2233 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2234 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 long ident = Binder.clearCallingIdentity();
2237 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002238 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 } finally {
2240 Binder.restoreCallingIdentity(ident);
2241 }
2242 }
2243 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002244
2245 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2246 // Make sure this is a valid input method.
2247 InputMethodInfo imm = mMethodMap.get(id);
2248 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002249 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002250 }
2251
satokd87c2592010-09-29 11:52:06 +09002252 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2253 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002254
satokd87c2592010-09-29 11:52:06 +09002255 if (enabled) {
2256 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2257 if (pair.first.equals(id)) {
2258 // We are enabling this input method, but it is already enabled.
2259 // Nothing to do. The previous state was enabled.
2260 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002261 }
2262 }
satokd87c2592010-09-29 11:52:06 +09002263 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2264 // Previous state was disabled.
2265 return false;
2266 } else {
2267 StringBuilder builder = new StringBuilder();
2268 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2269 builder, enabledInputMethodsList, id)) {
2270 // Disabled input method is currently selected, switch to another one.
2271 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2272 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002273 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2274 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2275 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002276 }
2277 // Previous state was enabled.
2278 return true;
2279 } else {
2280 // We are disabling the input method but it is already disabled.
2281 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002282 return false;
2283 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002284 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002285 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002286
satok57ffc002011-01-25 00:11:47 +09002287 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2288 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002289 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002290 }
2291
satok723a27e2010-11-11 14:58:11 +09002292 private void saveCurrentInputMethodAndSubtypeToHistory() {
2293 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2294 if (mCurrentSubtype != null) {
2295 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2296 }
satok57ffc002011-01-25 00:11:47 +09002297 if (canAddToLastInputMethod(mCurrentSubtype)) {
2298 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2299 }
satokab751aa2010-09-14 19:17:36 +09002300 }
2301
satok723a27e2010-11-11 14:58:11 +09002302 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2303 boolean setSubtypeOnly) {
2304 // Update the history of InputMethod and Subtype
2305 saveCurrentInputMethodAndSubtypeToHistory();
2306
2307 // Set Subtype here
2308 if (imi == null || subtypeId < 0) {
2309 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002310 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002311 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002312 if (subtypeId < imi.getSubtypeCount()) {
2313 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2314 mSettings.putSelectedSubtype(subtype.hashCode());
2315 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002316 } else {
2317 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2318 mCurrentSubtype = null;
2319 }
satokab751aa2010-09-14 19:17:36 +09002320 }
satok723a27e2010-11-11 14:58:11 +09002321
2322 if (!setSubtypeOnly) {
2323 // Set InputMethod here
2324 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2325 }
2326 }
2327
2328 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2329 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2330 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2331 // newDefaultIme is empty when there is no candidate for the selected IME.
2332 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2333 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2334 if (subtypeHashCode != null) {
2335 try {
2336 lastSubtypeId = getSubtypeIdFromHashCode(
2337 imi, Integer.valueOf(subtypeHashCode));
2338 } catch (NumberFormatException e) {
2339 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2340 }
2341 }
2342 }
2343 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002344 }
2345
2346 private int getSelectedInputMethodSubtypeId(String id) {
2347 InputMethodInfo imi = mMethodMap.get(id);
2348 if (imi == null) {
2349 return NOT_A_SUBTYPE_ID;
2350 }
satokab751aa2010-09-14 19:17:36 +09002351 int subtypeId;
2352 try {
2353 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2354 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2355 } catch (SettingNotFoundException e) {
2356 return NOT_A_SUBTYPE_ID;
2357 }
satok723a27e2010-11-11 14:58:11 +09002358 return getSubtypeIdFromHashCode(imi, subtypeId);
2359 }
2360
2361 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002362 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002363 final int subtypeCount = imi.getSubtypeCount();
2364 for (int i = 0; i < subtypeCount; ++i) {
2365 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002366 if (subtypeHashCode == ims.hashCode()) {
2367 return i;
2368 }
satokab751aa2010-09-14 19:17:36 +09002369 }
2370 }
2371 return NOT_A_SUBTYPE_ID;
2372 }
2373
satoka86f5e42011-09-02 17:12:42 +09002374 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2375 Resources res, InputMethodInfo imi) {
2376 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002377 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002378 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2379 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2380 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002381 final int N = subtypes.size();
2382 boolean containsKeyboardSubtype = false;
2383 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002384 // scan overriding implicitly enabled subtypes.
2385 InputMethodSubtype subtype = subtypes.get(i);
2386 if (subtype.overridesImplicitlyEnabledSubtype()) {
2387 final String mode = subtype.getMode();
2388 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2389 applicableModeAndSubtypesMap.put(mode, subtype);
2390 }
2391 }
2392 }
2393 if (applicableModeAndSubtypesMap.size() > 0) {
2394 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2395 }
2396 for (int i = 0; i < N; ++i) {
satok16331c82010-12-20 23:48:46 +09002397 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002398 final String locale = subtype.getLocale();
2399 final String mode = subtype.getMode();
2400 // When system locale starts with subtype's locale, that subtype will be applicable
2401 // for system locale
2402 // For instance, it's clearly applicable for cases like system locale = en_US and
2403 // subtype = en, but it is not necessarily considered applicable for cases like system
2404 // locale = en and subtype = en_US.
2405 // We just call systemLocale.startsWith(locale) in this function because there is no
2406 // need to find applicable subtypes aggressively unlike
2407 // findLastResortApplicableSubtypeLocked.
2408 if (systemLocale.startsWith(locale)) {
2409 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2410 // If more applicable subtypes are contained, skip.
2411 if (applicableSubtype != null
2412 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2413 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002414 if (!containsKeyboardSubtype
2415 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2416 containsKeyboardSubtype = true;
2417 }
2418 }
2419 }
satok4a28bde2011-06-29 21:03:40 +09002420 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002421 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002422 if (!containsKeyboardSubtype) {
2423 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002424 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002425 if (lastResortKeyboardSubtype != null) {
2426 applicableSubtypes.add(lastResortKeyboardSubtype);
2427 }
2428 }
2429 return applicableSubtypes;
2430 }
2431
satok4e4569d2010-11-19 18:45:53 +09002432 /**
2433 * If there are no selected subtypes, tries finding the most applicable one according to the
2434 * given locale.
2435 * @param subtypes this function will search the most applicable subtype in subtypes
2436 * @param mode subtypes will be filtered by mode
2437 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002438 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2439 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002440 * @return the most applicable subtypeId
2441 */
satokdf31ae62011-01-15 06:19:44 +09002442 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2443 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002444 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002445 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002446 return null;
satok8fbb1e82010-11-02 23:15:58 +09002447 }
satok4e4569d2010-11-19 18:45:53 +09002448 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002449 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002450 }
satok8fbb1e82010-11-02 23:15:58 +09002451 final String language = locale.substring(0, 2);
2452 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002453 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002454 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002455 final int N = subtypes.size();
2456 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002457 InputMethodSubtype subtype = subtypes.get(i);
2458 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002459 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2460 // and all subtypes with all modes can be candidates.
2461 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002462 if (firstMatchedModeSubtype == null) {
2463 firstMatchedModeSubtype = subtype;
2464 }
satok9ef02832010-11-04 21:17:48 +09002465 if (locale.equals(subtypeLocale)) {
2466 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002467 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002468 break;
2469 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2470 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002471 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002472 partialMatchFound = true;
2473 }
satok8fbb1e82010-11-02 23:15:58 +09002474 }
2475 }
2476
satok7599a7f2010-12-22 13:45:23 +09002477 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2478 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002479 }
2480
satok8fbb1e82010-11-02 23:15:58 +09002481 // The first subtype applicable to the system locale will be defined as the most applicable
2482 // subtype.
2483 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002484 if (applicableSubtype != null) {
2485 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2486 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2487 }
satok8fbb1e82010-11-02 23:15:58 +09002488 }
satokcd7cd292010-11-20 15:46:23 +09002489 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002490 }
2491
satok4e4569d2010-11-19 18:45:53 +09002492 // If there are no selected shortcuts, tries finding the most applicable ones.
2493 private Pair<InputMethodInfo, InputMethodSubtype>
2494 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2495 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2496 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002497 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002498 boolean foundInSystemIME = false;
2499
2500 // Search applicable subtype for each InputMethodInfo
2501 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002502 final String imiId = imi.getId();
2503 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2504 continue;
2505 }
satokcd7cd292010-11-20 15:46:23 +09002506 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002507 final List<InputMethodSubtype> enabledSubtypes =
2508 getEnabledInputMethodSubtypeList(imi, true);
2509 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002510 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002511 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002512 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002513 }
satokdf31ae62011-01-15 06:19:44 +09002514 // 2. Search by the system locale from enabledSubtypes.
2515 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002516 if (subtype == null) {
2517 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002518 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002519 }
satoka86f5e42011-09-02 17:12:42 +09002520 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2521 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2522 final ArrayList<InputMethodSubtype> subtypesForSearch =
2523 overridingImplicitlyEnabledSubtypes.isEmpty()
2524 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002525 // 4. Search by the current subtype's locale from all subtypes.
2526 if (subtype == null && mCurrentSubtype != null) {
2527 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002528 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002529 }
2530 // 5. Search by the system locale from all subtypes.
2531 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002532 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002533 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002534 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002535 }
satokcd7cd292010-11-20 15:46:23 +09002536 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002537 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002538 // The current input method is the most applicable IME.
2539 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002540 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002541 break;
satok7599a7f2010-12-22 13:45:23 +09002542 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002543 // The system input method is 2nd applicable IME.
2544 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002545 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002546 if ((imi.getServiceInfo().applicationInfo.flags
2547 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2548 foundInSystemIME = true;
2549 }
satok4e4569d2010-11-19 18:45:53 +09002550 }
2551 }
2552 }
2553 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002554 if (mostApplicableIMI != null) {
2555 Slog.w(TAG, "Most applicable shortcut input method was:"
2556 + mostApplicableIMI.getId());
2557 if (mostApplicableSubtype != null) {
2558 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2559 + "," + mostApplicableSubtype.getMode() + ","
2560 + mostApplicableSubtype.getLocale());
2561 }
2562 }
satok4e4569d2010-11-19 18:45:53 +09002563 }
satokcd7cd292010-11-20 15:46:23 +09002564 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002565 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002566 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002567 } else {
2568 return null;
2569 }
2570 }
2571
satokab751aa2010-09-14 19:17:36 +09002572 /**
2573 * @return Return the current subtype of this input method.
2574 */
satok42c5a162011-05-26 16:46:14 +09002575 @Override
satokab751aa2010-09-14 19:17:36 +09002576 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002577 boolean subtypeIsSelected = false;
2578 try {
2579 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2580 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2581 } catch (SettingNotFoundException e) {
2582 }
satok3ef8b292010-11-23 06:06:29 +09002583 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002584 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002585 String lastInputMethodId = Settings.Secure.getString(
2586 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002587 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2588 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002589 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2590 if (imi != null) {
2591 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002592 // the most applicable subtype from explicitly or implicitly enabled
2593 // subtypes.
2594 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2595 getEnabledInputMethodSubtypeList(imi, true);
2596 // If there is only one explicitly or implicitly enabled subtype,
2597 // just returns it.
2598 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2599 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2600 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2601 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2602 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2603 SUBTYPE_MODE_KEYBOARD, null, true);
2604 if (mCurrentSubtype == null) {
2605 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2606 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2607 true);
2608 }
2609 }
satok4e4569d2010-11-19 18:45:53 +09002610 }
satokcd7cd292010-11-20 15:46:23 +09002611 } else {
satok3ef8b292010-11-23 06:06:29 +09002612 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002613 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002614 }
satok8fbb1e82010-11-02 23:15:58 +09002615 }
satok3ef8b292010-11-23 06:06:29 +09002616 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002617 }
satokab751aa2010-09-14 19:17:36 +09002618 }
2619
satokf3db1af2010-11-23 13:34:33 +09002620 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2621 InputMethodSubtype subtype) {
2622 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2623 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2624 } else {
2625 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2626 subtypes.add(subtype);
2627 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2628 }
2629 }
2630
satok4e4569d2010-11-19 18:45:53 +09002631 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002632 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002633 @Override
satok4e4569d2010-11-19 18:45:53 +09002634 public List getShortcutInputMethodsAndSubtypes() {
2635 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002636 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002637 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002638 // If there are no selected shortcut subtypes, the framework will try to find
2639 // the most applicable subtype from all subtypes whose mode is
2640 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002641 Pair<InputMethodInfo, InputMethodSubtype> info =
2642 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2643 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002644 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002645 ret.add(info.first);
2646 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002647 }
satok3da92232011-01-11 22:46:30 +09002648 return ret;
satokf3db1af2010-11-23 13:34:33 +09002649 }
satokf3db1af2010-11-23 13:34:33 +09002650 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2651 ret.add(imi);
2652 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2653 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002654 }
2655 }
satokf3db1af2010-11-23 13:34:33 +09002656 return ret;
satok4e4569d2010-11-19 18:45:53 +09002657 }
2658 }
2659
satok42c5a162011-05-26 16:46:14 +09002660 @Override
satokb66d2872010-11-10 01:04:04 +09002661 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2662 synchronized (mMethodMap) {
2663 if (subtype != null && mCurMethodId != null) {
2664 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2665 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2666 if (subtypeId != NOT_A_SUBTYPE_ID) {
2667 setInputMethodLocked(mCurMethodId, subtypeId);
2668 return true;
2669 }
2670 }
2671 return false;
2672 }
2673 }
2674
satokd87c2592010-09-29 11:52:06 +09002675 /**
2676 * Utility class for putting and getting settings for InputMethod
2677 * TODO: Move all putters and getters of settings to this class.
2678 */
2679 private static class InputMethodSettings {
2680 // The string for enabled input method is saved as follows:
2681 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2682 private static final char INPUT_METHOD_SEPARATER = ':';
2683 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002684 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002685 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2686
satok723a27e2010-11-11 14:58:11 +09002687 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002688 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2689
satokdf31ae62011-01-15 06:19:44 +09002690 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002691 private final ContentResolver mResolver;
2692 private final HashMap<String, InputMethodInfo> mMethodMap;
2693 private final ArrayList<InputMethodInfo> mMethodList;
2694
2695 private String mEnabledInputMethodsStrCache;
2696
2697 private static void buildEnabledInputMethodsSettingString(
2698 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2699 String id = pair.first;
2700 ArrayList<String> subtypes = pair.second;
2701 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002702 // Inputmethod and subtypes are saved in the settings as follows:
2703 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2704 for (String subtypeId: subtypes) {
2705 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002706 }
2707 }
2708
2709 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002710 Resources res, ContentResolver resolver,
2711 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2712 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002713 mResolver = resolver;
2714 mMethodMap = methodMap;
2715 mMethodList = methodList;
2716 }
2717
2718 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2719 return createEnabledInputMethodListLocked(
2720 getEnabledInputMethodsAndSubtypeListLocked());
2721 }
2722
satok7f35c8c2010-10-07 21:13:11 +09002723 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002724 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2725 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002726 getEnabledInputMethodsAndSubtypeListLocked());
2727 }
2728
satok67ddf9c2010-11-17 09:45:54 +09002729 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2730 InputMethodInfo imi) {
2731 List<Pair<String, ArrayList<String>>> imsList =
2732 getEnabledInputMethodsAndSubtypeListLocked();
2733 ArrayList<InputMethodSubtype> enabledSubtypes =
2734 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002735 if (imi != null) {
2736 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2737 InputMethodInfo info = mMethodMap.get(imsPair.first);
2738 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002739 final int subtypeCount = info.getSubtypeCount();
2740 for (int i = 0; i < subtypeCount; ++i) {
2741 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002742 for (String s: imsPair.second) {
2743 if (String.valueOf(ims.hashCode()).equals(s)) {
2744 enabledSubtypes.add(ims);
2745 }
satok67ddf9c2010-11-17 09:45:54 +09002746 }
2747 }
satok884ef9a2010-11-18 10:39:46 +09002748 break;
satok67ddf9c2010-11-17 09:45:54 +09002749 }
satok67ddf9c2010-11-17 09:45:54 +09002750 }
2751 }
2752 return enabledSubtypes;
2753 }
2754
satokd87c2592010-09-29 11:52:06 +09002755 // At the initial boot, the settings for input methods are not set,
2756 // so we need to enable IME in that case.
2757 public void enableAllIMEsIfThereIsNoEnabledIME() {
2758 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2759 StringBuilder sb = new StringBuilder();
2760 final int N = mMethodList.size();
2761 for (int i = 0; i < N; i++) {
2762 InputMethodInfo imi = mMethodList.get(i);
2763 Slog.i(TAG, "Adding: " + imi.getId());
2764 if (i > 0) sb.append(':');
2765 sb.append(imi.getId());
2766 }
2767 putEnabledInputMethodsStr(sb.toString());
2768 }
2769 }
2770
satokbb4aa062011-01-19 21:40:27 +09002771 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002772 ArrayList<Pair<String, ArrayList<String>>> imsList
2773 = new ArrayList<Pair<String, ArrayList<String>>>();
2774 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2775 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2776 return imsList;
2777 }
satok723a27e2010-11-11 14:58:11 +09002778 mInputMethodSplitter.setString(enabledInputMethodsStr);
2779 while (mInputMethodSplitter.hasNext()) {
2780 String nextImsStr = mInputMethodSplitter.next();
2781 mSubtypeSplitter.setString(nextImsStr);
2782 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002783 ArrayList<String> subtypeHashes = new ArrayList<String>();
2784 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002785 String imeId = mSubtypeSplitter.next();
2786 while (mSubtypeSplitter.hasNext()) {
2787 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002788 }
2789 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2790 }
2791 }
2792 return imsList;
2793 }
2794
2795 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2796 if (reloadInputMethodStr) {
2797 getEnabledInputMethodsStr();
2798 }
2799 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2800 // Add in the newly enabled input method.
2801 putEnabledInputMethodsStr(id);
2802 } else {
2803 putEnabledInputMethodsStr(
2804 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2805 }
2806 }
2807
2808 /**
2809 * Build and put a string of EnabledInputMethods with removing specified Id.
2810 * @return the specified id was removed or not.
2811 */
2812 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2813 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2814 boolean isRemoved = false;
2815 boolean needsAppendSeparator = false;
2816 for (Pair<String, ArrayList<String>> ims: imsList) {
2817 String curId = ims.first;
2818 if (curId.equals(id)) {
2819 // We are disabling this input method, and it is
2820 // currently enabled. Skip it to remove from the
2821 // new list.
2822 isRemoved = true;
2823 } else {
2824 if (needsAppendSeparator) {
2825 builder.append(INPUT_METHOD_SEPARATER);
2826 } else {
2827 needsAppendSeparator = true;
2828 }
2829 buildEnabledInputMethodsSettingString(builder, ims);
2830 }
2831 }
2832 if (isRemoved) {
2833 // Update the setting with the new list of input methods.
2834 putEnabledInputMethodsStr(builder.toString());
2835 }
2836 return isRemoved;
2837 }
2838
2839 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2840 List<Pair<String, ArrayList<String>>> imsList) {
2841 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2842 for (Pair<String, ArrayList<String>> ims: imsList) {
2843 InputMethodInfo info = mMethodMap.get(ims.first);
2844 if (info != null) {
2845 res.add(info);
2846 }
2847 }
2848 return res;
2849 }
2850
satok7f35c8c2010-10-07 21:13:11 +09002851 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002852 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002853 List<Pair<String, ArrayList<String>>> imsList) {
2854 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2855 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2856 for (Pair<String, ArrayList<String>> ims : imsList) {
2857 InputMethodInfo info = mMethodMap.get(ims.first);
2858 if (info != null) {
2859 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2860 }
2861 }
2862 return res;
2863 }
2864
satokd87c2592010-09-29 11:52:06 +09002865 private void putEnabledInputMethodsStr(String str) {
2866 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2867 mEnabledInputMethodsStrCache = str;
2868 }
2869
2870 private String getEnabledInputMethodsStr() {
2871 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2872 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002873 if (DEBUG) {
2874 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2875 }
satokd87c2592010-09-29 11:52:06 +09002876 return mEnabledInputMethodsStrCache;
2877 }
satok723a27e2010-11-11 14:58:11 +09002878
2879 private void saveSubtypeHistory(
2880 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2881 StringBuilder builder = new StringBuilder();
2882 boolean isImeAdded = false;
2883 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2884 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2885 newSubtypeId);
2886 isImeAdded = true;
2887 }
2888 for (Pair<String, String> ime: savedImes) {
2889 String imeId = ime.first;
2890 String subtypeId = ime.second;
2891 if (TextUtils.isEmpty(subtypeId)) {
2892 subtypeId = NOT_A_SUBTYPE_ID_STR;
2893 }
2894 if (isImeAdded) {
2895 builder.append(INPUT_METHOD_SEPARATER);
2896 } else {
2897 isImeAdded = true;
2898 }
2899 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2900 subtypeId);
2901 }
2902 // Remove the last INPUT_METHOD_SEPARATER
2903 putSubtypeHistoryStr(builder.toString());
2904 }
2905
2906 public void addSubtypeToHistory(String imeId, String subtypeId) {
2907 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2908 for (Pair<String, String> ime: subtypeHistory) {
2909 if (ime.first.equals(imeId)) {
2910 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002911 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002912 + ime.second);
2913 }
2914 // We should break here
2915 subtypeHistory.remove(ime);
2916 break;
2917 }
2918 }
satokbb4aa062011-01-19 21:40:27 +09002919 if (DEBUG) {
2920 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2921 }
satok723a27e2010-11-11 14:58:11 +09002922 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2923 }
2924
2925 private void putSubtypeHistoryStr(String str) {
2926 if (DEBUG) {
2927 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2928 }
2929 Settings.Secure.putString(
2930 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2931 }
2932
2933 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2934 // Gets the first one from the history
2935 return getLastSubtypeForInputMethodLockedInternal(null);
2936 }
2937
2938 public String getLastSubtypeForInputMethodLocked(String imeId) {
2939 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2940 if (ime != null) {
2941 return ime.second;
2942 } else {
2943 return null;
2944 }
2945 }
2946
2947 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2948 List<Pair<String, ArrayList<String>>> enabledImes =
2949 getEnabledInputMethodsAndSubtypeListLocked();
2950 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002951 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002952 final String imeInTheHistory = imeAndSubtype.first;
2953 // If imeId is empty, returns the first IME and subtype in the history
2954 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2955 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002956 final String subtypeHashCode =
2957 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2958 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002959 if (!TextUtils.isEmpty(subtypeHashCode)) {
2960 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002961 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002962 }
2963 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2964 }
2965 }
2966 }
2967 if (DEBUG) {
2968 Slog.d(TAG, "No enabled IME found in the history");
2969 }
2970 return null;
2971 }
2972
satokdf31ae62011-01-15 06:19:44 +09002973 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002974 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2975 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2976 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002977 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2978 if (explicitlyEnabledSubtypes.size() == 0) {
2979 // If there are no explicitly enabled subtypes, applicable subtypes are
2980 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09002981 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09002982 // If IME is enabled and no subtypes are enabled, applicable subtypes
2983 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09002984 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002985 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09002986 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09002987 if (implicitlySelectedSubtypes != null) {
2988 final int N = implicitlySelectedSubtypes.size();
2989 for (int i = 0; i < N; ++i) {
2990 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2991 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2992 return subtypeHashCode;
2993 }
2994 }
2995 }
2996 }
2997 } else {
satokf6cafb62011-01-17 16:29:02 +09002998 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002999 if (s.equals(subtypeHashCode)) {
3000 // If both imeId and subtypeId are enabled, return subtypeId.
3001 return s;
3002 }
satok723a27e2010-11-11 14:58:11 +09003003 }
3004 }
3005 // If imeId was enabled but subtypeId was disabled.
3006 return NOT_A_SUBTYPE_ID_STR;
3007 }
3008 }
3009 // If both imeId and subtypeId are disabled, return null
3010 return null;
3011 }
3012
3013 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3014 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3015 final String subtypeHistoryStr = getSubtypeHistoryStr();
3016 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3017 return imsList;
3018 }
3019 mInputMethodSplitter.setString(subtypeHistoryStr);
3020 while (mInputMethodSplitter.hasNext()) {
3021 String nextImsStr = mInputMethodSplitter.next();
3022 mSubtypeSplitter.setString(nextImsStr);
3023 if (mSubtypeSplitter.hasNext()) {
3024 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3025 // The first element is ime id.
3026 String imeId = mSubtypeSplitter.next();
3027 while (mSubtypeSplitter.hasNext()) {
3028 subtypeId = mSubtypeSplitter.next();
3029 break;
3030 }
3031 imsList.add(new Pair<String, String>(imeId, subtypeId));
3032 }
3033 }
3034 return imsList;
3035 }
3036
3037 private String getSubtypeHistoryStr() {
3038 if (DEBUG) {
3039 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3040 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3041 }
3042 return Settings.Secure.getString(
3043 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3044 }
3045
3046 public void putSelectedInputMethod(String imeId) {
3047 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3048 }
3049
3050 public void putSelectedSubtype(int subtypeId) {
3051 Settings.Secure.putInt(
3052 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3053 }
satokd87c2592010-09-29 11:52:06 +09003054 }
3055
satoke7c6998e2011-06-03 17:57:59 +09003056 private static class InputMethodFileManager {
3057 private static final String SYSTEM_PATH = "system";
3058 private static final String INPUT_METHOD_PATH = "inputmethod";
3059 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3060 private static final String NODE_SUBTYPES = "subtypes";
3061 private static final String NODE_SUBTYPE = "subtype";
3062 private static final String NODE_IMI = "imi";
3063 private static final String ATTR_ID = "id";
3064 private static final String ATTR_LABEL = "label";
3065 private static final String ATTR_ICON = "icon";
3066 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3067 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3068 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3069 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3070 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3071 private final HashMap<String, InputMethodInfo> mMethodMap;
3072 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3073 new HashMap<String, List<InputMethodSubtype>>();
3074 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3075 if (methodMap == null) {
3076 throw new NullPointerException("methodMap is null");
3077 }
3078 mMethodMap = methodMap;
3079 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3080 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3081 if (!inputMethodDir.mkdirs()) {
3082 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3083 }
3084 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3085 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3086 if (!subtypeFile.exists()) {
3087 // If "subtypes.xml" doesn't exist, create a blank file.
3088 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3089 methodMap);
3090 } else {
3091 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3092 }
3093 }
3094
3095 private void deleteAllInputMethodSubtypes(String imiId) {
3096 synchronized (mMethodMap) {
3097 mSubtypesMap.remove(imiId);
3098 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3099 mMethodMap);
3100 }
3101 }
3102
3103 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003104 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003105 synchronized (mMethodMap) {
3106 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3107 final int N = additionalSubtypes.length;
3108 for (int i = 0; i < N; ++i) {
3109 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003110 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003111 subtypes.add(subtype);
3112 }
3113 }
satok4a28bde2011-06-29 21:03:40 +09003114 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003115 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3116 mMethodMap);
3117 }
3118 }
3119
3120 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3121 synchronized (mMethodMap) {
3122 return mSubtypesMap;
3123 }
3124 }
3125
3126 private static void writeAdditionalInputMethodSubtypes(
3127 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3128 HashMap<String, InputMethodInfo> methodMap) {
3129 // Safety net for the case that this function is called before methodMap is set.
3130 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3131 FileOutputStream fos = null;
3132 try {
3133 fos = subtypesFile.startWrite();
3134 final XmlSerializer out = new FastXmlSerializer();
3135 out.setOutput(fos, "utf-8");
3136 out.startDocument(null, true);
3137 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3138 out.startTag(null, NODE_SUBTYPES);
3139 for (String imiId : allSubtypes.keySet()) {
3140 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3141 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3142 continue;
3143 }
3144 out.startTag(null, NODE_IMI);
3145 out.attribute(null, ATTR_ID, imiId);
3146 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3147 final int N = subtypesList.size();
3148 for (int i = 0; i < N; ++i) {
3149 final InputMethodSubtype subtype = subtypesList.get(i);
3150 out.startTag(null, NODE_SUBTYPE);
3151 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3152 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3153 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3154 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3155 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3156 out.attribute(null, ATTR_IS_AUXILIARY,
3157 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3158 out.endTag(null, NODE_SUBTYPE);
3159 }
3160 out.endTag(null, NODE_IMI);
3161 }
3162 out.endTag(null, NODE_SUBTYPES);
3163 out.endDocument();
3164 subtypesFile.finishWrite(fos);
3165 } catch (java.io.IOException e) {
3166 Slog.w(TAG, "Error writing subtypes", e);
3167 if (fos != null) {
3168 subtypesFile.failWrite(fos);
3169 }
3170 }
3171 }
3172
3173 private static void readAdditionalInputMethodSubtypes(
3174 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3175 if (allSubtypes == null || subtypesFile == null) return;
3176 allSubtypes.clear();
3177 FileInputStream fis = null;
3178 try {
3179 fis = subtypesFile.openRead();
3180 final XmlPullParser parser = Xml.newPullParser();
3181 parser.setInput(fis, null);
3182 int type = parser.getEventType();
3183 // Skip parsing until START_TAG
3184 while ((type = parser.next()) != XmlPullParser.START_TAG
3185 && type != XmlPullParser.END_DOCUMENT) {}
3186 String firstNodeName = parser.getName();
3187 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3188 throw new XmlPullParserException("Xml doesn't start with subtypes");
3189 }
3190 final int depth =parser.getDepth();
3191 String currentImiId = null;
3192 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3193 while (((type = parser.next()) != XmlPullParser.END_TAG
3194 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3195 if (type != XmlPullParser.START_TAG)
3196 continue;
3197 final String nodeName = parser.getName();
3198 if (NODE_IMI.equals(nodeName)) {
3199 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3200 if (TextUtils.isEmpty(currentImiId)) {
3201 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3202 continue;
3203 }
3204 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3205 allSubtypes.put(currentImiId, tempSubtypesArray);
3206 } else if (NODE_SUBTYPE.equals(nodeName)) {
3207 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3208 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3209 continue;
3210 }
3211 final int icon = Integer.valueOf(
3212 parser.getAttributeValue(null, ATTR_ICON));
3213 final int label = Integer.valueOf(
3214 parser.getAttributeValue(null, ATTR_LABEL));
3215 final String imeSubtypeLocale =
3216 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3217 final String imeSubtypeMode =
3218 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3219 final String imeSubtypeExtraValue =
3220 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003221 final boolean isAuxiliary = "1".equals(String.valueOf(
3222 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003223 final InputMethodSubtype subtype =
3224 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3225 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3226 tempSubtypesArray.add(subtype);
3227 }
3228 }
3229 } catch (XmlPullParserException e) {
3230 Slog.w(TAG, "Error reading subtypes: " + e);
3231 return;
3232 } catch (java.io.IOException e) {
3233 Slog.w(TAG, "Error reading subtypes: " + e);
3234 return;
3235 } catch (NumberFormatException e) {
3236 Slog.w(TAG, "Error reading subtypes: " + e);
3237 return;
3238 } finally {
3239 if (fis != null) {
3240 try {
3241 fis.close();
3242 } catch (java.io.IOException e1) {
3243 Slog.w(TAG, "Failed to close.");
3244 }
3245 }
3246 }
3247 }
3248 }
3249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 @Override
3253 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3254 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3255 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3258 + Binder.getCallingPid()
3259 + ", uid=" + Binder.getCallingUid());
3260 return;
3261 }
3262
3263 IInputMethod method;
3264 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 synchronized (mMethodMap) {
3269 p.println("Current Input Method Manager state:");
3270 int N = mMethodList.size();
3271 p.println(" Input Methods:");
3272 for (int i=0; i<N; i++) {
3273 InputMethodInfo info = mMethodList.get(i);
3274 p.println(" InputMethod #" + i + ":");
3275 info.dump(p, " ");
3276 }
3277 p.println(" Clients:");
3278 for (ClientState ci : mClients.values()) {
3279 p.println(" Client " + ci + ":");
3280 p.println(" client=" + ci.client);
3281 p.println(" inputContext=" + ci.inputContext);
3282 p.println(" sessionRequested=" + ci.sessionRequested);
3283 p.println(" curSession=" + ci.curSession);
3284 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003285 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003287 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3288 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3290 + " mBoundToMethod=" + mBoundToMethod);
3291 p.println(" mCurToken=" + mCurToken);
3292 p.println(" mCurIntent=" + mCurIntent);
3293 method = mCurMethod;
3294 p.println(" mCurMethod=" + mCurMethod);
3295 p.println(" mEnabledSession=" + mEnabledSession);
3296 p.println(" mShowRequested=" + mShowRequested
3297 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3298 + " mShowForced=" + mShowForced
3299 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003300 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003302
Jeff Brownb88102f2010-09-08 11:49:43 -07003303 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 pw.flush();
3306 try {
3307 client.client.asBinder().dump(fd, args);
3308 } catch (RemoteException e) {
3309 p.println("Input method client dead: " + e);
3310 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003311 } else {
3312 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003314
Jeff Brownb88102f2010-09-08 11:49:43 -07003315 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 pw.flush();
3318 try {
3319 method.asBinder().dump(fd, args);
3320 } catch (RemoteException e) {
3321 p.println("Input method service dead: " + e);
3322 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003323 } else {
3324 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 }
3326 }
3327}