blob: 73996795205ed8acb70a310e0677e83d4683238a [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;
169 private final boolean mShowOngoingImeSwitcherForPhones;
170 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);
satok7cfc0ed2011-06-20 21:29:36 +0900541 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
542 com.android.internal.R.bool.show_ongoing_ime_switcher);
543
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;
Dianne Hackborncc278702009-09-02 23:07:23 -0700615 try {
616 startInputInnerLocked();
617 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800618 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700619 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700620 }
621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800623
satoke7c6998e2011-06-03 17:57:59 +0900624 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public List<InputMethodInfo> getInputMethodList() {
626 synchronized (mMethodMap) {
627 return new ArrayList<InputMethodInfo>(mMethodList);
628 }
629 }
630
satoke7c6998e2011-06-03 17:57:59 +0900631 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 public List<InputMethodInfo> getEnabledInputMethodList() {
633 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900634 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636 }
637
satokbb4aa062011-01-19 21:40:27 +0900638 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
639 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
640 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
641 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
642 for (InputMethodInfo imi: getEnabledInputMethodList()) {
643 enabledInputMethodAndSubtypes.put(
644 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
645 }
646 return enabledInputMethodAndSubtypes;
647 }
648
649 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
650 boolean allowsImplicitlySelectedSubtypes) {
651 if (imi == null && mCurMethodId != null) {
652 imi = mMethodMap.get(mCurMethodId);
653 }
satok7265d9b2011-02-14 15:47:30 +0900654 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900655 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900656 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
657 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900658 }
satok7265d9b2011-02-14 15:47:30 +0900659 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900660 }
661
satoke7c6998e2011-06-03 17:57:59 +0900662 @Override
satok16331c82010-12-20 23:48:46 +0900663 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
664 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900665 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900666 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900667 }
668 }
669
satoke7c6998e2011-06-03 17:57:59 +0900670 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 public void addClient(IInputMethodClient client,
672 IInputContext inputContext, int uid, int pid) {
673 synchronized (mMethodMap) {
674 mClients.put(client.asBinder(), new ClientState(client,
675 inputContext, uid, pid));
676 }
677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
satoke7c6998e2011-06-03 17:57:59 +0900679 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 public void removeClient(IInputMethodClient client) {
681 synchronized (mMethodMap) {
682 mClients.remove(client.asBinder());
683 }
684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 void executeOrSendMessage(IInterface target, Message msg) {
687 if (target.asBinder() instanceof Binder) {
688 mCaller.sendMessage(msg);
689 } else {
690 handleMessage(msg);
691 msg.recycle();
692 }
693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700695 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800697 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 + mCurClient.client.asBinder());
699 if (mBoundToMethod) {
700 mBoundToMethod = false;
701 if (mCurMethod != null) {
702 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
703 MSG_UNBIND_INPUT, mCurMethod));
704 }
705 }
706 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
707 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
708 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 // Call setActive(false) on the old client
711 try {
712 mCurClient.client.setActive(false);
713 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800714 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 + mCurClient.pid + " uid " + mCurClient.uid);
716 }
717 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800718
The Android Open Source Project10592532009-03-18 17:39:46 -0700719 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 private int getImeShowFlags() {
724 int flags = 0;
725 if (mShowForced) {
726 flags |= InputMethod.SHOW_FORCED
727 | InputMethod.SHOW_EXPLICIT;
728 } else if (mShowExplicitlyRequested) {
729 flags |= InputMethod.SHOW_EXPLICIT;
730 }
731 return flags;
732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 private int getAppShowFlags() {
735 int flags = 0;
736 if (mShowForced) {
737 flags |= InputMethodManager.SHOW_FORCED;
738 } else if (!mShowExplicitlyRequested) {
739 flags |= InputMethodManager.SHOW_IMPLICIT;
740 }
741 return flags;
742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
745 if (!mBoundToMethod) {
746 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
747 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
748 mBoundToMethod = true;
749 }
750 final SessionState session = mCurClient.curSession;
751 if (initial) {
752 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
753 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
754 } else {
755 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
756 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
757 }
758 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800759 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800760 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762 return needResult
763 ? new InputBindResult(session.session, mCurId, mCurSeq)
764 : null;
765 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 InputBindResult startInputLocked(IInputMethodClient client,
768 IInputContext inputContext, EditorInfo attribute,
769 boolean initial, boolean needResult) {
770 // If no method is currently selected, do nothing.
771 if (mCurMethodId == null) {
772 return mNoBinding;
773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 ClientState cs = mClients.get(client.asBinder());
776 if (cs == null) {
777 throw new IllegalArgumentException("unknown client "
778 + client.asBinder());
779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 try {
782 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
783 // Check with the window manager to make sure this client actually
784 // has a window with focus. If not, reject. This is thread safe
785 // because if the focus changes some time before or after, the
786 // next client receiving focus that has any interest in input will
787 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800788 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
790 return null;
791 }
792 } catch (RemoteException e) {
793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (mCurClient != cs) {
796 // If the client is changing, we need to switch over to the new
797 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700798 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800799 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 + cs.client.asBinder());
801
802 // If the screen is on, inform the new client it is active
803 if (mScreenOn) {
804 try {
805 cs.client.setActive(mScreenOn);
806 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800807 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 + cs.pid + " uid " + cs.uid);
809 }
810 }
811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 // Bump up the sequence for this client and attach it.
814 mCurSeq++;
815 if (mCurSeq <= 0) mCurSeq = 1;
816 mCurClient = cs;
817 mCurInputContext = inputContext;
818 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 // Check if the input method is changing.
821 if (mCurId != null && mCurId.equals(mCurMethodId)) {
822 if (cs.curSession != null) {
823 // Fast case: if we are already connected to the input method,
824 // then just return it.
825 return attachNewInputLocked(initial, needResult);
826 }
827 if (mHaveConnection) {
828 if (mCurMethod != null) {
829 if (!cs.sessionRequested) {
830 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800831 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
833 MSG_CREATE_SESSION, mCurMethod,
834 new MethodCallback(mCurMethod)));
835 }
836 // Return to client, and we will get back with it when
837 // we have had a session made for it.
838 return new InputBindResult(null, mCurId, mCurSeq);
839 } else if (SystemClock.uptimeMillis()
840 < (mLastBindTime+TIME_TO_RECONNECT)) {
841 // In this case we have connected to the service, but
842 // don't yet have its interface. If it hasn't been too
843 // long since we did the connection, we'll return to
844 // the client and wait to get the service interface so
845 // we can report back. If it has been too long, we want
846 // to fall through so we can try a disconnect/reconnect
847 // to see if we can get back in touch with the service.
848 return new InputBindResult(null, mCurId, mCurSeq);
849 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800850 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
851 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 }
853 }
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700856 return startInputInnerLocked();
857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700859 InputBindResult startInputInnerLocked() {
860 if (mCurMethodId == null) {
861 return mNoBinding;
862 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800863
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 if (!mSystemReady) {
865 // If the system is not yet ready, we shouldn't be running third
866 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700867 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 InputMethodInfo info = mMethodMap.get(mCurMethodId);
871 if (info == null) {
872 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800874
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700875 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
878 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700879 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
880 com.android.internal.R.string.input_method_binding_label);
881 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
882 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
884 mLastBindTime = SystemClock.uptimeMillis();
885 mHaveConnection = true;
886 mCurId = info.getId();
887 mCurToken = new Binder();
888 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800889 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 mIWindowManager.addWindowToken(mCurToken,
891 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
892 } catch (RemoteException e) {
893 }
894 return new InputBindResult(null, mCurId, mCurSeq);
895 } else {
896 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800897 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 + mCurIntent);
899 }
900 return null;
901 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902
satoke7c6998e2011-06-03 17:57:59 +0900903 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 public InputBindResult startInput(IInputMethodClient client,
905 IInputContext inputContext, EditorInfo attribute,
906 boolean initial, boolean needResult) {
907 synchronized (mMethodMap) {
908 final long ident = Binder.clearCallingIdentity();
909 try {
910 return startInputLocked(client, inputContext, attribute,
911 initial, needResult);
912 } finally {
913 Binder.restoreCallingIdentity(ident);
914 }
915 }
916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
satoke7c6998e2011-06-03 17:57:59 +0900918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public void finishInput(IInputMethodClient client) {
920 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800921
satoke7c6998e2011-06-03 17:57:59 +0900922 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 public void onServiceConnected(ComponentName name, IBinder service) {
924 synchronized (mMethodMap) {
925 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
926 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700927 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800928 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700929 unbindCurrentMethodLocked(false);
930 return;
931 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800932 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700933 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
934 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800936 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700937 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700939 MSG_CREATE_SESSION, mCurMethod,
940 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942 }
943 }
944 }
945
946 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
947 synchronized (mMethodMap) {
948 if (mCurMethod != null && method != null
949 && mCurMethod.asBinder() == method.asBinder()) {
950 if (mCurClient != null) {
951 mCurClient.curSession = new SessionState(mCurClient,
952 method, session);
953 mCurClient.sessionRequested = false;
954 InputBindResult res = attachNewInputLocked(true, true);
955 if (res.method != null) {
956 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
957 MSG_BIND_METHOD, mCurClient.client, res));
958 }
959 }
960 }
961 }
962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700964 void unbindCurrentMethodLocked(boolean reportToClient) {
965 if (mHaveConnection) {
966 mContext.unbindService(this);
967 mHaveConnection = false;
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 if (mCurToken != null) {
971 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800972 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700973 mIWindowManager.removeWindowToken(mCurToken);
974 } catch (RemoteException e) {
975 }
976 mCurToken = null;
977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
The Android Open Source Project10592532009-03-18 17:39:46 -0700979 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700982 if (reportToClient && mCurClient != null) {
983 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
984 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
985 }
986 }
987
Devin Taylor0c33ed22010-02-23 13:26:46 -0600988 private void finishSession(SessionState sessionState) {
989 if (sessionState != null && sessionState.session != null) {
990 try {
991 sessionState.session.finishSession();
992 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700993 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600994 }
995 }
996 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700998 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 if (mCurMethod != null) {
1000 for (ClientState cs : mClients.values()) {
1001 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001002 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 cs.curSession = null;
1004 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001005
1006 finishSession(mEnabledSession);
1007 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 mCurMethod = null;
1009 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001010 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001012
satoke7c6998e2011-06-03 17:57:59 +09001013 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 public void onServiceDisconnected(ComponentName name) {
1015 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001016 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 + " mCurIntent=" + mCurIntent);
1018 if (mCurMethod != null && mCurIntent != null
1019 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001020 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 // We consider this to be a new bind attempt, since the system
1022 // should now try to restart the service for us.
1023 mLastBindTime = SystemClock.uptimeMillis();
1024 mShowRequested = mInputShown;
1025 mInputShown = false;
1026 if (mCurClient != null) {
1027 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1028 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1029 }
1030 }
1031 }
1032 }
1033
satokf9f01002011-05-19 21:31:50 +09001034 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001036 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 long ident = Binder.clearCallingIdentity();
1038 try {
1039 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001040 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 return;
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 synchronized (mMethodMap) {
1045 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001046 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001047 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001049 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001050 CharSequence contentDescription = null;
1051 try {
1052 PackageManager packageManager = mContext.getPackageManager();
1053 contentDescription = packageManager.getApplicationLabel(
1054 packageManager.getApplicationInfo(packageName, 0));
1055 } catch (NameNotFoundException nnfe) {
1056 /* ignore */
1057 }
1058 mStatusBar.setIcon("ime", packageName, iconId, 0,
1059 contentDescription != null ? contentDescription.toString() : null);
Joe Onorato0cbda992010-05-02 16:28:15 -07001060 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062 }
1063 } finally {
1064 Binder.restoreCallingIdentity(ident);
1065 }
1066 }
1067
satok7cfc0ed2011-06-20 21:29:36 +09001068 private boolean needsToShowImeSwitchOngoingNotification() {
1069 if (!mShowOngoingImeSwitcherForPhones) return false;
1070 synchronized (mMethodMap) {
1071 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1072 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001073 if (N > 2) return true;
1074 if (N < 1) return false;
1075 int nonAuxCount = 0;
1076 int auxCount = 0;
1077 InputMethodSubtype nonAuxSubtype = null;
1078 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001079 for(int i = 0; i < N; ++i) {
1080 final InputMethodInfo imi = imis.get(i);
1081 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1082 imi, true);
1083 final int subtypeCount = subtypes.size();
1084 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001085 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001086 } else {
1087 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001088 final InputMethodSubtype subtype = subtypes.get(j);
1089 if (!subtype.isAuxiliary()) {
1090 ++nonAuxCount;
1091 nonAuxSubtype = subtype;
1092 } else {
1093 ++auxCount;
1094 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001095 }
1096 }
1097 }
satok7cfc0ed2011-06-20 21:29:36 +09001098 }
satokb6359412011-06-28 17:47:41 +09001099 if (nonAuxCount > 1 || auxCount > 1) {
1100 return true;
1101 } else if (nonAuxCount == 1 && auxCount == 1) {
1102 if (nonAuxSubtype != null && auxSubtype != null
1103 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1104 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1105 return false;
1106 }
1107 return true;
1108 }
1109 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001110 }
satok7cfc0ed2011-06-20 21:29:36 +09001111 }
1112
satokf9f01002011-05-19 21:31:50 +09001113 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001114 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001115 int uid = Binder.getCallingUid();
1116 long ident = Binder.clearCallingIdentity();
1117 try {
1118 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001119 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001120 return;
1121 }
1122
1123 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001124 mImeWindowVis = vis;
1125 mBackDisposition = backDisposition;
1126 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001127 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1128 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1129 final PackageManager pm = mContext.getPackageManager();
1130 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1131 final CharSequence title = mRes.getText(
1132 com.android.internal.R.string.select_input_method);
1133 mImeSwitcherNotification.setLatestEventInfo(
1134 mContext, title, label, mImeSwitchPendingIntent);
1135 mNotificationManager.notify(
1136 com.android.internal.R.string.select_input_method,
1137 mImeSwitcherNotification);
1138 mNotificationShown = true;
1139 } else {
1140 if (mNotificationShown) {
1141 mNotificationManager.cancel(
1142 com.android.internal.R.string.select_input_method);
1143 mNotificationShown = false;
1144 }
1145 }
satok06487a52010-10-29 11:37:18 +09001146 }
1147 } finally {
1148 Binder.restoreCallingIdentity(ident);
1149 }
1150 }
1151
satoke7c6998e2011-06-03 17:57:59 +09001152 @Override
satokf9f01002011-05-19 21:31:50 +09001153 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1154 synchronized (mMethodMap) {
1155 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1156 for (int i = 0; i < spans.length; ++i) {
1157 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001158 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001159 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001160 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001161 }
1162 }
1163 }
1164 }
1165
satoke7c6998e2011-06-03 17:57:59 +09001166 @Override
satokf9f01002011-05-19 21:31:50 +09001167 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1168 synchronized (mMethodMap) {
1169 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1170 // TODO: Do not send the intent if the process of the targetImi is already dead.
1171 if (targetImi != null) {
1172 final String[] suggestions = span.getSuggestions();
1173 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001174 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001175 final Intent intent = new Intent();
1176 // Ensures that only a class in the original IME package will receive the
1177 // notification.
satok42c5a162011-05-26 16:46:14 +09001178 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001179 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1180 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1181 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1182 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1183 mContext.sendBroadcast(intent);
1184 return true;
1185 }
1186 }
1187 return false;
1188 }
1189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001191 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1192 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1193 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1194 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001196 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001197 // There is no input method selected, try to choose new applicable input method.
1198 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1199 id = Settings.Secure.getString(mContext.getContentResolver(),
1200 Settings.Secure.DEFAULT_INPUT_METHOD);
1201 }
1202 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 try {
satokab751aa2010-09-14 19:17:36 +09001204 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001207 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001208 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
satokf3db1af2010-11-23 13:34:33 +09001210 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001211 } else {
1212 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001213 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001214 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
satokab751aa2010-09-14 19:17:36 +09001218 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 InputMethodInfo info = mMethodMap.get(id);
1220 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001221 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001225 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001226 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1227 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001228 }
1229 if (subtype != mCurrentSubtype) {
1230 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001231 if (subtype != null) {
1232 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1233 }
satokcd7cd292010-11-20 15:46:23 +09001234 if (mCurMethod != null) {
1235 try {
satoke40dea02011-01-30 01:14:02 +09001236 final Configuration conf = mRes.getConfiguration();
1237 final boolean haveHardKeyboard = conf.keyboard
1238 != Configuration.KEYBOARD_NOKEYS;
1239 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001240 && conf.hardKeyboardHidden
1241 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001242 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1243 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1244 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001245 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1246 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001247 // If subtype is null, try to find the most applicable one from
1248 // getCurrentInputMethodSubtype.
1249 if (subtype == null) {
1250 subtype = getCurrentInputMethodSubtype();
1251 }
1252 mCurMethod.changeInputMethodSubtype(subtype);
1253 } catch (RemoteException e) {
1254 return;
satokab751aa2010-09-14 19:17:36 +09001255 }
1256 }
1257 }
1258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 return;
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 final long ident = Binder.clearCallingIdentity();
1263 try {
satokab751aa2010-09-14 19:17:36 +09001264 // Set a subtype to this input method.
1265 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001266 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1267 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1268 // because mCurMethodId is stored as a history in
1269 // setSelectedInputMethodAndSubtypeLocked().
1270 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
1272 if (ActivityManagerNative.isSystemReady()) {
1273 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001274 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 intent.putExtra("input_method_id", id);
1276 mContext.sendBroadcast(intent);
1277 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001278 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 } finally {
1280 Binder.restoreCallingIdentity(ident);
1281 }
1282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
satok42c5a162011-05-26 16:46:14 +09001284 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001285 public boolean showSoftInput(IInputMethodClient client, int flags,
1286 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001287 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 long ident = Binder.clearCallingIdentity();
1289 try {
1290 synchronized (mMethodMap) {
1291 if (mCurClient == null || client == null
1292 || mCurClient.client.asBinder() != client.asBinder()) {
1293 try {
1294 // We need to check if this is the current client with
1295 // focus in the window manager, to allow this call to
1296 // be made before input is started in it.
1297 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001298 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001299 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001302 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Joe Onorato8a9b2202010-02-26 18:56:32 -08001306 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001307 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 }
1309 } finally {
1310 Binder.restoreCallingIdentity(ident);
1311 }
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
The Android Open Source Project4df24232009-03-05 14:34:35 -08001314 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 mShowRequested = true;
1316 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1317 mShowExplicitlyRequested = true;
1318 }
1319 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1320 mShowExplicitlyRequested = true;
1321 mShowForced = true;
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
Dianne Hackborncc278702009-09-02 23:07:23 -07001324 if (!mSystemReady) {
1325 return false;
1326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001327
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001330 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1331 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1332 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001334 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 } else if (mHaveConnection && SystemClock.uptimeMillis()
1336 < (mLastBindTime+TIME_TO_RECONNECT)) {
1337 // The client has asked to have the input method shown, but
1338 // we have been sitting here too long with a connection to the
1339 // service and no interface received, so let's disconnect/connect
1340 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001341 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 SystemClock.uptimeMillis()-mLastBindTime,1);
1343 mContext.unbindService(this);
1344 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1345 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001346
The Android Open Source Project4df24232009-03-05 14:34:35 -08001347 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001349
satok42c5a162011-05-26 16:46:14 +09001350 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001351 public boolean hideSoftInput(IInputMethodClient client, int flags,
1352 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001353 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 long ident = Binder.clearCallingIdentity();
1355 try {
1356 synchronized (mMethodMap) {
1357 if (mCurClient == null || client == null
1358 || mCurClient.client.asBinder() != client.asBinder()) {
1359 try {
1360 // We need to check if this is the current client with
1361 // focus in the window manager, to allow this call to
1362 // be made before input is started in it.
1363 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001364 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1365 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001366 mImeWindowVis = 0;
1367 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1368 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001369 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001372 mImeWindowVis = 0;
1373 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001374 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001379 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 } finally {
1382 Binder.restoreCallingIdentity(ident);
1383 }
1384 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001385
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1388 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001389 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001391 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001394 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001396 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1401 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1402 res = true;
1403 } else {
1404 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
1406 mInputShown = false;
1407 mShowRequested = false;
1408 mShowExplicitlyRequested = false;
1409 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001410 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001412
satok42c5a162011-05-26 16:46:14 +09001413 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001414 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1416 boolean first, int windowFlags) {
1417 long ident = Binder.clearCallingIdentity();
1418 try {
1419 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001420 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 + " viewHasFocus=" + viewHasFocus
1422 + " isTextEditor=" + isTextEditor
1423 + " softInputMode=#" + Integer.toHexString(softInputMode)
1424 + " first=" + first + " flags=#"
1425 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 if (mCurClient == null || client == null
1428 || mCurClient.client.asBinder() != client.asBinder()) {
1429 try {
1430 // We need to check if this is the current client with
1431 // focus in the window manager, to allow this call to
1432 // be made before input is started in it.
1433 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001434 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 return;
1436 }
1437 } catch (RemoteException e) {
1438 }
1439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001440
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001441 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001442 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001443 return;
1444 }
1445 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001446
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001447 // Should we auto-show the IME even if the caller has not
1448 // specified what should be done with it?
1449 // We only do this automatically if the window can resize
1450 // to accommodate the IME (so what the user sees will give
1451 // them good context without input information being obscured
1452 // by the IME) or if running on a large screen where there
1453 // is more room for the target window + IME.
1454 final boolean doAutoShow =
1455 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1456 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1457 || mRes.getConfiguration().isLayoutSizeAtLeast(
1458 Configuration.SCREENLAYOUT_SIZE_LARGE);
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1461 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001462 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1464 // There is no focus view, and this window will
1465 // be behind any soft input window, so hide the
1466 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001467 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001468 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001470 } else if (isTextEditor && doAutoShow && (softInputMode &
1471 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 // There is a focus view, and we are navigating forward
1473 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001474 // We only do this automatically if the window an resize
1475 // to accomodate the IME (so what the user sees will give
1476 // them good context without input information being obscured
1477 // by the IME) or if running on a large screen where there
1478 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001479 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001480 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482 break;
1483 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1484 // Do nothing.
1485 break;
1486 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1487 if ((softInputMode &
1488 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001490 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 break;
1493 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001494 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001495 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 break;
1497 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1498 if ((softInputMode &
1499 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001500 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001501 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
1503 break;
1504 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001505 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001506 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 break;
1508 }
1509 }
1510 } finally {
1511 Binder.restoreCallingIdentity(ident);
1512 }
1513 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
satok42c5a162011-05-26 16:46:14 +09001515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1517 synchronized (mMethodMap) {
1518 if (mCurClient == null || client == null
1519 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001520 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001521 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523
satok440aab52010-11-25 09:43:11 +09001524 // Always call subtype picker, because subtype picker is a superset of input method
1525 // picker.
satokab751aa2010-09-14 19:17:36 +09001526 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1527 }
1528 }
1529
satok42c5a162011-05-26 16:46:14 +09001530 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001532 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1533 }
1534
satok42c5a162011-05-26 16:46:14 +09001535 @Override
satok28203512010-11-24 11:06:49 +09001536 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1537 synchronized (mMethodMap) {
1538 if (subtype != null) {
1539 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1540 mMethodMap.get(id), subtype.hashCode()));
1541 } else {
1542 setInputMethod(token, id);
1543 }
1544 }
satokab751aa2010-09-14 19:17:36 +09001545 }
1546
satok42c5a162011-05-26 16:46:14 +09001547 @Override
satokb416a712010-11-25 20:42:14 +09001548 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001549 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001550 synchronized (mMethodMap) {
1551 if (mCurClient == null || client == null
1552 || mCurClient.client.asBinder() != client.asBinder()) {
1553 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1554 }
satok7fee71f2010-12-17 18:54:26 +09001555 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1556 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001557 }
1558 }
1559
satok4fc87d62011-05-20 16:13:43 +09001560 @Override
satok735cf382010-11-11 20:40:09 +09001561 public boolean switchToLastInputMethod(IBinder token) {
1562 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001563 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001564 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001565 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001566 lastImi = mMethodMap.get(lastIme.first);
1567 } else {
1568 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001569 }
satok4fc87d62011-05-20 16:13:43 +09001570 String targetLastImiId = null;
1571 int subtypeId = NOT_A_SUBTYPE_ID;
1572 if (lastIme != null && lastImi != null) {
1573 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1574 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1575 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1576 : mCurrentSubtype.hashCode();
1577 // If the last IME is the same as the current IME and the last subtype is not
1578 // defined, there is no need to switch to the last IME.
1579 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1580 targetLastImiId = lastIme.first;
1581 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1582 }
1583 }
1584
1585 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1586 // This is a safety net. If the currentSubtype can't be added to the history
1587 // and the framework couldn't find the last ime, we will make the last ime be
1588 // the most applicable enabled keyboard subtype of the system imes.
1589 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1590 if (enabled != null) {
1591 final int N = enabled.size();
1592 final String locale = mCurrentSubtype == null
1593 ? mRes.getConfiguration().locale.toString()
1594 : mCurrentSubtype.getLocale();
1595 for (int i = 0; i < N; ++i) {
1596 final InputMethodInfo imi = enabled.get(i);
1597 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1598 InputMethodSubtype keyboardSubtype =
1599 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1600 SUBTYPE_MODE_KEYBOARD, locale, true);
1601 if (keyboardSubtype != null) {
1602 targetLastImiId = imi.getId();
1603 subtypeId = getSubtypeIdFromHashCode(
1604 imi, keyboardSubtype.hashCode());
1605 if(keyboardSubtype.getLocale().equals(locale)) {
1606 break;
1607 }
1608 }
1609 }
1610 }
1611 }
1612 }
1613
1614 if (!TextUtils.isEmpty(targetLastImiId)) {
1615 if (DEBUG) {
1616 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1617 + ", from: " + mCurMethodId + ", " + subtypeId);
1618 }
1619 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1620 return true;
1621 } else {
1622 return false;
1623 }
satok735cf382010-11-11 20:40:09 +09001624 }
1625 }
1626
satoke7c6998e2011-06-03 17:57:59 +09001627 @Override
satok68f1b782011-04-11 14:26:04 +09001628 public InputMethodSubtype getLastInputMethodSubtype() {
1629 synchronized (mMethodMap) {
1630 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1631 // TODO: Handle the case of the last IME with no subtypes
1632 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1633 || TextUtils.isEmpty(lastIme.second)) return null;
1634 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1635 if (lastImi == null) return null;
1636 try {
1637 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001638 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1639 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1640 return null;
1641 }
1642 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001643 } catch (NumberFormatException e) {
1644 return null;
1645 }
1646 }
1647 }
1648
satoke7c6998e2011-06-03 17:57:59 +09001649 @Override
satok91e88122011-07-18 11:11:42 +09001650 public boolean setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
1651 // By this IPC call, only a process which shares the same uid with the IME can add
1652 // additional input method subtypes to the IME.
1653 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return false;
satoke7c6998e2011-06-03 17:57:59 +09001654 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001655 final InputMethodInfo imi = mMethodMap.get(imiId);
satoke7c6998e2011-06-03 17:57:59 +09001656 if (imi == null) return false;
satok91e88122011-07-18 11:11:42 +09001657 final PackageManager pm = mContext.getPackageManager();
1658 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1659 if (packageInfos != null) {
1660 final int packageNum = packageInfos.length;
1661 for (int i = 0; i < packageNum; ++i) {
1662 if (packageInfos[i].equals(imi.getPackageName())) {
1663 mFileManager.addInputMethodSubtypes(imi, subtypes);
1664 buildInputMethodListLocked(mMethodList, mMethodMap);
1665 return true;
1666 }
1667 }
1668 }
satoke7c6998e2011-06-03 17:57:59 +09001669 }
satok91e88122011-07-18 11:11:42 +09001670 return false;
satoke7c6998e2011-06-03 17:57:59 +09001671 }
1672
satok28203512010-11-24 11:06:49 +09001673 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 synchronized (mMethodMap) {
1675 if (token == null) {
1676 if (mContext.checkCallingOrSelfPermission(
1677 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1678 != PackageManager.PERMISSION_GRANTED) {
1679 throw new SecurityException(
1680 "Using null token requires permission "
1681 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1682 }
1683 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001684 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1685 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 return;
1687 }
1688
1689 long ident = Binder.clearCallingIdentity();
1690 try {
satokab751aa2010-09-14 19:17:36 +09001691 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 } finally {
1693 Binder.restoreCallingIdentity(ident);
1694 }
1695 }
1696 }
1697
satok42c5a162011-05-26 16:46:14 +09001698 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 public void hideMySoftInput(IBinder token, int flags) {
1700 synchronized (mMethodMap) {
1701 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001702 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1703 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 return;
1705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 long ident = Binder.clearCallingIdentity();
1707 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001708 hideCurrentInputLocked(flags, null);
1709 } finally {
1710 Binder.restoreCallingIdentity(ident);
1711 }
1712 }
1713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001714
satok42c5a162011-05-26 16:46:14 +09001715 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001716 public void showMySoftInput(IBinder token, int flags) {
1717 synchronized (mMethodMap) {
1718 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001719 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1720 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001721 return;
1722 }
1723 long ident = Binder.clearCallingIdentity();
1724 try {
1725 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } finally {
1727 Binder.restoreCallingIdentity(ident);
1728 }
1729 }
1730 }
1731
1732 void setEnabledSessionInMainThread(SessionState session) {
1733 if (mEnabledSession != session) {
1734 if (mEnabledSession != null) {
1735 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001736 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 mEnabledSession.method.setSessionEnabled(
1738 mEnabledSession.session, false);
1739 } catch (RemoteException e) {
1740 }
1741 }
1742 mEnabledSession = session;
1743 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001744 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 session.method.setSessionEnabled(
1746 session.session, true);
1747 } catch (RemoteException e) {
1748 }
1749 }
1750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
satok42c5a162011-05-26 16:46:14 +09001752 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 public boolean handleMessage(Message msg) {
1754 HandlerCaller.SomeArgs args;
1755 switch (msg.what) {
1756 case MSG_SHOW_IM_PICKER:
1757 showInputMethodMenu();
1758 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001759
satokab751aa2010-09-14 19:17:36 +09001760 case MSG_SHOW_IM_SUBTYPE_PICKER:
1761 showInputMethodSubtypeMenu();
1762 return true;
1763
satok47a44912010-10-06 16:03:58 +09001764 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001765 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001766 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001767 return true;
1768
1769 case MSG_SHOW_IM_CONFIG:
1770 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001771 return true;
1772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 case MSG_UNBIND_INPUT:
1776 try {
1777 ((IInputMethod)msg.obj).unbindInput();
1778 } catch (RemoteException e) {
1779 // There is nothing interesting about the method dying.
1780 }
1781 return true;
1782 case MSG_BIND_INPUT:
1783 args = (HandlerCaller.SomeArgs)msg.obj;
1784 try {
1785 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1786 } catch (RemoteException e) {
1787 }
1788 return true;
1789 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001790 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1793 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 } catch (RemoteException e) {
1795 }
1796 return true;
1797 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001798 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001800 ((IInputMethod)args.arg1).hideSoftInput(0,
1801 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 } catch (RemoteException e) {
1803 }
1804 return true;
1805 case MSG_ATTACH_TOKEN:
1806 args = (HandlerCaller.SomeArgs)msg.obj;
1807 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001808 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1810 } catch (RemoteException e) {
1811 }
1812 return true;
1813 case MSG_CREATE_SESSION:
1814 args = (HandlerCaller.SomeArgs)msg.obj;
1815 try {
1816 ((IInputMethod)args.arg1).createSession(
1817 (IInputMethodCallback)args.arg2);
1818 } catch (RemoteException e) {
1819 }
1820 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 case MSG_START_INPUT:
1824 args = (HandlerCaller.SomeArgs)msg.obj;
1825 try {
1826 SessionState session = (SessionState)args.arg1;
1827 setEnabledSessionInMainThread(session);
1828 session.method.startInput((IInputContext)args.arg2,
1829 (EditorInfo)args.arg3);
1830 } catch (RemoteException e) {
1831 }
1832 return true;
1833 case MSG_RESTART_INPUT:
1834 args = (HandlerCaller.SomeArgs)msg.obj;
1835 try {
1836 SessionState session = (SessionState)args.arg1;
1837 setEnabledSessionInMainThread(session);
1838 session.method.restartInput((IInputContext)args.arg2,
1839 (EditorInfo)args.arg3);
1840 } catch (RemoteException e) {
1841 }
1842 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 case MSG_UNBIND_METHOD:
1847 try {
1848 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1849 } catch (RemoteException e) {
1850 // There is nothing interesting about the last client dying.
1851 }
1852 return true;
1853 case MSG_BIND_METHOD:
1854 args = (HandlerCaller.SomeArgs)msg.obj;
1855 try {
1856 ((IInputMethodClient)args.arg1).onBindMethod(
1857 (InputBindResult)args.arg2);
1858 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001859 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 return true;
1862 }
1863 return false;
1864 }
1865
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001866 private boolean isSystemIme(InputMethodInfo inputMethod) {
1867 return (inputMethod.getServiceInfo().applicationInfo.flags
1868 & ApplicationInfo.FLAG_SYSTEM) != 0;
1869 }
1870
Ken Wakasa586f0512011-01-20 22:31:01 +09001871 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1872 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1873 final int subtypeCount = imi.getSubtypeCount();
1874 for (int i = 0; i < subtypeCount; ++i) {
1875 subtypes.add(imi.getSubtypeAt(i));
1876 }
1877 return subtypes;
1878 }
1879
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001880 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001881 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001882 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001883 // We'd prefer to fall back on a system IME, since that is safer.
1884 int i=enabled.size();
1885 while (i > 0) {
1886 i--;
1887 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1888 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1889 break;
1890 }
1891 }
satokab751aa2010-09-14 19:17:36 +09001892 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001893 if (DEBUG) {
1894 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1895 }
satok723a27e2010-11-11 14:58:11 +09001896 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001897 return true;
1898 }
1899
1900 return false;
1901 }
1902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1904 HashMap<String, InputMethodInfo> map) {
1905 list.clear();
1906 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001909 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001910 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1911 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1912 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1913 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914
1915 List<ResolveInfo> services = pm.queryIntentServices(
1916 new Intent(InputMethod.SERVICE_INTERFACE),
1917 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918
satoke7c6998e2011-06-03 17:57:59 +09001919 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1920 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 for (int i = 0; i < services.size(); ++i) {
1922 ResolveInfo ri = services.get(i);
1923 ServiceInfo si = ri.serviceInfo;
1924 ComponentName compName = new ComponentName(si.packageName, si.name);
1925 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1926 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001927 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 + ": it does not require the permission "
1929 + android.Manifest.permission.BIND_INPUT_METHOD);
1930 continue;
1931 }
1932
Joe Onorato8a9b2202010-02-26 18:56:32 -08001933 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934
1935 try {
satoke7c6998e2011-06-03 17:57:59 +09001936 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001938 final String id = p.getId();
1939 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940
Amith Yamasanie861ec12010-03-24 21:39:27 -07001941 // System IMEs are enabled by default, unless there's a hard keyboard
1942 // and the system IME was explicitly disabled
1943 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1944 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001945 }
1946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001948 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 } catch (IOException 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 }
1956 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001957
1958 String defaultIme = Settings.Secure.getString(mContext
1959 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001960 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001961 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001962 updateFromSettingsLocked();
1963 }
1964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
satokab751aa2010-09-14 19:17:36 +09001969 private void showInputMethodMenu() {
1970 showInputMethodMenuInternal(false);
1971 }
1972
1973 private void showInputMethodSubtypeMenu() {
1974 showInputMethodMenuInternal(true);
1975 }
1976
satok217f5482010-12-15 05:19:19 +09001977 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001978 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001979 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001980 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1981 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001982 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001983 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001984 }
satok217f5482010-12-15 05:19:19 +09001985 mContext.startActivity(intent);
1986 }
1987
1988 private void showConfigureInputMethods() {
1989 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1990 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1991 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1992 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001993 mContext.startActivity(intent);
1994 }
1995
satokab751aa2010-09-14 19:17:36 +09001996 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001997 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 String lastInputMethodId = Settings.Secure.getString(context
2004 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002005 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002006 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002007
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002008 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002009 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2010 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002011 if (immis == null || immis.size() == 0) {
2012 return;
2013 }
2014
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002015 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016
Ken Wakasa761eb372011-03-04 19:06:18 +09002017 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2018 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2019 new Comparator<InputMethodInfo>() {
2020 @Override
2021 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2022 if (imi2 == null) return 0;
2023 if (imi1 == null) return 1;
2024 if (pm == null) {
2025 return imi1.getId().compareTo(imi2.getId());
2026 }
2027 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2028 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2029 return imiId1.toString().compareTo(imiId2.toString());
2030 }
2031 });
satok913a8922010-08-26 21:53:41 +09002032
Ken Wakasa761eb372011-03-04 19:06:18 +09002033 sortedImmis.putAll(immis);
2034
2035 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2036 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2037
2038 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002039 if (imi == null) continue;
2040 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002041 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002042 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2043 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002044 }
satokbb4aa062011-01-19 21:40:27 +09002045 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002046 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002047 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002048 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002049 if (DEBUG) {
2050 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2051 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002052 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002053 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2054 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2055 // We show all enabled IMEs and subtypes when an IME is shown.
2056 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002057 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002058 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002059 final String mode = subtype.getMode();
2060 title = TextUtils.concat(subtype.getDisplayName(context,
2061 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2062 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002063 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2064 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002065 // Removing this subtype from enabledSubtypeSet because we no longer
2066 // need to add an entry of this subtype to imList to avoid duplicated
2067 // entries.
2068 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002069 }
satokab751aa2010-09-14 19:17:36 +09002070 }
2071 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002072 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2073 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002074 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002075 }
satok913a8922010-08-26 21:53:41 +09002076
Ken Wakasa761eb372011-03-04 19:06:18 +09002077 final int N = imList.size();
2078 mItems = new CharSequence[N];
2079 for (int i = 0; i < N; ++i) {
2080 mItems[i] = imList.get(i).first;
2081 }
satokab751aa2010-09-14 19:17:36 +09002082 mIms = new InputMethodInfo[N];
2083 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002084 int checkedItem = 0;
2085 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002086 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002087 mIms[i] = value.first;
2088 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002089 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002090 int subtypeId = mSubtypeIds[i];
2091 if ((subtypeId == NOT_A_SUBTYPE_ID)
2092 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2093 || (subtypeId == lastInputMethodSubtypeId)) {
2094 checkedItem = i;
2095 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 }
satokab751aa2010-09-14 19:17:36 +09002098
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002099 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002100 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002101 public void onClick(DialogInterface dialog, int which) {
2102 hideInputMethodMenu();
2103 }
2104 };
satokd87c2592010-09-29 11:52:06 +09002105
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002106 TypedArray a = context.obtainStyledAttributes(null,
2107 com.android.internal.R.styleable.DialogPreference,
2108 com.android.internal.R.attr.alertDialogStyle, 0);
2109 mDialogBuilder = new AlertDialog.Builder(context)
2110 .setTitle(com.android.internal.R.string.select_input_method)
2111 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002112 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002113 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002115 }
2116 })
2117 .setIcon(a.getDrawable(
2118 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2119 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002120
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002121 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2122 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002123 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002124 public void onClick(DialogInterface dialog, int which) {
2125 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002126 if (mIms == null || mIms.length <= which
2127 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002128 return;
2129 }
2130 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002131 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002132 hideInputMethodMenu();
2133 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002134 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002135 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002136 subtypeId = NOT_A_SUBTYPE_ID;
2137 }
2138 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002139 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002142 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143
satokf90a33e2011-07-19 11:55:52 +09002144 if (showSubtypes && !(mKeyguardManager.isKeyguardLocked()
2145 && mKeyguardManager.isKeyguardSecure())) {
satok82beadf2010-12-27 19:03:06 +09002146 mDialogBuilder.setPositiveButton(
2147 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002148 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002149 @Override
satok7f35c8c2010-10-07 21:13:11 +09002150 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002151 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002152 }
2153 });
2154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002156 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 mSwitchingDialog.getWindow().setType(
2158 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002159 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 mSwitchingDialog.show();
2161 }
2162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002165 synchronized (mMethodMap) {
2166 hideInputMethodMenuLocked();
2167 }
2168 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002169
The Android Open Source Project10592532009-03-18 17:39:46 -07002170 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002171 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172
The Android Open Source Project10592532009-03-18 17:39:46 -07002173 if (mSwitchingDialog != null) {
2174 mSwitchingDialog.dismiss();
2175 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002177
The Android Open Source Project10592532009-03-18 17:39:46 -07002178 mDialogBuilder = null;
2179 mItems = null;
2180 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002184
satok42c5a162011-05-26 16:46:14 +09002185 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 public boolean setInputMethodEnabled(String id, boolean enabled) {
2187 synchronized (mMethodMap) {
2188 if (mContext.checkCallingOrSelfPermission(
2189 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2190 != PackageManager.PERMISSION_GRANTED) {
2191 throw new SecurityException(
2192 "Requires permission "
2193 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2194 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 long ident = Binder.clearCallingIdentity();
2197 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002198 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 } finally {
2200 Binder.restoreCallingIdentity(ident);
2201 }
2202 }
2203 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002204
2205 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2206 // Make sure this is a valid input method.
2207 InputMethodInfo imm = mMethodMap.get(id);
2208 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002209 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002210 }
2211
satokd87c2592010-09-29 11:52:06 +09002212 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2213 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002214
satokd87c2592010-09-29 11:52:06 +09002215 if (enabled) {
2216 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2217 if (pair.first.equals(id)) {
2218 // We are enabling this input method, but it is already enabled.
2219 // Nothing to do. The previous state was enabled.
2220 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002221 }
2222 }
satokd87c2592010-09-29 11:52:06 +09002223 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2224 // Previous state was disabled.
2225 return false;
2226 } else {
2227 StringBuilder builder = new StringBuilder();
2228 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2229 builder, enabledInputMethodsList, id)) {
2230 // Disabled input method is currently selected, switch to another one.
2231 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2232 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002233 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2234 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2235 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002236 }
2237 // Previous state was enabled.
2238 return true;
2239 } else {
2240 // We are disabling the input method but it is already disabled.
2241 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002242 return false;
2243 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002244 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002245 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002246
satok57ffc002011-01-25 00:11:47 +09002247 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2248 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002249 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002250 }
2251
satok723a27e2010-11-11 14:58:11 +09002252 private void saveCurrentInputMethodAndSubtypeToHistory() {
2253 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2254 if (mCurrentSubtype != null) {
2255 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2256 }
satok57ffc002011-01-25 00:11:47 +09002257 if (canAddToLastInputMethod(mCurrentSubtype)) {
2258 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2259 }
satokab751aa2010-09-14 19:17:36 +09002260 }
2261
satok723a27e2010-11-11 14:58:11 +09002262 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2263 boolean setSubtypeOnly) {
2264 // Update the history of InputMethod and Subtype
2265 saveCurrentInputMethodAndSubtypeToHistory();
2266
2267 // Set Subtype here
2268 if (imi == null || subtypeId < 0) {
2269 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002270 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002271 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002272 if (subtypeId < imi.getSubtypeCount()) {
2273 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2274 mSettings.putSelectedSubtype(subtype.hashCode());
2275 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002276 } else {
2277 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2278 mCurrentSubtype = null;
2279 }
satokab751aa2010-09-14 19:17:36 +09002280 }
satok723a27e2010-11-11 14:58:11 +09002281
2282 if (!setSubtypeOnly) {
2283 // Set InputMethod here
2284 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2285 }
2286 }
2287
2288 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2289 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2290 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2291 // newDefaultIme is empty when there is no candidate for the selected IME.
2292 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2293 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2294 if (subtypeHashCode != null) {
2295 try {
2296 lastSubtypeId = getSubtypeIdFromHashCode(
2297 imi, Integer.valueOf(subtypeHashCode));
2298 } catch (NumberFormatException e) {
2299 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2300 }
2301 }
2302 }
2303 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002304 }
2305
2306 private int getSelectedInputMethodSubtypeId(String id) {
2307 InputMethodInfo imi = mMethodMap.get(id);
2308 if (imi == null) {
2309 return NOT_A_SUBTYPE_ID;
2310 }
satokab751aa2010-09-14 19:17:36 +09002311 int subtypeId;
2312 try {
2313 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2314 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2315 } catch (SettingNotFoundException e) {
2316 return NOT_A_SUBTYPE_ID;
2317 }
satok723a27e2010-11-11 14:58:11 +09002318 return getSubtypeIdFromHashCode(imi, subtypeId);
2319 }
2320
2321 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002322 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002323 final int subtypeCount = imi.getSubtypeCount();
2324 for (int i = 0; i < subtypeCount; ++i) {
2325 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002326 if (subtypeHashCode == ims.hashCode()) {
2327 return i;
2328 }
satokab751aa2010-09-14 19:17:36 +09002329 }
2330 }
2331 return NOT_A_SUBTYPE_ID;
2332 }
2333
satokdf31ae62011-01-15 06:19:44 +09002334 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2335 Resources res, List<InputMethodSubtype> subtypes) {
2336 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002337 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2338 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2339 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002340 final int N = subtypes.size();
2341 boolean containsKeyboardSubtype = false;
2342 for (int i = 0; i < N; ++i) {
2343 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002344 final String locale = subtype.getLocale();
2345 final String mode = subtype.getMode();
2346 // When system locale starts with subtype's locale, that subtype will be applicable
2347 // for system locale
2348 // For instance, it's clearly applicable for cases like system locale = en_US and
2349 // subtype = en, but it is not necessarily considered applicable for cases like system
2350 // locale = en and subtype = en_US.
2351 // We just call systemLocale.startsWith(locale) in this function because there is no
2352 // need to find applicable subtypes aggressively unlike
2353 // findLastResortApplicableSubtypeLocked.
2354 if (systemLocale.startsWith(locale)) {
2355 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2356 // If more applicable subtypes are contained, skip.
2357 if (applicableSubtype != null
2358 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2359 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002360 if (!containsKeyboardSubtype
2361 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2362 containsKeyboardSubtype = true;
2363 }
2364 }
2365 }
satok4a28bde2011-06-29 21:03:40 +09002366 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002367 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002368 if (!containsKeyboardSubtype) {
2369 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002370 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002371 if (lastResortKeyboardSubtype != null) {
2372 applicableSubtypes.add(lastResortKeyboardSubtype);
2373 }
2374 }
2375 return applicableSubtypes;
2376 }
2377
satok4e4569d2010-11-19 18:45:53 +09002378 /**
2379 * If there are no selected subtypes, tries finding the most applicable one according to the
2380 * given locale.
2381 * @param subtypes this function will search the most applicable subtype in subtypes
2382 * @param mode subtypes will be filtered by mode
2383 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002384 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2385 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002386 * @return the most applicable subtypeId
2387 */
satokdf31ae62011-01-15 06:19:44 +09002388 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2389 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002390 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002391 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002392 return null;
satok8fbb1e82010-11-02 23:15:58 +09002393 }
satok4e4569d2010-11-19 18:45:53 +09002394 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002395 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002396 }
satok8fbb1e82010-11-02 23:15:58 +09002397 final String language = locale.substring(0, 2);
2398 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002399 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002400 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002401 final int N = subtypes.size();
2402 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002403 InputMethodSubtype subtype = subtypes.get(i);
2404 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002405 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2406 // and all subtypes with all modes can be candidates.
2407 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002408 if (firstMatchedModeSubtype == null) {
2409 firstMatchedModeSubtype = subtype;
2410 }
satok9ef02832010-11-04 21:17:48 +09002411 if (locale.equals(subtypeLocale)) {
2412 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002413 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002414 break;
2415 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2416 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002417 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002418 partialMatchFound = true;
2419 }
satok8fbb1e82010-11-02 23:15:58 +09002420 }
2421 }
2422
satok7599a7f2010-12-22 13:45:23 +09002423 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2424 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002425 }
2426
satok8fbb1e82010-11-02 23:15:58 +09002427 // The first subtype applicable to the system locale will be defined as the most applicable
2428 // subtype.
2429 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002430 if (applicableSubtype != null) {
2431 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2432 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2433 }
satok8fbb1e82010-11-02 23:15:58 +09002434 }
satokcd7cd292010-11-20 15:46:23 +09002435 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002436 }
2437
satok4e4569d2010-11-19 18:45:53 +09002438 // If there are no selected shortcuts, tries finding the most applicable ones.
2439 private Pair<InputMethodInfo, InputMethodSubtype>
2440 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2441 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2442 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002443 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002444 boolean foundInSystemIME = false;
2445
2446 // Search applicable subtype for each InputMethodInfo
2447 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002448 final String imiId = imi.getId();
2449 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2450 continue;
2451 }
satokcd7cd292010-11-20 15:46:23 +09002452 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002453 final List<InputMethodSubtype> enabledSubtypes =
2454 getEnabledInputMethodSubtypeList(imi, true);
2455 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002456 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002457 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002458 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002459 }
satokdf31ae62011-01-15 06:19:44 +09002460 // 2. Search by the system locale from enabledSubtypes.
2461 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002462 if (subtype == null) {
2463 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002464 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002465 }
satok7599a7f2010-12-22 13:45:23 +09002466 // 4. Search by the current subtype's locale from all subtypes.
2467 if (subtype == null && mCurrentSubtype != null) {
2468 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002469 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002470 }
2471 // 5. Search by the system locale from all subtypes.
2472 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002473 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002474 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002475 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002476 }
satokcd7cd292010-11-20 15:46:23 +09002477 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002478 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002479 // The current input method is the most applicable IME.
2480 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002481 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002482 break;
satok7599a7f2010-12-22 13:45:23 +09002483 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002484 // The system input method is 2nd applicable IME.
2485 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002486 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002487 if ((imi.getServiceInfo().applicationInfo.flags
2488 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2489 foundInSystemIME = true;
2490 }
satok4e4569d2010-11-19 18:45:53 +09002491 }
2492 }
2493 }
2494 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002495 if (mostApplicableIMI != null) {
2496 Slog.w(TAG, "Most applicable shortcut input method was:"
2497 + mostApplicableIMI.getId());
2498 if (mostApplicableSubtype != null) {
2499 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2500 + "," + mostApplicableSubtype.getMode() + ","
2501 + mostApplicableSubtype.getLocale());
2502 }
2503 }
satok4e4569d2010-11-19 18:45:53 +09002504 }
satokcd7cd292010-11-20 15:46:23 +09002505 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002506 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002507 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002508 } else {
2509 return null;
2510 }
2511 }
2512
satokab751aa2010-09-14 19:17:36 +09002513 /**
2514 * @return Return the current subtype of this input method.
2515 */
satok42c5a162011-05-26 16:46:14 +09002516 @Override
satokab751aa2010-09-14 19:17:36 +09002517 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002518 boolean subtypeIsSelected = false;
2519 try {
2520 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2521 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2522 } catch (SettingNotFoundException e) {
2523 }
satok3ef8b292010-11-23 06:06:29 +09002524 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002525 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002526 String lastInputMethodId = Settings.Secure.getString(
2527 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002528 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2529 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002530 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2531 if (imi != null) {
2532 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002533 // the most applicable subtype from explicitly or implicitly enabled
2534 // subtypes.
2535 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2536 getEnabledInputMethodSubtypeList(imi, true);
2537 // If there is only one explicitly or implicitly enabled subtype,
2538 // just returns it.
2539 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2540 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2541 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2542 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2543 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2544 SUBTYPE_MODE_KEYBOARD, null, true);
2545 if (mCurrentSubtype == null) {
2546 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2547 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2548 true);
2549 }
2550 }
satok4e4569d2010-11-19 18:45:53 +09002551 }
satokcd7cd292010-11-20 15:46:23 +09002552 } else {
satok3ef8b292010-11-23 06:06:29 +09002553 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002554 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002555 }
satok8fbb1e82010-11-02 23:15:58 +09002556 }
satok3ef8b292010-11-23 06:06:29 +09002557 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002558 }
satokab751aa2010-09-14 19:17:36 +09002559 }
2560
satokf3db1af2010-11-23 13:34:33 +09002561 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2562 InputMethodSubtype subtype) {
2563 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2564 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2565 } else {
2566 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2567 subtypes.add(subtype);
2568 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2569 }
2570 }
2571
satok4e4569d2010-11-19 18:45:53 +09002572 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002573 @Override
satok4e4569d2010-11-19 18:45:53 +09002574 public List getShortcutInputMethodsAndSubtypes() {
2575 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002576 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002577 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002578 // If there are no selected shortcut subtypes, the framework will try to find
2579 // the most applicable subtype from all subtypes whose mode is
2580 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002581 Pair<InputMethodInfo, InputMethodSubtype> info =
2582 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2583 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002584 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002585 ret.add(info.first);
2586 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002587 }
satok3da92232011-01-11 22:46:30 +09002588 return ret;
satokf3db1af2010-11-23 13:34:33 +09002589 }
satokf3db1af2010-11-23 13:34:33 +09002590 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2591 ret.add(imi);
2592 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2593 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002594 }
2595 }
satokf3db1af2010-11-23 13:34:33 +09002596 return ret;
satok4e4569d2010-11-19 18:45:53 +09002597 }
2598 }
2599
satok42c5a162011-05-26 16:46:14 +09002600 @Override
satokb66d2872010-11-10 01:04:04 +09002601 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2602 synchronized (mMethodMap) {
2603 if (subtype != null && mCurMethodId != null) {
2604 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2605 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2606 if (subtypeId != NOT_A_SUBTYPE_ID) {
2607 setInputMethodLocked(mCurMethodId, subtypeId);
2608 return true;
2609 }
2610 }
2611 return false;
2612 }
2613 }
2614
satokd87c2592010-09-29 11:52:06 +09002615 /**
2616 * Utility class for putting and getting settings for InputMethod
2617 * TODO: Move all putters and getters of settings to this class.
2618 */
2619 private static class InputMethodSettings {
2620 // The string for enabled input method is saved as follows:
2621 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2622 private static final char INPUT_METHOD_SEPARATER = ':';
2623 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002624 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002625 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2626
satok723a27e2010-11-11 14:58:11 +09002627 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002628 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2629
satokdf31ae62011-01-15 06:19:44 +09002630 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002631 private final ContentResolver mResolver;
2632 private final HashMap<String, InputMethodInfo> mMethodMap;
2633 private final ArrayList<InputMethodInfo> mMethodList;
2634
2635 private String mEnabledInputMethodsStrCache;
2636
2637 private static void buildEnabledInputMethodsSettingString(
2638 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2639 String id = pair.first;
2640 ArrayList<String> subtypes = pair.second;
2641 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002642 // Inputmethod and subtypes are saved in the settings as follows:
2643 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2644 for (String subtypeId: subtypes) {
2645 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002646 }
2647 }
2648
2649 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002650 Resources res, ContentResolver resolver,
2651 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2652 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002653 mResolver = resolver;
2654 mMethodMap = methodMap;
2655 mMethodList = methodList;
2656 }
2657
2658 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2659 return createEnabledInputMethodListLocked(
2660 getEnabledInputMethodsAndSubtypeListLocked());
2661 }
2662
satok7f35c8c2010-10-07 21:13:11 +09002663 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002664 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2665 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002666 getEnabledInputMethodsAndSubtypeListLocked());
2667 }
2668
satok67ddf9c2010-11-17 09:45:54 +09002669 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2670 InputMethodInfo imi) {
2671 List<Pair<String, ArrayList<String>>> imsList =
2672 getEnabledInputMethodsAndSubtypeListLocked();
2673 ArrayList<InputMethodSubtype> enabledSubtypes =
2674 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002675 if (imi != null) {
2676 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2677 InputMethodInfo info = mMethodMap.get(imsPair.first);
2678 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002679 final int subtypeCount = info.getSubtypeCount();
2680 for (int i = 0; i < subtypeCount; ++i) {
2681 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002682 for (String s: imsPair.second) {
2683 if (String.valueOf(ims.hashCode()).equals(s)) {
2684 enabledSubtypes.add(ims);
2685 }
satok67ddf9c2010-11-17 09:45:54 +09002686 }
2687 }
satok884ef9a2010-11-18 10:39:46 +09002688 break;
satok67ddf9c2010-11-17 09:45:54 +09002689 }
satok67ddf9c2010-11-17 09:45:54 +09002690 }
2691 }
2692 return enabledSubtypes;
2693 }
2694
satokd87c2592010-09-29 11:52:06 +09002695 // At the initial boot, the settings for input methods are not set,
2696 // so we need to enable IME in that case.
2697 public void enableAllIMEsIfThereIsNoEnabledIME() {
2698 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2699 StringBuilder sb = new StringBuilder();
2700 final int N = mMethodList.size();
2701 for (int i = 0; i < N; i++) {
2702 InputMethodInfo imi = mMethodList.get(i);
2703 Slog.i(TAG, "Adding: " + imi.getId());
2704 if (i > 0) sb.append(':');
2705 sb.append(imi.getId());
2706 }
2707 putEnabledInputMethodsStr(sb.toString());
2708 }
2709 }
2710
satokbb4aa062011-01-19 21:40:27 +09002711 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002712 ArrayList<Pair<String, ArrayList<String>>> imsList
2713 = new ArrayList<Pair<String, ArrayList<String>>>();
2714 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2715 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2716 return imsList;
2717 }
satok723a27e2010-11-11 14:58:11 +09002718 mInputMethodSplitter.setString(enabledInputMethodsStr);
2719 while (mInputMethodSplitter.hasNext()) {
2720 String nextImsStr = mInputMethodSplitter.next();
2721 mSubtypeSplitter.setString(nextImsStr);
2722 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002723 ArrayList<String> subtypeHashes = new ArrayList<String>();
2724 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002725 String imeId = mSubtypeSplitter.next();
2726 while (mSubtypeSplitter.hasNext()) {
2727 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002728 }
2729 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2730 }
2731 }
2732 return imsList;
2733 }
2734
2735 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2736 if (reloadInputMethodStr) {
2737 getEnabledInputMethodsStr();
2738 }
2739 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2740 // Add in the newly enabled input method.
2741 putEnabledInputMethodsStr(id);
2742 } else {
2743 putEnabledInputMethodsStr(
2744 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2745 }
2746 }
2747
2748 /**
2749 * Build and put a string of EnabledInputMethods with removing specified Id.
2750 * @return the specified id was removed or not.
2751 */
2752 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2753 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2754 boolean isRemoved = false;
2755 boolean needsAppendSeparator = false;
2756 for (Pair<String, ArrayList<String>> ims: imsList) {
2757 String curId = ims.first;
2758 if (curId.equals(id)) {
2759 // We are disabling this input method, and it is
2760 // currently enabled. Skip it to remove from the
2761 // new list.
2762 isRemoved = true;
2763 } else {
2764 if (needsAppendSeparator) {
2765 builder.append(INPUT_METHOD_SEPARATER);
2766 } else {
2767 needsAppendSeparator = true;
2768 }
2769 buildEnabledInputMethodsSettingString(builder, ims);
2770 }
2771 }
2772 if (isRemoved) {
2773 // Update the setting with the new list of input methods.
2774 putEnabledInputMethodsStr(builder.toString());
2775 }
2776 return isRemoved;
2777 }
2778
2779 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2780 List<Pair<String, ArrayList<String>>> imsList) {
2781 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2782 for (Pair<String, ArrayList<String>> ims: imsList) {
2783 InputMethodInfo info = mMethodMap.get(ims.first);
2784 if (info != null) {
2785 res.add(info);
2786 }
2787 }
2788 return res;
2789 }
2790
satok7f35c8c2010-10-07 21:13:11 +09002791 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002792 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002793 List<Pair<String, ArrayList<String>>> imsList) {
2794 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2795 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2796 for (Pair<String, ArrayList<String>> ims : imsList) {
2797 InputMethodInfo info = mMethodMap.get(ims.first);
2798 if (info != null) {
2799 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2800 }
2801 }
2802 return res;
2803 }
2804
satokd87c2592010-09-29 11:52:06 +09002805 private void putEnabledInputMethodsStr(String str) {
2806 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2807 mEnabledInputMethodsStrCache = str;
2808 }
2809
2810 private String getEnabledInputMethodsStr() {
2811 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2812 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002813 if (DEBUG) {
2814 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2815 }
satokd87c2592010-09-29 11:52:06 +09002816 return mEnabledInputMethodsStrCache;
2817 }
satok723a27e2010-11-11 14:58:11 +09002818
2819 private void saveSubtypeHistory(
2820 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2821 StringBuilder builder = new StringBuilder();
2822 boolean isImeAdded = false;
2823 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2824 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2825 newSubtypeId);
2826 isImeAdded = true;
2827 }
2828 for (Pair<String, String> ime: savedImes) {
2829 String imeId = ime.first;
2830 String subtypeId = ime.second;
2831 if (TextUtils.isEmpty(subtypeId)) {
2832 subtypeId = NOT_A_SUBTYPE_ID_STR;
2833 }
2834 if (isImeAdded) {
2835 builder.append(INPUT_METHOD_SEPARATER);
2836 } else {
2837 isImeAdded = true;
2838 }
2839 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2840 subtypeId);
2841 }
2842 // Remove the last INPUT_METHOD_SEPARATER
2843 putSubtypeHistoryStr(builder.toString());
2844 }
2845
2846 public void addSubtypeToHistory(String imeId, String subtypeId) {
2847 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2848 for (Pair<String, String> ime: subtypeHistory) {
2849 if (ime.first.equals(imeId)) {
2850 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002851 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002852 + ime.second);
2853 }
2854 // We should break here
2855 subtypeHistory.remove(ime);
2856 break;
2857 }
2858 }
satokbb4aa062011-01-19 21:40:27 +09002859 if (DEBUG) {
2860 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2861 }
satok723a27e2010-11-11 14:58:11 +09002862 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2863 }
2864
2865 private void putSubtypeHistoryStr(String str) {
2866 if (DEBUG) {
2867 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2868 }
2869 Settings.Secure.putString(
2870 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2871 }
2872
2873 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2874 // Gets the first one from the history
2875 return getLastSubtypeForInputMethodLockedInternal(null);
2876 }
2877
2878 public String getLastSubtypeForInputMethodLocked(String imeId) {
2879 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2880 if (ime != null) {
2881 return ime.second;
2882 } else {
2883 return null;
2884 }
2885 }
2886
2887 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2888 List<Pair<String, ArrayList<String>>> enabledImes =
2889 getEnabledInputMethodsAndSubtypeListLocked();
2890 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002891 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002892 final String imeInTheHistory = imeAndSubtype.first;
2893 // If imeId is empty, returns the first IME and subtype in the history
2894 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2895 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002896 final String subtypeHashCode =
2897 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2898 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002899 if (!TextUtils.isEmpty(subtypeHashCode)) {
2900 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002901 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002902 }
2903 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2904 }
2905 }
2906 }
2907 if (DEBUG) {
2908 Slog.d(TAG, "No enabled IME found in the history");
2909 }
2910 return null;
2911 }
2912
satokdf31ae62011-01-15 06:19:44 +09002913 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002914 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2915 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2916 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002917 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2918 if (explicitlyEnabledSubtypes.size() == 0) {
2919 // If there are no explicitly enabled subtypes, applicable subtypes are
2920 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002921 InputMethodInfo ime = mMethodMap.get(imeId);
2922 // If IME is enabled and no subtypes are enabled, applicable subtypes
2923 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002924 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002925 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002926 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002927 if (implicitlySelectedSubtypes != null) {
2928 final int N = implicitlySelectedSubtypes.size();
2929 for (int i = 0; i < N; ++i) {
2930 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2931 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2932 return subtypeHashCode;
2933 }
2934 }
2935 }
2936 }
2937 } else {
satokf6cafb62011-01-17 16:29:02 +09002938 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002939 if (s.equals(subtypeHashCode)) {
2940 // If both imeId and subtypeId are enabled, return subtypeId.
2941 return s;
2942 }
satok723a27e2010-11-11 14:58:11 +09002943 }
2944 }
2945 // If imeId was enabled but subtypeId was disabled.
2946 return NOT_A_SUBTYPE_ID_STR;
2947 }
2948 }
2949 // If both imeId and subtypeId are disabled, return null
2950 return null;
2951 }
2952
2953 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2954 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2955 final String subtypeHistoryStr = getSubtypeHistoryStr();
2956 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2957 return imsList;
2958 }
2959 mInputMethodSplitter.setString(subtypeHistoryStr);
2960 while (mInputMethodSplitter.hasNext()) {
2961 String nextImsStr = mInputMethodSplitter.next();
2962 mSubtypeSplitter.setString(nextImsStr);
2963 if (mSubtypeSplitter.hasNext()) {
2964 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2965 // The first element is ime id.
2966 String imeId = mSubtypeSplitter.next();
2967 while (mSubtypeSplitter.hasNext()) {
2968 subtypeId = mSubtypeSplitter.next();
2969 break;
2970 }
2971 imsList.add(new Pair<String, String>(imeId, subtypeId));
2972 }
2973 }
2974 return imsList;
2975 }
2976
2977 private String getSubtypeHistoryStr() {
2978 if (DEBUG) {
2979 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2980 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2981 }
2982 return Settings.Secure.getString(
2983 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2984 }
2985
2986 public void putSelectedInputMethod(String imeId) {
2987 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2988 }
2989
2990 public void putSelectedSubtype(int subtypeId) {
2991 Settings.Secure.putInt(
2992 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2993 }
satokd87c2592010-09-29 11:52:06 +09002994 }
2995
satoke7c6998e2011-06-03 17:57:59 +09002996 private static class InputMethodFileManager {
2997 private static final String SYSTEM_PATH = "system";
2998 private static final String INPUT_METHOD_PATH = "inputmethod";
2999 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3000 private static final String NODE_SUBTYPES = "subtypes";
3001 private static final String NODE_SUBTYPE = "subtype";
3002 private static final String NODE_IMI = "imi";
3003 private static final String ATTR_ID = "id";
3004 private static final String ATTR_LABEL = "label";
3005 private static final String ATTR_ICON = "icon";
3006 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3007 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3008 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3009 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3010 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3011 private final HashMap<String, InputMethodInfo> mMethodMap;
3012 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3013 new HashMap<String, List<InputMethodSubtype>>();
3014 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3015 if (methodMap == null) {
3016 throw new NullPointerException("methodMap is null");
3017 }
3018 mMethodMap = methodMap;
3019 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3020 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3021 if (!inputMethodDir.mkdirs()) {
3022 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3023 }
3024 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3025 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3026 if (!subtypeFile.exists()) {
3027 // If "subtypes.xml" doesn't exist, create a blank file.
3028 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3029 methodMap);
3030 } else {
3031 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3032 }
3033 }
3034
3035 private void deleteAllInputMethodSubtypes(String imiId) {
3036 synchronized (mMethodMap) {
3037 mSubtypesMap.remove(imiId);
3038 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3039 mMethodMap);
3040 }
3041 }
3042
3043 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003044 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003045 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003046 final HashSet<InputMethodSubtype> existingSubtypes =
3047 new HashSet<InputMethodSubtype>();
3048 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3049 existingSubtypes.add(imi.getSubtypeAt(i));
3050 }
3051
satoke7c6998e2011-06-03 17:57:59 +09003052 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3053 final int N = additionalSubtypes.length;
3054 for (int i = 0; i < N; ++i) {
3055 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003056 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003057 subtypes.add(subtype);
3058 }
3059 }
satok4a28bde2011-06-29 21:03:40 +09003060 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003061 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3062 mMethodMap);
3063 }
3064 }
3065
3066 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3067 synchronized (mMethodMap) {
3068 return mSubtypesMap;
3069 }
3070 }
3071
3072 private static void writeAdditionalInputMethodSubtypes(
3073 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3074 HashMap<String, InputMethodInfo> methodMap) {
3075 // Safety net for the case that this function is called before methodMap is set.
3076 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3077 FileOutputStream fos = null;
3078 try {
3079 fos = subtypesFile.startWrite();
3080 final XmlSerializer out = new FastXmlSerializer();
3081 out.setOutput(fos, "utf-8");
3082 out.startDocument(null, true);
3083 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3084 out.startTag(null, NODE_SUBTYPES);
3085 for (String imiId : allSubtypes.keySet()) {
3086 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3087 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3088 continue;
3089 }
3090 out.startTag(null, NODE_IMI);
3091 out.attribute(null, ATTR_ID, imiId);
3092 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3093 final int N = subtypesList.size();
3094 for (int i = 0; i < N; ++i) {
3095 final InputMethodSubtype subtype = subtypesList.get(i);
3096 out.startTag(null, NODE_SUBTYPE);
3097 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3098 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3099 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3100 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3101 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3102 out.attribute(null, ATTR_IS_AUXILIARY,
3103 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3104 out.endTag(null, NODE_SUBTYPE);
3105 }
3106 out.endTag(null, NODE_IMI);
3107 }
3108 out.endTag(null, NODE_SUBTYPES);
3109 out.endDocument();
3110 subtypesFile.finishWrite(fos);
3111 } catch (java.io.IOException e) {
3112 Slog.w(TAG, "Error writing subtypes", e);
3113 if (fos != null) {
3114 subtypesFile.failWrite(fos);
3115 }
3116 }
3117 }
3118
3119 private static void readAdditionalInputMethodSubtypes(
3120 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3121 if (allSubtypes == null || subtypesFile == null) return;
3122 allSubtypes.clear();
3123 FileInputStream fis = null;
3124 try {
3125 fis = subtypesFile.openRead();
3126 final XmlPullParser parser = Xml.newPullParser();
3127 parser.setInput(fis, null);
3128 int type = parser.getEventType();
3129 // Skip parsing until START_TAG
3130 while ((type = parser.next()) != XmlPullParser.START_TAG
3131 && type != XmlPullParser.END_DOCUMENT) {}
3132 String firstNodeName = parser.getName();
3133 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3134 throw new XmlPullParserException("Xml doesn't start with subtypes");
3135 }
3136 final int depth =parser.getDepth();
3137 String currentImiId = null;
3138 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3139 while (((type = parser.next()) != XmlPullParser.END_TAG
3140 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3141 if (type != XmlPullParser.START_TAG)
3142 continue;
3143 final String nodeName = parser.getName();
3144 if (NODE_IMI.equals(nodeName)) {
3145 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3146 if (TextUtils.isEmpty(currentImiId)) {
3147 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3148 continue;
3149 }
3150 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3151 allSubtypes.put(currentImiId, tempSubtypesArray);
3152 } else if (NODE_SUBTYPE.equals(nodeName)) {
3153 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3154 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3155 continue;
3156 }
3157 final int icon = Integer.valueOf(
3158 parser.getAttributeValue(null, ATTR_ICON));
3159 final int label = Integer.valueOf(
3160 parser.getAttributeValue(null, ATTR_LABEL));
3161 final String imeSubtypeLocale =
3162 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3163 final String imeSubtypeMode =
3164 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3165 final String imeSubtypeExtraValue =
3166 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003167 final boolean isAuxiliary = "1".equals(String.valueOf(
3168 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003169 final InputMethodSubtype subtype =
3170 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3171 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3172 tempSubtypesArray.add(subtype);
3173 }
3174 }
3175 } catch (XmlPullParserException e) {
3176 Slog.w(TAG, "Error reading subtypes: " + e);
3177 return;
3178 } catch (java.io.IOException e) {
3179 Slog.w(TAG, "Error reading subtypes: " + e);
3180 return;
3181 } catch (NumberFormatException e) {
3182 Slog.w(TAG, "Error reading subtypes: " + e);
3183 return;
3184 } finally {
3185 if (fis != null) {
3186 try {
3187 fis.close();
3188 } catch (java.io.IOException e1) {
3189 Slog.w(TAG, "Failed to close.");
3190 }
3191 }
3192 }
3193 }
3194 }
3195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 @Override
3199 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3200 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3201 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3204 + Binder.getCallingPid()
3205 + ", uid=" + Binder.getCallingUid());
3206 return;
3207 }
3208
3209 IInputMethod method;
3210 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 synchronized (mMethodMap) {
3215 p.println("Current Input Method Manager state:");
3216 int N = mMethodList.size();
3217 p.println(" Input Methods:");
3218 for (int i=0; i<N; i++) {
3219 InputMethodInfo info = mMethodList.get(i);
3220 p.println(" InputMethod #" + i + ":");
3221 info.dump(p, " ");
3222 }
3223 p.println(" Clients:");
3224 for (ClientState ci : mClients.values()) {
3225 p.println(" Client " + ci + ":");
3226 p.println(" client=" + ci.client);
3227 p.println(" inputContext=" + ci.inputContext);
3228 p.println(" sessionRequested=" + ci.sessionRequested);
3229 p.println(" curSession=" + ci.curSession);
3230 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003231 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003233 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3234 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3236 + " mBoundToMethod=" + mBoundToMethod);
3237 p.println(" mCurToken=" + mCurToken);
3238 p.println(" mCurIntent=" + mCurIntent);
3239 method = mCurMethod;
3240 p.println(" mCurMethod=" + mCurMethod);
3241 p.println(" mEnabledSession=" + mEnabledSession);
3242 p.println(" mShowRequested=" + mShowRequested
3243 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3244 + " mShowForced=" + mShowForced
3245 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003246 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003248
Jeff Brownb88102f2010-09-08 11:49:43 -07003249 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 pw.flush();
3252 try {
3253 client.client.asBinder().dump(fd, args);
3254 } catch (RemoteException e) {
3255 p.println("Input method client dead: " + e);
3256 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003257 } else {
3258 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003260
Jeff Brownb88102f2010-09-08 11:49:43 -07003261 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 pw.flush();
3264 try {
3265 method.asBinder().dump(fd, args);
3266 } catch (RemoteException e) {
3267 p.println("Input method service dead: " + e);
3268 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003269 } else {
3270 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 }
3272 }
3273}