blob: d39f565f7f847d79bbe5a748b7188c30dd43caa9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2008 The Android Open Source Project
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08007 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08009 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090020import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090022import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.view.IInputContext;
24import com.android.internal.view.IInputMethod;
25import com.android.internal.view.IInputMethodCallback;
26import com.android.internal.view.IInputMethodClient;
27import com.android.internal.view.IInputMethodManager;
28import com.android.internal.view.IInputMethodSession;
29import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080030import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090038import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090039import android.app.Notification;
40import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070041import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.ComponentName;
43import android.content.ContentResolver;
44import android.content.Context;
45import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.DialogInterface.OnCancelListener;
47import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090048import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070050import android.content.pm.ApplicationInfo;
satok91e88122011-07-18 11:11:42 +090051import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070056import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.res.Resources;
58import android.content.res.TypedArray;
59import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090060import android.graphics.BitmapFactory;
Joe Onorato857fd9b2011-01-27 15:08:35 -080061import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090063import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Handler;
65import android.os.IBinder;
66import android.os.IInterface;
67import android.os.Message;
68import android.os.Parcel;
69import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080070import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.ServiceManager;
72import android.os.SystemClock;
73import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070074import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090075import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090077import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090079import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090080import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.util.PrintWriterPrinter;
82import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090083import android.util.Slog;
84import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.view.IWindowManager;
86import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090087import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.inputmethod.InputBinding;
89import android.view.inputmethod.InputMethod;
90import android.view.inputmethod.InputMethodInfo;
91import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090092import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
satoke7c6998e2011-06-03 17:57:59 +090094import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090096import java.io.FileInputStream;
97import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.IOException;
99import java.io.PrintWriter;
100import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +0900101import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900102import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900104import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900106import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900107import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109/**
110 * This class provides a system service that manages input methods.
111 */
112public class InputMethodManagerService extends IInputMethodManager.Stub
113 implements ServiceConnection, Handler.Callback {
114 static final boolean DEBUG = false;
115 static final String TAG = "InputManagerService";
116
117 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900118 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900119 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900120 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 static final int MSG_UNBIND_INPUT = 1000;
123 static final int MSG_BIND_INPUT = 1010;
124 static final int MSG_SHOW_SOFT_INPUT = 1020;
125 static final int MSG_HIDE_SOFT_INPUT = 1030;
126 static final int MSG_ATTACH_TOKEN = 1040;
127 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 static final int MSG_START_INPUT = 2000;
130 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 static final int MSG_UNBIND_METHOD = 3000;
133 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
satokf9f01002011-05-19 21:31:50 +0900137 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
138
satokab751aa2010-09-14 19:17:36 +0900139 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900140 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900141 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
142 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900143 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800146 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900148 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700150 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final IWindowManager mIWindowManager;
152 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900153 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // All known input methods. mMethodMap also serves as the global
158 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900159 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
160 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900161 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
162 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
satok7cfc0ed2011-06-20 21:29:36 +0900164 // Ongoing notification
165 private final NotificationManager mNotificationManager;
satokf90a33e2011-07-19 11:55:52 +0900166 private final KeyguardManager mKeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +0900167 private final Notification mImeSwitcherNotification;
168 private final PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900169 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900170 private boolean mNotificationShown;
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 class SessionState {
173 final ClientState client;
174 final IInputMethod method;
175 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 @Override
178 public String toString() {
179 return "SessionState{uid " + client.uid + " pid " + client.pid
180 + " method " + Integer.toHexString(
181 System.identityHashCode(method))
182 + " session " + Integer.toHexString(
183 System.identityHashCode(session))
184 + "}";
185 }
186
187 SessionState(ClientState _client, IInputMethod _method,
188 IInputMethodSession _session) {
189 client = _client;
190 method = _method;
191 session = _session;
192 }
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 class ClientState {
196 final IInputMethodClient client;
197 final IInputContext inputContext;
198 final int uid;
199 final int pid;
200 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 boolean sessionRequested;
203 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 @Override
206 public String toString() {
207 return "ClientState{" + Integer.toHexString(
208 System.identityHashCode(this)) + " uid " + uid
209 + " pid " + pid + "}";
210 }
211
212 ClientState(IInputMethodClient _client, IInputContext _inputContext,
213 int _uid, int _pid) {
214 client = _client;
215 inputContext = _inputContext;
216 uid = _uid;
217 pid = _pid;
218 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
219 }
220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 final HashMap<IBinder, ClientState> mClients
223 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700226 * Set once the system is ready to run third party code.
227 */
228 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * Id of the currently selected input method.
232 */
233 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * The current binding sequence number, incremented every time there is
237 * a new bind performed.
238 */
239 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * The client that is currently bound to an input method.
243 */
244 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700247 * The last window token that gained focus.
248 */
249 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 * The input context last provided by the current client.
253 */
254 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * The attributes last provided by the current client.
258 */
259 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
262 * The input method ID of the input method service that we are currently
263 * connected to or in the process of connecting to.
264 */
265 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 /**
satokab751aa2010-09-14 19:17:36 +0900268 * The current subtype of the current input method.
269 */
270 private InputMethodSubtype mCurrentSubtype;
271
satok4e4569d2010-11-19 18:45:53 +0900272 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900273 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
274 mShortcutInputMethodsAndSubtypes =
275 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900276
277 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 * Set to true if our ServiceConnection is currently actively bound to
279 * a service (whether or not we have gotten its IBinder back yet).
280 */
281 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 /**
284 * Set if the client has asked for the input method to be shown.
285 */
286 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 /**
289 * Set if we were explicitly told to show the input method.
290 */
291 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Set if we were forced to be shown.
295 */
296 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 /**
299 * Set if we last told the input method to show itself.
300 */
301 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
304 * The Intent used to connect to the current input method.
305 */
306 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 /**
309 * The token we have made for the currently active input method, to
310 * identify it in the future.
311 */
312 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 /**
315 * If non-null, this is the input method service we are currently connected
316 * to.
317 */
318 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * Time that we last initiated a bind to the input method, to determine
322 * if we should try to disconnect and reconnect to it.
323 */
324 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 /**
327 * Have we called mCurMethod.bindInput()?
328 */
329 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 /**
332 * Currently enabled session. Only touched by service thread, not
333 * protected by a lock.
334 */
335 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * True if the screen is on. The value is true initially.
339 */
340 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800341
Joe Onorato857fd9b2011-01-27 15:08:35 -0800342 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
343 int mImeWindowVis;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 AlertDialog.Builder mDialogBuilder;
346 AlertDialog mSwitchingDialog;
347 InputMethodInfo[] mIms;
348 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900349 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 class SettingsObserver extends ContentObserver {
352 SettingsObserver(Handler handler) {
353 super(handler);
354 ContentResolver resolver = mContext.getContentResolver();
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
356 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900358 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
359 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900360 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override public void onChange(boolean selfChange) {
364 synchronized (mMethodMap) {
365 updateFromSettingsLocked();
366 }
367 }
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
371 @Override
372 public void onReceive(Context context, Intent intent) {
373 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
374 mScreenOn = true;
375 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
376 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
378 hideInputMethodMenu();
379 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 // Inform the current client of the change in active status
385 try {
386 if (mCurClient != null && mCurClient.client != null) {
387 mCurClient.client.setActive(mScreenOn);
388 }
389 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800390 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + mCurClient.pid + " uid " + mCurClient.uid);
392 }
393 }
394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 class MyPackageMonitor extends PackageMonitor {
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
403 final int N = mMethodList.size();
404 if (curInputMethodId != null) {
405 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 InputMethodInfo imi = mMethodList.get(i);
407 if (imi.getId().equals(curInputMethodId)) {
408 for (String pkg : packages) {
409 if (imi.getPackageName().equals(pkg)) {
410 if (!doit) {
411 return true;
412 }
satok723a27e2010-11-11 14:58:11 +0900413 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 chooseNewDefaultIMELocked();
415 return true;
416 }
417 }
418 }
419 }
420 }
421 }
422 return false;
423 }
424
425 @Override
426 public void onSomePackagesChanged() {
427 synchronized (mMethodMap) {
428 InputMethodInfo curIm = null;
429 String curInputMethodId = Settings.Secure.getString(mContext
430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
434 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900435 final String imiId = imi.getId();
436 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 curIm = imi;
438 }
satoke7c6998e2011-06-03 17:57:59 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900441 if (isPackageModified(imi.getPackageName())) {
442 mFileManager.deleteAllInputMethodSubtypes(imiId);
443 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800446 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 + imi.getComponent());
448 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 buildInputMethodListLocked(mMethodList, mMethodMap);
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 int change = isPackageDisappearing(curIm.getPackageName());
459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 ServiceInfo si = null;
462 try {
463 si = mContext.getPackageManager().getServiceInfo(
464 curIm.getComponent(), 0);
465 } catch (PackageManager.NameNotFoundException ex) {
466 }
467 if (si == null) {
468 // Uh oh, current input method is no longer around!
469 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 mImeWindowVis = 0;
472 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
473 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800474 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 changed = true;
476 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900478 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800479 }
480 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800481 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800482 }
satokab751aa2010-09-14 19:17:36 +0900483
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 if (curIm == null) {
485 // We currently don't have a default input method... is
486 // one now available?
487 changed = chooseNewDefaultIMELocked();
488 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800489
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 if (changed) {
491 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 }
494 }
495 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 class MethodCallback extends IInputMethodCallback.Stub {
498 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 MethodCallback(IInputMethod method) {
501 mMethod = method;
502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800503
satoke7c6998e2011-06-03 17:57:59 +0900504 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public void finishedEvent(int seq, boolean handled) throws RemoteException {
506 }
507
satoke7c6998e2011-06-03 17:57:59 +0900508 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public void sessionCreated(IInputMethodSession session) throws RemoteException {
510 onSessionCreated(mMethod, session);
511 }
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Joe Onorato089de882010-04-12 08:18:45 -0700514 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800516 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 mHandler = new Handler(this);
518 mIWindowManager = IWindowManager.Stub.asInterface(
519 ServiceManager.getService(Context.WINDOW_SERVICE));
520 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900521 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public void executeMessage(Message msg) {
523 handleMessage(msg);
524 }
525 });
satok7cfc0ed2011-06-20 21:29:36 +0900526
satokf90a33e2011-07-19 11:55:52 +0900527 mKeyguardManager = (KeyguardManager)
528 mContext.getSystemService(Context.KEYGUARD_SERVICE);
satok7cfc0ed2011-06-20 21:29:36 +0900529 mNotificationManager = (NotificationManager)
530 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
531 mImeSwitcherNotification = new Notification();
532 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
533 mImeSwitcherNotification.when = 0;
534 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
535 mImeSwitcherNotification.tickerText = null;
536 mImeSwitcherNotification.defaults = 0; // please be quiet
537 mImeSwitcherNotification.sound = null;
538 mImeSwitcherNotification.vibrate = null;
539 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900540 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900541
542 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900543
satoke7c6998e2011-06-03 17:57:59 +0900544 synchronized (mMethodMap) {
545 mFileManager = new InputMethodFileManager(mMethodMap);
546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 IntentFilter screenOnOffFilt = new IntentFilter();
551 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
552 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700553 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
satok913a8922010-08-26 21:53:41 +0900556 mStatusBar = statusBar;
557 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900558 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900559
satokd87c2592010-09-29 11:52:06 +0900560 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900561 mSettings = new InputMethodSettings(
562 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900564 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565
satokd87c2592010-09-29 11:52:06 +0900566 if (TextUtils.isEmpty(Settings.Secure.getString(
567 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900569 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
571 try {
satokd87c2592010-09-29 11:52:06 +0900572 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 imi.getPackageName(), 0).getResources();
574 if (res.getBoolean(imi.getIsDefaultResourceId())) {
575 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800576 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 } catch (PackageManager.NameNotFoundException ex) {
579 } catch (Resources.NotFoundException ex) {
580 }
581 }
582 }
satokd87c2592010-09-29 11:52:06 +0900583 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800585 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900588 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mSettingsObserver = new SettingsObserver(mHandler);
593 updateFromSettingsLocked();
594 }
595
596 @Override
597 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
598 throws RemoteException {
599 try {
600 return super.onTransact(code, data, reply, flags);
601 } catch (RuntimeException e) {
602 // The input method manager only throws security exceptions, so let's
603 // log all others.
604 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800605 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 throw e;
608 }
609 }
610
611 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700612 synchronized (mMethodMap) {
613 if (!mSystemReady) {
614 mSystemReady = true;
satokb858c732011-07-22 19:54:34 +0900615 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
616 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700617 try {
618 startInputInnerLocked();
619 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700621 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700622 }
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
satoke7c6998e2011-06-03 17:57:59 +0900626 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 public List<InputMethodInfo> getInputMethodList() {
628 synchronized (mMethodMap) {
629 return new ArrayList<InputMethodInfo>(mMethodList);
630 }
631 }
632
satoke7c6998e2011-06-03 17:57:59 +0900633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public List<InputMethodInfo> getEnabledInputMethodList() {
635 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900636 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638 }
639
satokbb4aa062011-01-19 21:40:27 +0900640 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
641 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
642 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
643 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
644 for (InputMethodInfo imi: getEnabledInputMethodList()) {
645 enabledInputMethodAndSubtypes.put(
646 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
647 }
648 return enabledInputMethodAndSubtypes;
649 }
650
651 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
652 boolean allowsImplicitlySelectedSubtypes) {
653 if (imi == null && mCurMethodId != null) {
654 imi = mMethodMap.get(mCurMethodId);
655 }
satok7265d9b2011-02-14 15:47:30 +0900656 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900657 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900658 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
659 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900660 }
satok7265d9b2011-02-14 15:47:30 +0900661 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900662 }
663
satoke7c6998e2011-06-03 17:57:59 +0900664 @Override
satok16331c82010-12-20 23:48:46 +0900665 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
666 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900667 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900668 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900669 }
670 }
671
satoke7c6998e2011-06-03 17:57:59 +0900672 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 public void addClient(IInputMethodClient client,
674 IInputContext inputContext, int uid, int pid) {
675 synchronized (mMethodMap) {
676 mClients.put(client.asBinder(), new ClientState(client,
677 inputContext, uid, pid));
678 }
679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
satoke7c6998e2011-06-03 17:57:59 +0900681 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public void removeClient(IInputMethodClient client) {
683 synchronized (mMethodMap) {
684 mClients.remove(client.asBinder());
685 }
686 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 void executeOrSendMessage(IInterface target, Message msg) {
689 if (target.asBinder() instanceof Binder) {
690 mCaller.sendMessage(msg);
691 } else {
692 handleMessage(msg);
693 msg.recycle();
694 }
695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800696
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700697 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800699 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 + mCurClient.client.asBinder());
701 if (mBoundToMethod) {
702 mBoundToMethod = false;
703 if (mCurMethod != null) {
704 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
705 MSG_UNBIND_INPUT, mCurMethod));
706 }
707 }
708 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
709 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
710 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 // Call setActive(false) on the old client
713 try {
714 mCurClient.client.setActive(false);
715 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800716 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 + mCurClient.pid + " uid " + mCurClient.uid);
718 }
719 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
The Android Open Source Project10592532009-03-18 17:39:46 -0700721 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 private int getImeShowFlags() {
726 int flags = 0;
727 if (mShowForced) {
728 flags |= InputMethod.SHOW_FORCED
729 | InputMethod.SHOW_EXPLICIT;
730 } else if (mShowExplicitlyRequested) {
731 flags |= InputMethod.SHOW_EXPLICIT;
732 }
733 return flags;
734 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 private int getAppShowFlags() {
737 int flags = 0;
738 if (mShowForced) {
739 flags |= InputMethodManager.SHOW_FORCED;
740 } else if (!mShowExplicitlyRequested) {
741 flags |= InputMethodManager.SHOW_IMPLICIT;
742 }
743 return flags;
744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
747 if (!mBoundToMethod) {
748 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
749 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
750 mBoundToMethod = true;
751 }
752 final SessionState session = mCurClient.curSession;
753 if (initial) {
754 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
755 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
756 } else {
757 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
758 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
759 }
760 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800762 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 return needResult
765 ? new InputBindResult(session.session, mCurId, mCurSeq)
766 : null;
767 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 InputBindResult startInputLocked(IInputMethodClient client,
770 IInputContext inputContext, EditorInfo attribute,
771 boolean initial, boolean needResult) {
772 // If no method is currently selected, do nothing.
773 if (mCurMethodId == null) {
774 return mNoBinding;
775 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 ClientState cs = mClients.get(client.asBinder());
778 if (cs == null) {
779 throw new IllegalArgumentException("unknown client "
780 + client.asBinder());
781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 try {
784 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
785 // Check with the window manager to make sure this client actually
786 // has a window with focus. If not, reject. This is thread safe
787 // because if the focus changes some time before or after, the
788 // next client receiving focus that has any interest in input will
789 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800790 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
792 return null;
793 }
794 } catch (RemoteException e) {
795 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 if (mCurClient != cs) {
798 // If the client is changing, we need to switch over to the new
799 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700800 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 + cs.client.asBinder());
803
804 // If the screen is on, inform the new client it is active
805 if (mScreenOn) {
806 try {
807 cs.client.setActive(mScreenOn);
808 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 + cs.pid + " uid " + cs.uid);
811 }
812 }
813 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 // Bump up the sequence for this client and attach it.
816 mCurSeq++;
817 if (mCurSeq <= 0) mCurSeq = 1;
818 mCurClient = cs;
819 mCurInputContext = inputContext;
820 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 // Check if the input method is changing.
823 if (mCurId != null && mCurId.equals(mCurMethodId)) {
824 if (cs.curSession != null) {
825 // Fast case: if we are already connected to the input method,
826 // then just return it.
827 return attachNewInputLocked(initial, needResult);
828 }
829 if (mHaveConnection) {
830 if (mCurMethod != null) {
831 if (!cs.sessionRequested) {
832 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800833 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
835 MSG_CREATE_SESSION, mCurMethod,
836 new MethodCallback(mCurMethod)));
837 }
838 // Return to client, and we will get back with it when
839 // we have had a session made for it.
840 return new InputBindResult(null, mCurId, mCurSeq);
841 } else if (SystemClock.uptimeMillis()
842 < (mLastBindTime+TIME_TO_RECONNECT)) {
843 // In this case we have connected to the service, but
844 // don't yet have its interface. If it hasn't been too
845 // long since we did the connection, we'll return to
846 // the client and wait to get the service interface so
847 // we can report back. If it has been too long, we want
848 // to fall through so we can try a disconnect/reconnect
849 // to see if we can get back in touch with the service.
850 return new InputBindResult(null, mCurId, mCurSeq);
851 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
853 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 return startInputInnerLocked();
859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800860
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 InputBindResult startInputInnerLocked() {
862 if (mCurMethodId == null) {
863 return mNoBinding;
864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700866 if (!mSystemReady) {
867 // If the system is not yet ready, we shouldn't be running third
868 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700869 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 InputMethodInfo info = mMethodMap.get(mCurMethodId);
873 if (info == null) {
874 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700877 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
880 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700881 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
882 com.android.internal.R.string.input_method_binding_label);
883 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
884 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
886 mLastBindTime = SystemClock.uptimeMillis();
887 mHaveConnection = true;
888 mCurId = info.getId();
889 mCurToken = new Binder();
890 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800891 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 mIWindowManager.addWindowToken(mCurToken,
893 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
894 } catch (RemoteException e) {
895 }
896 return new InputBindResult(null, mCurId, mCurSeq);
897 } else {
898 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800899 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 + mCurIntent);
901 }
902 return null;
903 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800904
satoke7c6998e2011-06-03 17:57:59 +0900905 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 public InputBindResult startInput(IInputMethodClient client,
907 IInputContext inputContext, EditorInfo attribute,
908 boolean initial, boolean needResult) {
909 synchronized (mMethodMap) {
910 final long ident = Binder.clearCallingIdentity();
911 try {
912 return startInputLocked(client, inputContext, attribute,
913 initial, needResult);
914 } finally {
915 Binder.restoreCallingIdentity(ident);
916 }
917 }
918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
satoke7c6998e2011-06-03 17:57:59 +0900920 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void finishInput(IInputMethodClient client) {
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public void onServiceConnected(ComponentName name, IBinder service) {
926 synchronized (mMethodMap) {
927 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
928 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700929 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700931 unbindCurrentMethodLocked(false);
932 return;
933 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
936 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700939 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700941 MSG_CREATE_SESSION, mCurMethod,
942 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944 }
945 }
946 }
947
948 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
949 synchronized (mMethodMap) {
950 if (mCurMethod != null && method != null
951 && mCurMethod.asBinder() == method.asBinder()) {
952 if (mCurClient != null) {
953 mCurClient.curSession = new SessionState(mCurClient,
954 method, session);
955 mCurClient.sessionRequested = false;
956 InputBindResult res = attachNewInputLocked(true, true);
957 if (res.method != null) {
958 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
959 MSG_BIND_METHOD, mCurClient.client, res));
960 }
961 }
962 }
963 }
964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700966 void unbindCurrentMethodLocked(boolean reportToClient) {
967 if (mHaveConnection) {
968 mContext.unbindService(this);
969 mHaveConnection = false;
970 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800971
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700972 if (mCurToken != null) {
973 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800974 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700975 mIWindowManager.removeWindowToken(mCurToken);
976 } catch (RemoteException e) {
977 }
978 mCurToken = null;
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Project10592532009-03-18 17:39:46 -0700981 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700982 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800983
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700984 if (reportToClient && mCurClient != null) {
985 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
986 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
987 }
988 }
989
Devin Taylor0c33ed22010-02-23 13:26:46 -0600990 private void finishSession(SessionState sessionState) {
991 if (sessionState != null && sessionState.session != null) {
992 try {
993 sessionState.session.finishSession();
994 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700995 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600996 }
997 }
998 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800999
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001000 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (mCurMethod != null) {
1002 for (ClientState cs : mClients.values()) {
1003 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001004 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 cs.curSession = null;
1006 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001007
1008 finishSession(mEnabledSession);
1009 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 mCurMethod = null;
1011 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001012 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
satoke7c6998e2011-06-03 17:57:59 +09001015 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 public void onServiceDisconnected(ComponentName name) {
1017 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 + " mCurIntent=" + mCurIntent);
1020 if (mCurMethod != null && mCurIntent != null
1021 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001022 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 // We consider this to be a new bind attempt, since the system
1024 // should now try to restart the service for us.
1025 mLastBindTime = SystemClock.uptimeMillis();
1026 mShowRequested = mInputShown;
1027 mInputShown = false;
1028 if (mCurClient != null) {
1029 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1030 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1031 }
1032 }
1033 }
1034 }
1035
satokf9f01002011-05-19 21:31:50 +09001036 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001038 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 long ident = Binder.clearCallingIdentity();
1040 try {
1041 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001042 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 return;
1044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 synchronized (mMethodMap) {
1047 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001049 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001051 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001052 CharSequence contentDescription = null;
1053 try {
1054 PackageManager packageManager = mContext.getPackageManager();
1055 contentDescription = packageManager.getApplicationLabel(
1056 packageManager.getApplicationInfo(packageName, 0));
1057 } catch (NameNotFoundException nnfe) {
1058 /* ignore */
1059 }
1060 mStatusBar.setIcon("ime", packageName, iconId, 0,
1061 contentDescription != null ? contentDescription.toString() : null);
Joe Onorato0cbda992010-05-02 16:28:15 -07001062 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
1065 } finally {
1066 Binder.restoreCallingIdentity(ident);
1067 }
1068 }
1069
satok7cfc0ed2011-06-20 21:29:36 +09001070 private boolean needsToShowImeSwitchOngoingNotification() {
1071 if (!mShowOngoingImeSwitcherForPhones) return false;
1072 synchronized (mMethodMap) {
1073 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1074 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001075 if (N > 2) return true;
1076 if (N < 1) return false;
1077 int nonAuxCount = 0;
1078 int auxCount = 0;
1079 InputMethodSubtype nonAuxSubtype = null;
1080 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001081 for(int i = 0; i < N; ++i) {
1082 final InputMethodInfo imi = imis.get(i);
1083 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1084 imi, true);
1085 final int subtypeCount = subtypes.size();
1086 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001087 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001088 } else {
1089 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001090 final InputMethodSubtype subtype = subtypes.get(j);
1091 if (!subtype.isAuxiliary()) {
1092 ++nonAuxCount;
1093 nonAuxSubtype = subtype;
1094 } else {
1095 ++auxCount;
1096 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001097 }
1098 }
1099 }
satok7cfc0ed2011-06-20 21:29:36 +09001100 }
satokb6359412011-06-28 17:47:41 +09001101 if (nonAuxCount > 1 || auxCount > 1) {
1102 return true;
1103 } else if (nonAuxCount == 1 && auxCount == 1) {
1104 if (nonAuxSubtype != null && auxSubtype != null
1105 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1106 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1107 return false;
1108 }
1109 return true;
1110 }
1111 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001112 }
satok7cfc0ed2011-06-20 21:29:36 +09001113 }
1114
satokf9f01002011-05-19 21:31:50 +09001115 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001116 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001117 int uid = Binder.getCallingUid();
1118 long ident = Binder.clearCallingIdentity();
1119 try {
1120 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001121 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001122 return;
1123 }
1124
1125 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001126 mImeWindowVis = vis;
1127 mBackDisposition = backDisposition;
1128 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001129 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1130 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1131 final PackageManager pm = mContext.getPackageManager();
1132 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1133 final CharSequence title = mRes.getText(
1134 com.android.internal.R.string.select_input_method);
1135 mImeSwitcherNotification.setLatestEventInfo(
1136 mContext, title, label, mImeSwitchPendingIntent);
1137 mNotificationManager.notify(
1138 com.android.internal.R.string.select_input_method,
1139 mImeSwitcherNotification);
1140 mNotificationShown = true;
1141 } else {
1142 if (mNotificationShown) {
1143 mNotificationManager.cancel(
1144 com.android.internal.R.string.select_input_method);
1145 mNotificationShown = false;
1146 }
1147 }
satok06487a52010-10-29 11:37:18 +09001148 }
1149 } finally {
1150 Binder.restoreCallingIdentity(ident);
1151 }
1152 }
1153
satoke7c6998e2011-06-03 17:57:59 +09001154 @Override
satokf9f01002011-05-19 21:31:50 +09001155 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1156 synchronized (mMethodMap) {
1157 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1158 for (int i = 0; i < spans.length; ++i) {
1159 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001160 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001161 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001162 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001163 }
1164 }
1165 }
1166 }
1167
satoke7c6998e2011-06-03 17:57:59 +09001168 @Override
satokf9f01002011-05-19 21:31:50 +09001169 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1170 synchronized (mMethodMap) {
1171 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1172 // TODO: Do not send the intent if the process of the targetImi is already dead.
1173 if (targetImi != null) {
1174 final String[] suggestions = span.getSuggestions();
1175 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001176 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001177 final Intent intent = new Intent();
1178 // Ensures that only a class in the original IME package will receive the
1179 // notification.
satok42c5a162011-05-26 16:46:14 +09001180 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001181 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1182 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1183 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1184 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1185 mContext.sendBroadcast(intent);
1186 return true;
1187 }
1188 }
1189 return false;
1190 }
1191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001193 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1194 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1195 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1196 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001198 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001199 // There is no input method selected, try to choose new applicable input method.
1200 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1201 id = Settings.Secure.getString(mContext.getContentResolver(),
1202 Settings.Secure.DEFAULT_INPUT_METHOD);
1203 }
1204 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 try {
satokab751aa2010-09-14 19:17:36 +09001206 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001208 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001209 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001210 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
satokf3db1af2010-11-23 13:34:33 +09001212 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001213 } else {
1214 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001215 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001216 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 }
1218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
satokab751aa2010-09-14 19:17:36 +09001220 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 InputMethodInfo info = mMethodMap.get(id);
1222 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001223 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001227 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001228 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1229 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001230 }
1231 if (subtype != mCurrentSubtype) {
1232 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001233 if (subtype != null) {
1234 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1235 }
satokcd7cd292010-11-20 15:46:23 +09001236 if (mCurMethod != null) {
1237 try {
satoke40dea02011-01-30 01:14:02 +09001238 final Configuration conf = mRes.getConfiguration();
1239 final boolean haveHardKeyboard = conf.keyboard
1240 != Configuration.KEYBOARD_NOKEYS;
1241 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001242 && conf.hardKeyboardHidden
1243 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001244 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1245 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1246 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001247 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1248 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001249 // If subtype is null, try to find the most applicable one from
1250 // getCurrentInputMethodSubtype.
1251 if (subtype == null) {
1252 subtype = getCurrentInputMethodSubtype();
1253 }
1254 mCurMethod.changeInputMethodSubtype(subtype);
1255 } catch (RemoteException e) {
1256 return;
satokab751aa2010-09-14 19:17:36 +09001257 }
1258 }
1259 }
1260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 return;
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 final long ident = Binder.clearCallingIdentity();
1265 try {
satokab751aa2010-09-14 19:17:36 +09001266 // Set a subtype to this input method.
1267 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001268 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1269 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1270 // because mCurMethodId is stored as a history in
1271 // setSelectedInputMethodAndSubtypeLocked().
1272 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273
1274 if (ActivityManagerNative.isSystemReady()) {
1275 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001276 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 intent.putExtra("input_method_id", id);
1278 mContext.sendBroadcast(intent);
1279 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001280 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 } finally {
1282 Binder.restoreCallingIdentity(ident);
1283 }
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
satok42c5a162011-05-26 16:46:14 +09001286 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001287 public boolean showSoftInput(IInputMethodClient client, int flags,
1288 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001289 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 long ident = Binder.clearCallingIdentity();
1291 try {
1292 synchronized (mMethodMap) {
1293 if (mCurClient == null || client == null
1294 || mCurClient.client.asBinder() != client.asBinder()) {
1295 try {
1296 // We need to check if this is the current client with
1297 // focus in the window manager, to allow this call to
1298 // be made before input is started in it.
1299 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001300 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001301 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
1303 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001304 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001309 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311 } finally {
1312 Binder.restoreCallingIdentity(ident);
1313 }
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Project4df24232009-03-05 14:34:35 -08001316 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 mShowRequested = true;
1318 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1319 mShowExplicitlyRequested = true;
1320 }
1321 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1322 mShowExplicitlyRequested = true;
1323 mShowForced = true;
1324 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
Dianne Hackborncc278702009-09-02 23:07:23 -07001326 if (!mSystemReady) {
1327 return false;
1328 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001329
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001332 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1333 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1334 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 } else if (mHaveConnection && SystemClock.uptimeMillis()
1338 < (mLastBindTime+TIME_TO_RECONNECT)) {
1339 // The client has asked to have the input method shown, but
1340 // we have been sitting here too long with a connection to the
1341 // service and no interface received, so let's disconnect/connect
1342 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 SystemClock.uptimeMillis()-mLastBindTime,1);
1345 mContext.unbindService(this);
1346 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
The Android Open Source Project4df24232009-03-05 14:34:35 -08001349 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
satok42c5a162011-05-26 16:46:14 +09001352 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001353 public boolean hideSoftInput(IInputMethodClient client, int flags,
1354 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001355 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 long ident = Binder.clearCallingIdentity();
1357 try {
1358 synchronized (mMethodMap) {
1359 if (mCurClient == null || client == null
1360 || mCurClient.client.asBinder() != client.asBinder()) {
1361 try {
1362 // We need to check if this is the current client with
1363 // focus in the window manager, to allow this call to
1364 // be made before input is started in it.
1365 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001366 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1367 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001368 mImeWindowVis = 0;
1369 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1370 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001371 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001374 mImeWindowVis = 0;
1375 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001376 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 }
1378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
Joe Onorato8a9b2202010-02-26 18:56:32 -08001380 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001381 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 } finally {
1384 Binder.restoreCallingIdentity(ident);
1385 }
1386 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001387
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1390 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001391 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001393 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001396 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001402 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1403 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1404 res = true;
1405 } else {
1406 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 mInputShown = false;
1409 mShowRequested = false;
1410 mShowExplicitlyRequested = false;
1411 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001412 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
satok42c5a162011-05-26 16:46:14 +09001415 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001416 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1418 boolean first, int windowFlags) {
1419 long ident = Binder.clearCallingIdentity();
1420 try {
1421 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001422 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 + " viewHasFocus=" + viewHasFocus
1424 + " isTextEditor=" + isTextEditor
1425 + " softInputMode=#" + Integer.toHexString(softInputMode)
1426 + " first=" + first + " flags=#"
1427 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 if (mCurClient == null || client == null
1430 || mCurClient.client.asBinder() != client.asBinder()) {
1431 try {
1432 // We need to check if this is the current client with
1433 // focus in the window manager, to allow this call to
1434 // be made before input is started in it.
1435 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001436 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 return;
1438 }
1439 } catch (RemoteException e) {
1440 }
1441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001442
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001443 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001444 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001445 return;
1446 }
1447 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001448
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001449 // Should we auto-show the IME even if the caller has not
1450 // specified what should be done with it?
1451 // We only do this automatically if the window can resize
1452 // to accommodate the IME (so what the user sees will give
1453 // them good context without input information being obscured
1454 // by the IME) or if running on a large screen where there
1455 // is more room for the target window + IME.
1456 final boolean doAutoShow =
1457 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1458 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1459 || mRes.getConfiguration().isLayoutSizeAtLeast(
1460 Configuration.SCREENLAYOUT_SIZE_LARGE);
1461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1463 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001464 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1466 // There is no focus view, and this window will
1467 // be behind any soft input window, so hide the
1468 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001470 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001472 } else if (isTextEditor && doAutoShow && (softInputMode &
1473 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 // There is a focus view, and we are navigating forward
1475 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001476 // We only do this automatically if the window an resize
1477 // to accomodate the IME (so what the user sees will give
1478 // them good context without input information being obscured
1479 // by the IME) or if running on a large screen where there
1480 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001481 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001482 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484 break;
1485 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1486 // Do nothing.
1487 break;
1488 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1489 if ((softInputMode &
1490 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001492 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 }
1494 break;
1495 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001496 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001497 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 break;
1499 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1500 if ((softInputMode &
1501 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001502 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001503 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
1505 break;
1506 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001508 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 break;
1510 }
1511 }
1512 } finally {
1513 Binder.restoreCallingIdentity(ident);
1514 }
1515 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001516
satok42c5a162011-05-26 16:46:14 +09001517 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1519 synchronized (mMethodMap) {
1520 if (mCurClient == null || client == null
1521 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001522 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001523 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525
satok440aab52010-11-25 09:43:11 +09001526 // Always call subtype picker, because subtype picker is a superset of input method
1527 // picker.
satokab751aa2010-09-14 19:17:36 +09001528 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1529 }
1530 }
1531
satok42c5a162011-05-26 16:46:14 +09001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001534 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1535 }
1536
satok42c5a162011-05-26 16:46:14 +09001537 @Override
satok28203512010-11-24 11:06:49 +09001538 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1539 synchronized (mMethodMap) {
1540 if (subtype != null) {
1541 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1542 mMethodMap.get(id), subtype.hashCode()));
1543 } else {
1544 setInputMethod(token, id);
1545 }
1546 }
satokab751aa2010-09-14 19:17:36 +09001547 }
1548
satok42c5a162011-05-26 16:46:14 +09001549 @Override
satokb416a712010-11-25 20:42:14 +09001550 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001551 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001552 synchronized (mMethodMap) {
1553 if (mCurClient == null || client == null
1554 || mCurClient.client.asBinder() != client.asBinder()) {
1555 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1556 }
satok7fee71f2010-12-17 18:54:26 +09001557 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1558 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001559 }
1560 }
1561
satok4fc87d62011-05-20 16:13:43 +09001562 @Override
satok735cf382010-11-11 20:40:09 +09001563 public boolean switchToLastInputMethod(IBinder token) {
1564 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001565 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001566 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001567 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001568 lastImi = mMethodMap.get(lastIme.first);
1569 } else {
1570 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001571 }
satok4fc87d62011-05-20 16:13:43 +09001572 String targetLastImiId = null;
1573 int subtypeId = NOT_A_SUBTYPE_ID;
1574 if (lastIme != null && lastImi != null) {
1575 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1576 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1577 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1578 : mCurrentSubtype.hashCode();
1579 // If the last IME is the same as the current IME and the last subtype is not
1580 // defined, there is no need to switch to the last IME.
1581 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1582 targetLastImiId = lastIme.first;
1583 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1584 }
1585 }
1586
1587 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1588 // This is a safety net. If the currentSubtype can't be added to the history
1589 // and the framework couldn't find the last ime, we will make the last ime be
1590 // the most applicable enabled keyboard subtype of the system imes.
1591 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1592 if (enabled != null) {
1593 final int N = enabled.size();
1594 final String locale = mCurrentSubtype == null
1595 ? mRes.getConfiguration().locale.toString()
1596 : mCurrentSubtype.getLocale();
1597 for (int i = 0; i < N; ++i) {
1598 final InputMethodInfo imi = enabled.get(i);
1599 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1600 InputMethodSubtype keyboardSubtype =
1601 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1602 SUBTYPE_MODE_KEYBOARD, locale, true);
1603 if (keyboardSubtype != null) {
1604 targetLastImiId = imi.getId();
1605 subtypeId = getSubtypeIdFromHashCode(
1606 imi, keyboardSubtype.hashCode());
1607 if(keyboardSubtype.getLocale().equals(locale)) {
1608 break;
1609 }
1610 }
1611 }
1612 }
1613 }
1614 }
1615
1616 if (!TextUtils.isEmpty(targetLastImiId)) {
1617 if (DEBUG) {
1618 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1619 + ", from: " + mCurMethodId + ", " + subtypeId);
1620 }
1621 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1622 return true;
1623 } else {
1624 return false;
1625 }
satok735cf382010-11-11 20:40:09 +09001626 }
1627 }
1628
satoke7c6998e2011-06-03 17:57:59 +09001629 @Override
satok68f1b782011-04-11 14:26:04 +09001630 public InputMethodSubtype getLastInputMethodSubtype() {
1631 synchronized (mMethodMap) {
1632 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1633 // TODO: Handle the case of the last IME with no subtypes
1634 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1635 || TextUtils.isEmpty(lastIme.second)) return null;
1636 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1637 if (lastImi == null) return null;
1638 try {
1639 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001640 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1641 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1642 return null;
1643 }
1644 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001645 } catch (NumberFormatException e) {
1646 return null;
1647 }
1648 }
1649 }
1650
satoke7c6998e2011-06-03 17:57:59 +09001651 @Override
satok91e88122011-07-18 11:11:42 +09001652 public boolean setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
1653 // By this IPC call, only a process which shares the same uid with the IME can add
1654 // additional input method subtypes to the IME.
1655 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return false;
satoke7c6998e2011-06-03 17:57:59 +09001656 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001657 final InputMethodInfo imi = mMethodMap.get(imiId);
satoke7c6998e2011-06-03 17:57:59 +09001658 if (imi == null) return false;
satok91e88122011-07-18 11:11:42 +09001659 final PackageManager pm = mContext.getPackageManager();
1660 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1661 if (packageInfos != null) {
1662 final int packageNum = packageInfos.length;
1663 for (int i = 0; i < packageNum; ++i) {
1664 if (packageInfos[i].equals(imi.getPackageName())) {
1665 mFileManager.addInputMethodSubtypes(imi, subtypes);
1666 buildInputMethodListLocked(mMethodList, mMethodMap);
1667 return true;
1668 }
1669 }
1670 }
satoke7c6998e2011-06-03 17:57:59 +09001671 }
satok91e88122011-07-18 11:11:42 +09001672 return false;
satoke7c6998e2011-06-03 17:57:59 +09001673 }
1674
satok28203512010-11-24 11:06:49 +09001675 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 synchronized (mMethodMap) {
1677 if (token == null) {
1678 if (mContext.checkCallingOrSelfPermission(
1679 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1680 != PackageManager.PERMISSION_GRANTED) {
1681 throw new SecurityException(
1682 "Using null token requires permission "
1683 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1684 }
1685 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001686 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1687 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 return;
1689 }
1690
1691 long ident = Binder.clearCallingIdentity();
1692 try {
satokab751aa2010-09-14 19:17:36 +09001693 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 } finally {
1695 Binder.restoreCallingIdentity(ident);
1696 }
1697 }
1698 }
1699
satok42c5a162011-05-26 16:46:14 +09001700 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 public void hideMySoftInput(IBinder token, int flags) {
1702 synchronized (mMethodMap) {
1703 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001704 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1705 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 return;
1707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 long ident = Binder.clearCallingIdentity();
1709 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 hideCurrentInputLocked(flags, null);
1711 } finally {
1712 Binder.restoreCallingIdentity(ident);
1713 }
1714 }
1715 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001716
satok42c5a162011-05-26 16:46:14 +09001717 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001718 public void showMySoftInput(IBinder token, int flags) {
1719 synchronized (mMethodMap) {
1720 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001721 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1722 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001723 return;
1724 }
1725 long ident = Binder.clearCallingIdentity();
1726 try {
1727 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 } finally {
1729 Binder.restoreCallingIdentity(ident);
1730 }
1731 }
1732 }
1733
1734 void setEnabledSessionInMainThread(SessionState session) {
1735 if (mEnabledSession != session) {
1736 if (mEnabledSession != null) {
1737 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001738 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 mEnabledSession.method.setSessionEnabled(
1740 mEnabledSession.session, false);
1741 } catch (RemoteException e) {
1742 }
1743 }
1744 mEnabledSession = session;
1745 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001746 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 session.method.setSessionEnabled(
1748 session.session, true);
1749 } catch (RemoteException e) {
1750 }
1751 }
1752 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001753
satok42c5a162011-05-26 16:46:14 +09001754 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 public boolean handleMessage(Message msg) {
1756 HandlerCaller.SomeArgs args;
1757 switch (msg.what) {
1758 case MSG_SHOW_IM_PICKER:
1759 showInputMethodMenu();
1760 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001761
satokab751aa2010-09-14 19:17:36 +09001762 case MSG_SHOW_IM_SUBTYPE_PICKER:
1763 showInputMethodSubtypeMenu();
1764 return true;
1765
satok47a44912010-10-06 16:03:58 +09001766 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001767 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001768 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001769 return true;
1770
1771 case MSG_SHOW_IM_CONFIG:
1772 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001773 return true;
1774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 case MSG_UNBIND_INPUT:
1778 try {
1779 ((IInputMethod)msg.obj).unbindInput();
1780 } catch (RemoteException e) {
1781 // There is nothing interesting about the method dying.
1782 }
1783 return true;
1784 case MSG_BIND_INPUT:
1785 args = (HandlerCaller.SomeArgs)msg.obj;
1786 try {
1787 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1788 } catch (RemoteException e) {
1789 }
1790 return true;
1791 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1795 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 } catch (RemoteException e) {
1797 }
1798 return true;
1799 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001800 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 ((IInputMethod)args.arg1).hideSoftInput(0,
1803 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 } catch (RemoteException e) {
1805 }
1806 return true;
1807 case MSG_ATTACH_TOKEN:
1808 args = (HandlerCaller.SomeArgs)msg.obj;
1809 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001810 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1812 } catch (RemoteException e) {
1813 }
1814 return true;
1815 case MSG_CREATE_SESSION:
1816 args = (HandlerCaller.SomeArgs)msg.obj;
1817 try {
1818 ((IInputMethod)args.arg1).createSession(
1819 (IInputMethodCallback)args.arg2);
1820 } catch (RemoteException e) {
1821 }
1822 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 case MSG_START_INPUT:
1826 args = (HandlerCaller.SomeArgs)msg.obj;
1827 try {
1828 SessionState session = (SessionState)args.arg1;
1829 setEnabledSessionInMainThread(session);
1830 session.method.startInput((IInputContext)args.arg2,
1831 (EditorInfo)args.arg3);
1832 } catch (RemoteException e) {
1833 }
1834 return true;
1835 case MSG_RESTART_INPUT:
1836 args = (HandlerCaller.SomeArgs)msg.obj;
1837 try {
1838 SessionState session = (SessionState)args.arg1;
1839 setEnabledSessionInMainThread(session);
1840 session.method.restartInput((IInputContext)args.arg2,
1841 (EditorInfo)args.arg3);
1842 } catch (RemoteException e) {
1843 }
1844 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 case MSG_UNBIND_METHOD:
1849 try {
1850 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1851 } catch (RemoteException e) {
1852 // There is nothing interesting about the last client dying.
1853 }
1854 return true;
1855 case MSG_BIND_METHOD:
1856 args = (HandlerCaller.SomeArgs)msg.obj;
1857 try {
1858 ((IInputMethodClient)args.arg1).onBindMethod(
1859 (InputBindResult)args.arg2);
1860 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001861 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863 return true;
1864 }
1865 return false;
1866 }
1867
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001868 private boolean isSystemIme(InputMethodInfo inputMethod) {
1869 return (inputMethod.getServiceInfo().applicationInfo.flags
1870 & ApplicationInfo.FLAG_SYSTEM) != 0;
1871 }
1872
Ken Wakasa586f0512011-01-20 22:31:01 +09001873 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1874 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1875 final int subtypeCount = imi.getSubtypeCount();
1876 for (int i = 0; i < subtypeCount; ++i) {
1877 subtypes.add(imi.getSubtypeAt(i));
1878 }
1879 return subtypes;
1880 }
1881
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001882 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001883 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001884 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001885 // We'd prefer to fall back on a system IME, since that is safer.
1886 int i=enabled.size();
1887 while (i > 0) {
1888 i--;
1889 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1890 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1891 break;
1892 }
1893 }
satokab751aa2010-09-14 19:17:36 +09001894 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001895 if (DEBUG) {
1896 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1897 }
satok723a27e2010-11-11 14:58:11 +09001898 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001899 return true;
1900 }
1901
1902 return false;
1903 }
1904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1906 HashMap<String, InputMethodInfo> map) {
1907 list.clear();
1908 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001911 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001912 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1913 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1914 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1915 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916
1917 List<ResolveInfo> services = pm.queryIntentServices(
1918 new Intent(InputMethod.SERVICE_INTERFACE),
1919 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001920
satoke7c6998e2011-06-03 17:57:59 +09001921 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1922 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 for (int i = 0; i < services.size(); ++i) {
1924 ResolveInfo ri = services.get(i);
1925 ServiceInfo si = ri.serviceInfo;
1926 ComponentName compName = new ComponentName(si.packageName, si.name);
1927 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1928 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 + ": it does not require the permission "
1931 + android.Manifest.permission.BIND_INPUT_METHOD);
1932 continue;
1933 }
1934
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936
1937 try {
satoke7c6998e2011-06-03 17:57:59 +09001938 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001940 final String id = p.getId();
1941 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942
Amith Yamasanie861ec12010-03-24 21:39:27 -07001943 // System IMEs are enabled by default, unless there's a hard keyboard
1944 // and the system IME was explicitly disabled
1945 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1946 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001947 }
1948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001950 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001954 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001956 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001959
1960 String defaultIme = Settings.Secure.getString(mContext
1961 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001962 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001963 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001964 updateFromSettingsLocked();
1965 }
1966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001970
satokab751aa2010-09-14 19:17:36 +09001971 private void showInputMethodMenu() {
1972 showInputMethodMenuInternal(false);
1973 }
1974
1975 private void showInputMethodSubtypeMenu() {
1976 showInputMethodMenuInternal(true);
1977 }
1978
satok217f5482010-12-15 05:19:19 +09001979 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001980 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001981 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001982 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1983 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001984 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001985 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001986 }
satok217f5482010-12-15 05:19:19 +09001987 mContext.startActivity(intent);
1988 }
1989
1990 private void showConfigureInputMethods() {
1991 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1992 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1993 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1994 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001995 mContext.startActivity(intent);
1996 }
1997
satokab751aa2010-09-14 19:17:36 +09001998 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001999 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 String lastInputMethodId = Settings.Secure.getString(context
2006 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002007 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002008 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002010 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002011 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2012 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002013 if (immis == null || immis.size() == 0) {
2014 return;
2015 }
2016
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002017 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018
Ken Wakasa761eb372011-03-04 19:06:18 +09002019 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2020 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2021 new Comparator<InputMethodInfo>() {
2022 @Override
2023 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2024 if (imi2 == null) return 0;
2025 if (imi1 == null) return 1;
2026 if (pm == null) {
2027 return imi1.getId().compareTo(imi2.getId());
2028 }
2029 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2030 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2031 return imiId1.toString().compareTo(imiId2.toString());
2032 }
2033 });
satok913a8922010-08-26 21:53:41 +09002034
Ken Wakasa761eb372011-03-04 19:06:18 +09002035 sortedImmis.putAll(immis);
2036
2037 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2038 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2039
2040 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002041 if (imi == null) continue;
2042 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002043 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002044 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2045 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002046 }
satokbb4aa062011-01-19 21:40:27 +09002047 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002048 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002049 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002050 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002051 if (DEBUG) {
2052 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2053 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002054 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002055 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2056 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2057 // We show all enabled IMEs and subtypes when an IME is shown.
2058 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002059 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002060 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002061 final String mode = subtype.getMode();
2062 title = TextUtils.concat(subtype.getDisplayName(context,
2063 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2064 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002065 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2066 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002067 // Removing this subtype from enabledSubtypeSet because we no longer
2068 // need to add an entry of this subtype to imList to avoid duplicated
2069 // entries.
2070 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002071 }
satokab751aa2010-09-14 19:17:36 +09002072 }
2073 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002074 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2075 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002076 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002077 }
satok913a8922010-08-26 21:53:41 +09002078
Ken Wakasa761eb372011-03-04 19:06:18 +09002079 final int N = imList.size();
2080 mItems = new CharSequence[N];
2081 for (int i = 0; i < N; ++i) {
2082 mItems[i] = imList.get(i).first;
2083 }
satokab751aa2010-09-14 19:17:36 +09002084 mIms = new InputMethodInfo[N];
2085 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002086 int checkedItem = 0;
2087 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002088 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002089 mIms[i] = value.first;
2090 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002091 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002092 int subtypeId = mSubtypeIds[i];
2093 if ((subtypeId == NOT_A_SUBTYPE_ID)
2094 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2095 || (subtypeId == lastInputMethodSubtypeId)) {
2096 checkedItem = i;
2097 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
satokab751aa2010-09-14 19:17:36 +09002100
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002101 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002102 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002103 public void onClick(DialogInterface dialog, int which) {
2104 hideInputMethodMenu();
2105 }
2106 };
satokd87c2592010-09-29 11:52:06 +09002107
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002108 TypedArray a = context.obtainStyledAttributes(null,
2109 com.android.internal.R.styleable.DialogPreference,
2110 com.android.internal.R.attr.alertDialogStyle, 0);
2111 mDialogBuilder = new AlertDialog.Builder(context)
2112 .setTitle(com.android.internal.R.string.select_input_method)
2113 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002114 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002115 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002117 }
2118 })
2119 .setIcon(a.getDrawable(
2120 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2121 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002122
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002123 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2124 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002125 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002126 public void onClick(DialogInterface dialog, int which) {
2127 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002128 if (mIms == null || mIms.length <= which
2129 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002130 return;
2131 }
2132 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002133 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002134 hideInputMethodMenu();
2135 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002136 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002137 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002138 subtypeId = NOT_A_SUBTYPE_ID;
2139 }
2140 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002141 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002144 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145
satokf90a33e2011-07-19 11:55:52 +09002146 if (showSubtypes && !(mKeyguardManager.isKeyguardLocked()
2147 && mKeyguardManager.isKeyguardSecure())) {
satok82beadf2010-12-27 19:03:06 +09002148 mDialogBuilder.setPositiveButton(
2149 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002150 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002151 @Override
satok7f35c8c2010-10-07 21:13:11 +09002152 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002153 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002154 }
2155 });
2156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002158 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 mSwitchingDialog.getWindow().setType(
2160 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002161 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 mSwitchingDialog.show();
2163 }
2164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002167 synchronized (mMethodMap) {
2168 hideInputMethodMenuLocked();
2169 }
2170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002171
The Android Open Source Project10592532009-03-18 17:39:46 -07002172 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002173 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174
The Android Open Source Project10592532009-03-18 17:39:46 -07002175 if (mSwitchingDialog != null) {
2176 mSwitchingDialog.dismiss();
2177 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002179
The Android Open Source Project10592532009-03-18 17:39:46 -07002180 mDialogBuilder = null;
2181 mItems = null;
2182 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002186
satok42c5a162011-05-26 16:46:14 +09002187 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 public boolean setInputMethodEnabled(String id, boolean enabled) {
2189 synchronized (mMethodMap) {
2190 if (mContext.checkCallingOrSelfPermission(
2191 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2192 != PackageManager.PERMISSION_GRANTED) {
2193 throw new SecurityException(
2194 "Requires permission "
2195 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2196 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 long ident = Binder.clearCallingIdentity();
2199 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002200 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 } finally {
2202 Binder.restoreCallingIdentity(ident);
2203 }
2204 }
2205 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002206
2207 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2208 // Make sure this is a valid input method.
2209 InputMethodInfo imm = mMethodMap.get(id);
2210 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002211 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002212 }
2213
satokd87c2592010-09-29 11:52:06 +09002214 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2215 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002216
satokd87c2592010-09-29 11:52:06 +09002217 if (enabled) {
2218 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2219 if (pair.first.equals(id)) {
2220 // We are enabling this input method, but it is already enabled.
2221 // Nothing to do. The previous state was enabled.
2222 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002223 }
2224 }
satokd87c2592010-09-29 11:52:06 +09002225 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2226 // Previous state was disabled.
2227 return false;
2228 } else {
2229 StringBuilder builder = new StringBuilder();
2230 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2231 builder, enabledInputMethodsList, id)) {
2232 // Disabled input method is currently selected, switch to another one.
2233 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2234 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002235 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2236 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2237 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002238 }
2239 // Previous state was enabled.
2240 return true;
2241 } else {
2242 // We are disabling the input method but it is already disabled.
2243 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002244 return false;
2245 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002246 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002247 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002248
satok57ffc002011-01-25 00:11:47 +09002249 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2250 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002251 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002252 }
2253
satok723a27e2010-11-11 14:58:11 +09002254 private void saveCurrentInputMethodAndSubtypeToHistory() {
2255 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2256 if (mCurrentSubtype != null) {
2257 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2258 }
satok57ffc002011-01-25 00:11:47 +09002259 if (canAddToLastInputMethod(mCurrentSubtype)) {
2260 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2261 }
satokab751aa2010-09-14 19:17:36 +09002262 }
2263
satok723a27e2010-11-11 14:58:11 +09002264 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2265 boolean setSubtypeOnly) {
2266 // Update the history of InputMethod and Subtype
2267 saveCurrentInputMethodAndSubtypeToHistory();
2268
2269 // Set Subtype here
2270 if (imi == null || subtypeId < 0) {
2271 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002272 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002273 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002274 if (subtypeId < imi.getSubtypeCount()) {
2275 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2276 mSettings.putSelectedSubtype(subtype.hashCode());
2277 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002278 } else {
2279 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2280 mCurrentSubtype = null;
2281 }
satokab751aa2010-09-14 19:17:36 +09002282 }
satok723a27e2010-11-11 14:58:11 +09002283
2284 if (!setSubtypeOnly) {
2285 // Set InputMethod here
2286 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2287 }
2288 }
2289
2290 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2291 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2292 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2293 // newDefaultIme is empty when there is no candidate for the selected IME.
2294 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2295 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2296 if (subtypeHashCode != null) {
2297 try {
2298 lastSubtypeId = getSubtypeIdFromHashCode(
2299 imi, Integer.valueOf(subtypeHashCode));
2300 } catch (NumberFormatException e) {
2301 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2302 }
2303 }
2304 }
2305 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002306 }
2307
2308 private int getSelectedInputMethodSubtypeId(String id) {
2309 InputMethodInfo imi = mMethodMap.get(id);
2310 if (imi == null) {
2311 return NOT_A_SUBTYPE_ID;
2312 }
satokab751aa2010-09-14 19:17:36 +09002313 int subtypeId;
2314 try {
2315 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2316 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2317 } catch (SettingNotFoundException e) {
2318 return NOT_A_SUBTYPE_ID;
2319 }
satok723a27e2010-11-11 14:58:11 +09002320 return getSubtypeIdFromHashCode(imi, subtypeId);
2321 }
2322
2323 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002324 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002325 final int subtypeCount = imi.getSubtypeCount();
2326 for (int i = 0; i < subtypeCount; ++i) {
2327 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002328 if (subtypeHashCode == ims.hashCode()) {
2329 return i;
2330 }
satokab751aa2010-09-14 19:17:36 +09002331 }
2332 }
2333 return NOT_A_SUBTYPE_ID;
2334 }
2335
satokdf31ae62011-01-15 06:19:44 +09002336 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2337 Resources res, List<InputMethodSubtype> subtypes) {
2338 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002339 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2340 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2341 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002342 final int N = subtypes.size();
2343 boolean containsKeyboardSubtype = false;
2344 for (int i = 0; i < N; ++i) {
2345 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002346 final String locale = subtype.getLocale();
2347 final String mode = subtype.getMode();
2348 // When system locale starts with subtype's locale, that subtype will be applicable
2349 // for system locale
2350 // For instance, it's clearly applicable for cases like system locale = en_US and
2351 // subtype = en, but it is not necessarily considered applicable for cases like system
2352 // locale = en and subtype = en_US.
2353 // We just call systemLocale.startsWith(locale) in this function because there is no
2354 // need to find applicable subtypes aggressively unlike
2355 // findLastResortApplicableSubtypeLocked.
2356 if (systemLocale.startsWith(locale)) {
2357 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2358 // If more applicable subtypes are contained, skip.
2359 if (applicableSubtype != null
2360 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2361 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002362 if (!containsKeyboardSubtype
2363 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2364 containsKeyboardSubtype = true;
2365 }
2366 }
2367 }
satok4a28bde2011-06-29 21:03:40 +09002368 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002369 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002370 if (!containsKeyboardSubtype) {
2371 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002372 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002373 if (lastResortKeyboardSubtype != null) {
2374 applicableSubtypes.add(lastResortKeyboardSubtype);
2375 }
2376 }
2377 return applicableSubtypes;
2378 }
2379
satok4e4569d2010-11-19 18:45:53 +09002380 /**
2381 * If there are no selected subtypes, tries finding the most applicable one according to the
2382 * given locale.
2383 * @param subtypes this function will search the most applicable subtype in subtypes
2384 * @param mode subtypes will be filtered by mode
2385 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002386 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2387 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002388 * @return the most applicable subtypeId
2389 */
satokdf31ae62011-01-15 06:19:44 +09002390 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2391 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002392 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002393 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002394 return null;
satok8fbb1e82010-11-02 23:15:58 +09002395 }
satok4e4569d2010-11-19 18:45:53 +09002396 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002397 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002398 }
satok8fbb1e82010-11-02 23:15:58 +09002399 final String language = locale.substring(0, 2);
2400 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002401 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002402 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002403 final int N = subtypes.size();
2404 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002405 InputMethodSubtype subtype = subtypes.get(i);
2406 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002407 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2408 // and all subtypes with all modes can be candidates.
2409 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002410 if (firstMatchedModeSubtype == null) {
2411 firstMatchedModeSubtype = subtype;
2412 }
satok9ef02832010-11-04 21:17:48 +09002413 if (locale.equals(subtypeLocale)) {
2414 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002415 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002416 break;
2417 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2418 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002419 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002420 partialMatchFound = true;
2421 }
satok8fbb1e82010-11-02 23:15:58 +09002422 }
2423 }
2424
satok7599a7f2010-12-22 13:45:23 +09002425 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2426 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002427 }
2428
satok8fbb1e82010-11-02 23:15:58 +09002429 // The first subtype applicable to the system locale will be defined as the most applicable
2430 // subtype.
2431 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002432 if (applicableSubtype != null) {
2433 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2434 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2435 }
satok8fbb1e82010-11-02 23:15:58 +09002436 }
satokcd7cd292010-11-20 15:46:23 +09002437 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002438 }
2439
satok4e4569d2010-11-19 18:45:53 +09002440 // If there are no selected shortcuts, tries finding the most applicable ones.
2441 private Pair<InputMethodInfo, InputMethodSubtype>
2442 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2443 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2444 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002445 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002446 boolean foundInSystemIME = false;
2447
2448 // Search applicable subtype for each InputMethodInfo
2449 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002450 final String imiId = imi.getId();
2451 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2452 continue;
2453 }
satokcd7cd292010-11-20 15:46:23 +09002454 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002455 final List<InputMethodSubtype> enabledSubtypes =
2456 getEnabledInputMethodSubtypeList(imi, true);
2457 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002458 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002459 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002460 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002461 }
satokdf31ae62011-01-15 06:19:44 +09002462 // 2. Search by the system locale from enabledSubtypes.
2463 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002464 if (subtype == null) {
2465 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002466 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002467 }
satok7599a7f2010-12-22 13:45:23 +09002468 // 4. Search by the current subtype's locale from all subtypes.
2469 if (subtype == null && mCurrentSubtype != null) {
2470 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002471 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002472 }
2473 // 5. Search by the system locale from all subtypes.
2474 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002475 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002476 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002477 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002478 }
satokcd7cd292010-11-20 15:46:23 +09002479 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002480 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002481 // The current input method is the most applicable IME.
2482 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002483 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002484 break;
satok7599a7f2010-12-22 13:45:23 +09002485 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002486 // The system input method is 2nd applicable IME.
2487 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002488 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002489 if ((imi.getServiceInfo().applicationInfo.flags
2490 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2491 foundInSystemIME = true;
2492 }
satok4e4569d2010-11-19 18:45:53 +09002493 }
2494 }
2495 }
2496 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002497 if (mostApplicableIMI != null) {
2498 Slog.w(TAG, "Most applicable shortcut input method was:"
2499 + mostApplicableIMI.getId());
2500 if (mostApplicableSubtype != null) {
2501 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2502 + "," + mostApplicableSubtype.getMode() + ","
2503 + mostApplicableSubtype.getLocale());
2504 }
2505 }
satok4e4569d2010-11-19 18:45:53 +09002506 }
satokcd7cd292010-11-20 15:46:23 +09002507 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002508 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002509 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002510 } else {
2511 return null;
2512 }
2513 }
2514
satokab751aa2010-09-14 19:17:36 +09002515 /**
2516 * @return Return the current subtype of this input method.
2517 */
satok42c5a162011-05-26 16:46:14 +09002518 @Override
satokab751aa2010-09-14 19:17:36 +09002519 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002520 boolean subtypeIsSelected = false;
2521 try {
2522 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2523 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2524 } catch (SettingNotFoundException e) {
2525 }
satok3ef8b292010-11-23 06:06:29 +09002526 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002527 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002528 String lastInputMethodId = Settings.Secure.getString(
2529 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002530 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2531 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002532 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2533 if (imi != null) {
2534 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002535 // the most applicable subtype from explicitly or implicitly enabled
2536 // subtypes.
2537 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2538 getEnabledInputMethodSubtypeList(imi, true);
2539 // If there is only one explicitly or implicitly enabled subtype,
2540 // just returns it.
2541 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2542 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2543 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2544 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2545 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2546 SUBTYPE_MODE_KEYBOARD, null, true);
2547 if (mCurrentSubtype == null) {
2548 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2549 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2550 true);
2551 }
2552 }
satok4e4569d2010-11-19 18:45:53 +09002553 }
satokcd7cd292010-11-20 15:46:23 +09002554 } else {
satok3ef8b292010-11-23 06:06:29 +09002555 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002556 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002557 }
satok8fbb1e82010-11-02 23:15:58 +09002558 }
satok3ef8b292010-11-23 06:06:29 +09002559 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002560 }
satokab751aa2010-09-14 19:17:36 +09002561 }
2562
satokf3db1af2010-11-23 13:34:33 +09002563 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2564 InputMethodSubtype subtype) {
2565 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2566 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2567 } else {
2568 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2569 subtypes.add(subtype);
2570 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2571 }
2572 }
2573
satok4e4569d2010-11-19 18:45:53 +09002574 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002575 @Override
satok4e4569d2010-11-19 18:45:53 +09002576 public List getShortcutInputMethodsAndSubtypes() {
2577 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002578 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002579 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002580 // If there are no selected shortcut subtypes, the framework will try to find
2581 // the most applicable subtype from all subtypes whose mode is
2582 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002583 Pair<InputMethodInfo, InputMethodSubtype> info =
2584 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2585 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002586 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002587 ret.add(info.first);
2588 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002589 }
satok3da92232011-01-11 22:46:30 +09002590 return ret;
satokf3db1af2010-11-23 13:34:33 +09002591 }
satokf3db1af2010-11-23 13:34:33 +09002592 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2593 ret.add(imi);
2594 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2595 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002596 }
2597 }
satokf3db1af2010-11-23 13:34:33 +09002598 return ret;
satok4e4569d2010-11-19 18:45:53 +09002599 }
2600 }
2601
satok42c5a162011-05-26 16:46:14 +09002602 @Override
satokb66d2872010-11-10 01:04:04 +09002603 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2604 synchronized (mMethodMap) {
2605 if (subtype != null && mCurMethodId != null) {
2606 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2607 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2608 if (subtypeId != NOT_A_SUBTYPE_ID) {
2609 setInputMethodLocked(mCurMethodId, subtypeId);
2610 return true;
2611 }
2612 }
2613 return false;
2614 }
2615 }
2616
satokd87c2592010-09-29 11:52:06 +09002617 /**
2618 * Utility class for putting and getting settings for InputMethod
2619 * TODO: Move all putters and getters of settings to this class.
2620 */
2621 private static class InputMethodSettings {
2622 // The string for enabled input method is saved as follows:
2623 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2624 private static final char INPUT_METHOD_SEPARATER = ':';
2625 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002626 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002627 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2628
satok723a27e2010-11-11 14:58:11 +09002629 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002630 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2631
satokdf31ae62011-01-15 06:19:44 +09002632 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002633 private final ContentResolver mResolver;
2634 private final HashMap<String, InputMethodInfo> mMethodMap;
2635 private final ArrayList<InputMethodInfo> mMethodList;
2636
2637 private String mEnabledInputMethodsStrCache;
2638
2639 private static void buildEnabledInputMethodsSettingString(
2640 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2641 String id = pair.first;
2642 ArrayList<String> subtypes = pair.second;
2643 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002644 // Inputmethod and subtypes are saved in the settings as follows:
2645 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2646 for (String subtypeId: subtypes) {
2647 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002648 }
2649 }
2650
2651 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002652 Resources res, ContentResolver resolver,
2653 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2654 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002655 mResolver = resolver;
2656 mMethodMap = methodMap;
2657 mMethodList = methodList;
2658 }
2659
2660 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2661 return createEnabledInputMethodListLocked(
2662 getEnabledInputMethodsAndSubtypeListLocked());
2663 }
2664
satok7f35c8c2010-10-07 21:13:11 +09002665 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002666 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2667 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002668 getEnabledInputMethodsAndSubtypeListLocked());
2669 }
2670
satok67ddf9c2010-11-17 09:45:54 +09002671 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2672 InputMethodInfo imi) {
2673 List<Pair<String, ArrayList<String>>> imsList =
2674 getEnabledInputMethodsAndSubtypeListLocked();
2675 ArrayList<InputMethodSubtype> enabledSubtypes =
2676 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002677 if (imi != null) {
2678 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2679 InputMethodInfo info = mMethodMap.get(imsPair.first);
2680 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002681 final int subtypeCount = info.getSubtypeCount();
2682 for (int i = 0; i < subtypeCount; ++i) {
2683 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002684 for (String s: imsPair.second) {
2685 if (String.valueOf(ims.hashCode()).equals(s)) {
2686 enabledSubtypes.add(ims);
2687 }
satok67ddf9c2010-11-17 09:45:54 +09002688 }
2689 }
satok884ef9a2010-11-18 10:39:46 +09002690 break;
satok67ddf9c2010-11-17 09:45:54 +09002691 }
satok67ddf9c2010-11-17 09:45:54 +09002692 }
2693 }
2694 return enabledSubtypes;
2695 }
2696
satokd87c2592010-09-29 11:52:06 +09002697 // At the initial boot, the settings for input methods are not set,
2698 // so we need to enable IME in that case.
2699 public void enableAllIMEsIfThereIsNoEnabledIME() {
2700 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2701 StringBuilder sb = new StringBuilder();
2702 final int N = mMethodList.size();
2703 for (int i = 0; i < N; i++) {
2704 InputMethodInfo imi = mMethodList.get(i);
2705 Slog.i(TAG, "Adding: " + imi.getId());
2706 if (i > 0) sb.append(':');
2707 sb.append(imi.getId());
2708 }
2709 putEnabledInputMethodsStr(sb.toString());
2710 }
2711 }
2712
satokbb4aa062011-01-19 21:40:27 +09002713 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002714 ArrayList<Pair<String, ArrayList<String>>> imsList
2715 = new ArrayList<Pair<String, ArrayList<String>>>();
2716 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2717 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2718 return imsList;
2719 }
satok723a27e2010-11-11 14:58:11 +09002720 mInputMethodSplitter.setString(enabledInputMethodsStr);
2721 while (mInputMethodSplitter.hasNext()) {
2722 String nextImsStr = mInputMethodSplitter.next();
2723 mSubtypeSplitter.setString(nextImsStr);
2724 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002725 ArrayList<String> subtypeHashes = new ArrayList<String>();
2726 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002727 String imeId = mSubtypeSplitter.next();
2728 while (mSubtypeSplitter.hasNext()) {
2729 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002730 }
2731 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2732 }
2733 }
2734 return imsList;
2735 }
2736
2737 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2738 if (reloadInputMethodStr) {
2739 getEnabledInputMethodsStr();
2740 }
2741 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2742 // Add in the newly enabled input method.
2743 putEnabledInputMethodsStr(id);
2744 } else {
2745 putEnabledInputMethodsStr(
2746 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2747 }
2748 }
2749
2750 /**
2751 * Build and put a string of EnabledInputMethods with removing specified Id.
2752 * @return the specified id was removed or not.
2753 */
2754 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2755 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2756 boolean isRemoved = false;
2757 boolean needsAppendSeparator = false;
2758 for (Pair<String, ArrayList<String>> ims: imsList) {
2759 String curId = ims.first;
2760 if (curId.equals(id)) {
2761 // We are disabling this input method, and it is
2762 // currently enabled. Skip it to remove from the
2763 // new list.
2764 isRemoved = true;
2765 } else {
2766 if (needsAppendSeparator) {
2767 builder.append(INPUT_METHOD_SEPARATER);
2768 } else {
2769 needsAppendSeparator = true;
2770 }
2771 buildEnabledInputMethodsSettingString(builder, ims);
2772 }
2773 }
2774 if (isRemoved) {
2775 // Update the setting with the new list of input methods.
2776 putEnabledInputMethodsStr(builder.toString());
2777 }
2778 return isRemoved;
2779 }
2780
2781 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2782 List<Pair<String, ArrayList<String>>> imsList) {
2783 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2784 for (Pair<String, ArrayList<String>> ims: imsList) {
2785 InputMethodInfo info = mMethodMap.get(ims.first);
2786 if (info != null) {
2787 res.add(info);
2788 }
2789 }
2790 return res;
2791 }
2792
satok7f35c8c2010-10-07 21:13:11 +09002793 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002794 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002795 List<Pair<String, ArrayList<String>>> imsList) {
2796 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2797 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2798 for (Pair<String, ArrayList<String>> ims : imsList) {
2799 InputMethodInfo info = mMethodMap.get(ims.first);
2800 if (info != null) {
2801 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2802 }
2803 }
2804 return res;
2805 }
2806
satokd87c2592010-09-29 11:52:06 +09002807 private void putEnabledInputMethodsStr(String str) {
2808 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2809 mEnabledInputMethodsStrCache = str;
2810 }
2811
2812 private String getEnabledInputMethodsStr() {
2813 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2814 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002815 if (DEBUG) {
2816 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2817 }
satokd87c2592010-09-29 11:52:06 +09002818 return mEnabledInputMethodsStrCache;
2819 }
satok723a27e2010-11-11 14:58:11 +09002820
2821 private void saveSubtypeHistory(
2822 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2823 StringBuilder builder = new StringBuilder();
2824 boolean isImeAdded = false;
2825 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2826 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2827 newSubtypeId);
2828 isImeAdded = true;
2829 }
2830 for (Pair<String, String> ime: savedImes) {
2831 String imeId = ime.first;
2832 String subtypeId = ime.second;
2833 if (TextUtils.isEmpty(subtypeId)) {
2834 subtypeId = NOT_A_SUBTYPE_ID_STR;
2835 }
2836 if (isImeAdded) {
2837 builder.append(INPUT_METHOD_SEPARATER);
2838 } else {
2839 isImeAdded = true;
2840 }
2841 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2842 subtypeId);
2843 }
2844 // Remove the last INPUT_METHOD_SEPARATER
2845 putSubtypeHistoryStr(builder.toString());
2846 }
2847
2848 public void addSubtypeToHistory(String imeId, String subtypeId) {
2849 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2850 for (Pair<String, String> ime: subtypeHistory) {
2851 if (ime.first.equals(imeId)) {
2852 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002853 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002854 + ime.second);
2855 }
2856 // We should break here
2857 subtypeHistory.remove(ime);
2858 break;
2859 }
2860 }
satokbb4aa062011-01-19 21:40:27 +09002861 if (DEBUG) {
2862 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2863 }
satok723a27e2010-11-11 14:58:11 +09002864 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2865 }
2866
2867 private void putSubtypeHistoryStr(String str) {
2868 if (DEBUG) {
2869 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2870 }
2871 Settings.Secure.putString(
2872 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2873 }
2874
2875 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2876 // Gets the first one from the history
2877 return getLastSubtypeForInputMethodLockedInternal(null);
2878 }
2879
2880 public String getLastSubtypeForInputMethodLocked(String imeId) {
2881 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2882 if (ime != null) {
2883 return ime.second;
2884 } else {
2885 return null;
2886 }
2887 }
2888
2889 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2890 List<Pair<String, ArrayList<String>>> enabledImes =
2891 getEnabledInputMethodsAndSubtypeListLocked();
2892 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002893 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002894 final String imeInTheHistory = imeAndSubtype.first;
2895 // If imeId is empty, returns the first IME and subtype in the history
2896 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2897 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002898 final String subtypeHashCode =
2899 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2900 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002901 if (!TextUtils.isEmpty(subtypeHashCode)) {
2902 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002903 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002904 }
2905 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2906 }
2907 }
2908 }
2909 if (DEBUG) {
2910 Slog.d(TAG, "No enabled IME found in the history");
2911 }
2912 return null;
2913 }
2914
satokdf31ae62011-01-15 06:19:44 +09002915 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002916 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2917 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2918 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002919 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2920 if (explicitlyEnabledSubtypes.size() == 0) {
2921 // If there are no explicitly enabled subtypes, applicable subtypes are
2922 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002923 InputMethodInfo ime = mMethodMap.get(imeId);
2924 // If IME is enabled and no subtypes are enabled, applicable subtypes
2925 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002926 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002927 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002928 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002929 if (implicitlySelectedSubtypes != null) {
2930 final int N = implicitlySelectedSubtypes.size();
2931 for (int i = 0; i < N; ++i) {
2932 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2933 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2934 return subtypeHashCode;
2935 }
2936 }
2937 }
2938 }
2939 } else {
satokf6cafb62011-01-17 16:29:02 +09002940 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002941 if (s.equals(subtypeHashCode)) {
2942 // If both imeId and subtypeId are enabled, return subtypeId.
2943 return s;
2944 }
satok723a27e2010-11-11 14:58:11 +09002945 }
2946 }
2947 // If imeId was enabled but subtypeId was disabled.
2948 return NOT_A_SUBTYPE_ID_STR;
2949 }
2950 }
2951 // If both imeId and subtypeId are disabled, return null
2952 return null;
2953 }
2954
2955 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2956 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2957 final String subtypeHistoryStr = getSubtypeHistoryStr();
2958 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2959 return imsList;
2960 }
2961 mInputMethodSplitter.setString(subtypeHistoryStr);
2962 while (mInputMethodSplitter.hasNext()) {
2963 String nextImsStr = mInputMethodSplitter.next();
2964 mSubtypeSplitter.setString(nextImsStr);
2965 if (mSubtypeSplitter.hasNext()) {
2966 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2967 // The first element is ime id.
2968 String imeId = mSubtypeSplitter.next();
2969 while (mSubtypeSplitter.hasNext()) {
2970 subtypeId = mSubtypeSplitter.next();
2971 break;
2972 }
2973 imsList.add(new Pair<String, String>(imeId, subtypeId));
2974 }
2975 }
2976 return imsList;
2977 }
2978
2979 private String getSubtypeHistoryStr() {
2980 if (DEBUG) {
2981 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2982 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2983 }
2984 return Settings.Secure.getString(
2985 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2986 }
2987
2988 public void putSelectedInputMethod(String imeId) {
2989 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2990 }
2991
2992 public void putSelectedSubtype(int subtypeId) {
2993 Settings.Secure.putInt(
2994 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2995 }
satokd87c2592010-09-29 11:52:06 +09002996 }
2997
satoke7c6998e2011-06-03 17:57:59 +09002998 private static class InputMethodFileManager {
2999 private static final String SYSTEM_PATH = "system";
3000 private static final String INPUT_METHOD_PATH = "inputmethod";
3001 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3002 private static final String NODE_SUBTYPES = "subtypes";
3003 private static final String NODE_SUBTYPE = "subtype";
3004 private static final String NODE_IMI = "imi";
3005 private static final String ATTR_ID = "id";
3006 private static final String ATTR_LABEL = "label";
3007 private static final String ATTR_ICON = "icon";
3008 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3009 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3010 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3011 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3012 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3013 private final HashMap<String, InputMethodInfo> mMethodMap;
3014 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3015 new HashMap<String, List<InputMethodSubtype>>();
3016 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3017 if (methodMap == null) {
3018 throw new NullPointerException("methodMap is null");
3019 }
3020 mMethodMap = methodMap;
3021 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3022 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3023 if (!inputMethodDir.mkdirs()) {
3024 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3025 }
3026 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3027 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3028 if (!subtypeFile.exists()) {
3029 // If "subtypes.xml" doesn't exist, create a blank file.
3030 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3031 methodMap);
3032 } else {
3033 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3034 }
3035 }
3036
3037 private void deleteAllInputMethodSubtypes(String imiId) {
3038 synchronized (mMethodMap) {
3039 mSubtypesMap.remove(imiId);
3040 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3041 mMethodMap);
3042 }
3043 }
3044
3045 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003046 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003047 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003048 final HashSet<InputMethodSubtype> existingSubtypes =
3049 new HashSet<InputMethodSubtype>();
3050 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3051 existingSubtypes.add(imi.getSubtypeAt(i));
3052 }
3053
satoke7c6998e2011-06-03 17:57:59 +09003054 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3055 final int N = additionalSubtypes.length;
3056 for (int i = 0; i < N; ++i) {
3057 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003058 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003059 subtypes.add(subtype);
3060 }
3061 }
satok4a28bde2011-06-29 21:03:40 +09003062 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003063 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3064 mMethodMap);
3065 }
3066 }
3067
3068 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3069 synchronized (mMethodMap) {
3070 return mSubtypesMap;
3071 }
3072 }
3073
3074 private static void writeAdditionalInputMethodSubtypes(
3075 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3076 HashMap<String, InputMethodInfo> methodMap) {
3077 // Safety net for the case that this function is called before methodMap is set.
3078 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3079 FileOutputStream fos = null;
3080 try {
3081 fos = subtypesFile.startWrite();
3082 final XmlSerializer out = new FastXmlSerializer();
3083 out.setOutput(fos, "utf-8");
3084 out.startDocument(null, true);
3085 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3086 out.startTag(null, NODE_SUBTYPES);
3087 for (String imiId : allSubtypes.keySet()) {
3088 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3089 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3090 continue;
3091 }
3092 out.startTag(null, NODE_IMI);
3093 out.attribute(null, ATTR_ID, imiId);
3094 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3095 final int N = subtypesList.size();
3096 for (int i = 0; i < N; ++i) {
3097 final InputMethodSubtype subtype = subtypesList.get(i);
3098 out.startTag(null, NODE_SUBTYPE);
3099 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3100 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3101 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3102 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3103 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3104 out.attribute(null, ATTR_IS_AUXILIARY,
3105 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3106 out.endTag(null, NODE_SUBTYPE);
3107 }
3108 out.endTag(null, NODE_IMI);
3109 }
3110 out.endTag(null, NODE_SUBTYPES);
3111 out.endDocument();
3112 subtypesFile.finishWrite(fos);
3113 } catch (java.io.IOException e) {
3114 Slog.w(TAG, "Error writing subtypes", e);
3115 if (fos != null) {
3116 subtypesFile.failWrite(fos);
3117 }
3118 }
3119 }
3120
3121 private static void readAdditionalInputMethodSubtypes(
3122 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3123 if (allSubtypes == null || subtypesFile == null) return;
3124 allSubtypes.clear();
3125 FileInputStream fis = null;
3126 try {
3127 fis = subtypesFile.openRead();
3128 final XmlPullParser parser = Xml.newPullParser();
3129 parser.setInput(fis, null);
3130 int type = parser.getEventType();
3131 // Skip parsing until START_TAG
3132 while ((type = parser.next()) != XmlPullParser.START_TAG
3133 && type != XmlPullParser.END_DOCUMENT) {}
3134 String firstNodeName = parser.getName();
3135 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3136 throw new XmlPullParserException("Xml doesn't start with subtypes");
3137 }
3138 final int depth =parser.getDepth();
3139 String currentImiId = null;
3140 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3141 while (((type = parser.next()) != XmlPullParser.END_TAG
3142 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3143 if (type != XmlPullParser.START_TAG)
3144 continue;
3145 final String nodeName = parser.getName();
3146 if (NODE_IMI.equals(nodeName)) {
3147 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3148 if (TextUtils.isEmpty(currentImiId)) {
3149 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3150 continue;
3151 }
3152 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3153 allSubtypes.put(currentImiId, tempSubtypesArray);
3154 } else if (NODE_SUBTYPE.equals(nodeName)) {
3155 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3156 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3157 continue;
3158 }
3159 final int icon = Integer.valueOf(
3160 parser.getAttributeValue(null, ATTR_ICON));
3161 final int label = Integer.valueOf(
3162 parser.getAttributeValue(null, ATTR_LABEL));
3163 final String imeSubtypeLocale =
3164 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3165 final String imeSubtypeMode =
3166 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3167 final String imeSubtypeExtraValue =
3168 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003169 final boolean isAuxiliary = "1".equals(String.valueOf(
3170 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003171 final InputMethodSubtype subtype =
3172 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3173 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3174 tempSubtypesArray.add(subtype);
3175 }
3176 }
3177 } catch (XmlPullParserException e) {
3178 Slog.w(TAG, "Error reading subtypes: " + e);
3179 return;
3180 } catch (java.io.IOException e) {
3181 Slog.w(TAG, "Error reading subtypes: " + e);
3182 return;
3183 } catch (NumberFormatException e) {
3184 Slog.w(TAG, "Error reading subtypes: " + e);
3185 return;
3186 } finally {
3187 if (fis != null) {
3188 try {
3189 fis.close();
3190 } catch (java.io.IOException e1) {
3191 Slog.w(TAG, "Failed to close.");
3192 }
3193 }
3194 }
3195 }
3196 }
3197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 @Override
3201 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3202 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3203 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3206 + Binder.getCallingPid()
3207 + ", uid=" + Binder.getCallingUid());
3208 return;
3209 }
3210
3211 IInputMethod method;
3212 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 synchronized (mMethodMap) {
3217 p.println("Current Input Method Manager state:");
3218 int N = mMethodList.size();
3219 p.println(" Input Methods:");
3220 for (int i=0; i<N; i++) {
3221 InputMethodInfo info = mMethodList.get(i);
3222 p.println(" InputMethod #" + i + ":");
3223 info.dump(p, " ");
3224 }
3225 p.println(" Clients:");
3226 for (ClientState ci : mClients.values()) {
3227 p.println(" Client " + ci + ":");
3228 p.println(" client=" + ci.client);
3229 p.println(" inputContext=" + ci.inputContext);
3230 p.println(" sessionRequested=" + ci.sessionRequested);
3231 p.println(" curSession=" + ci.curSession);
3232 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003233 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003235 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3236 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3238 + " mBoundToMethod=" + mBoundToMethod);
3239 p.println(" mCurToken=" + mCurToken);
3240 p.println(" mCurIntent=" + mCurIntent);
3241 method = mCurMethod;
3242 p.println(" mCurMethod=" + mCurMethod);
3243 p.println(" mEnabledSession=" + mEnabledSession);
3244 p.println(" mShowRequested=" + mShowRequested
3245 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3246 + " mShowForced=" + mShowForced
3247 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003248 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003250
Jeff Brownb88102f2010-09-08 11:49:43 -07003251 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 pw.flush();
3254 try {
3255 client.client.asBinder().dump(fd, args);
3256 } catch (RemoteException e) {
3257 p.println("Input method client dead: " + e);
3258 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003259 } else {
3260 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003262
Jeff Brownb88102f2010-09-08 11:49:43 -07003263 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 pw.flush();
3266 try {
3267 method.asBinder().dump(fd, args);
3268 } catch (RemoteException e) {
3269 p.println("Input method service dead: " + e);
3270 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003271 } else {
3272 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 }
3274 }
3275}