blob: 25979782afdeea0ac611fb39ba39a26260664d1d [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;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070055import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.res.Resources;
57import android.content.res.TypedArray;
58import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090059import android.graphics.BitmapFactory;
Joe Onorato857fd9b2011-01-27 15:08:35 -080060import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.IInterface;
66import android.os.Message;
67import android.os.Parcel;
68import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080069import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.ServiceManager;
71import android.os.SystemClock;
72import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070073import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090074import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090076import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090078import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090079import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.PrintWriterPrinter;
81import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090082import android.util.Slog;
83import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.IWindowManager;
85import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090086import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.view.inputmethod.InputBinding;
88import android.view.inputmethod.InputMethod;
89import android.view.inputmethod.InputMethodInfo;
90import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090091import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
satoke7c6998e2011-06-03 17:57:59 +090093import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090095import java.io.FileInputStream;
96import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.io.IOException;
98import java.io.PrintWriter;
99import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +0900100import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900101import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900103import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900105import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900106import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108/**
109 * This class provides a system service that manages input methods.
110 */
111public class InputMethodManagerService extends IInputMethodManager.Stub
112 implements ServiceConnection, Handler.Callback {
113 static final boolean DEBUG = false;
114 static final String TAG = "InputManagerService";
115
116 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900117 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900118 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900119 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 static final int MSG_UNBIND_INPUT = 1000;
122 static final int MSG_BIND_INPUT = 1010;
123 static final int MSG_SHOW_SOFT_INPUT = 1020;
124 static final int MSG_HIDE_SOFT_INPUT = 1030;
125 static final int MSG_ATTACH_TOKEN = 1040;
126 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 static final int MSG_START_INPUT = 2000;
129 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 static final int MSG_UNBIND_METHOD = 3000;
132 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
satokf9f01002011-05-19 21:31:50 +0900136 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
137
satokab751aa2010-09-14 19:17:36 +0900138 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900139 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900140 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
141 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900142 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800145 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900147 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700149 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final IWindowManager mIWindowManager;
151 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900152 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 // All known input methods. mMethodMap also serves as the global
157 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900158 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
159 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900160 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
161 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
satok7cfc0ed2011-06-20 21:29:36 +0900163 // Ongoing notification
164 private final NotificationManager mNotificationManager;
satokf90a33e2011-07-19 11:55:52 +0900165 private final KeyguardManager mKeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +0900166 private final Notification mImeSwitcherNotification;
167 private final PendingIntent mImeSwitchPendingIntent;
168 private final boolean mShowOngoingImeSwitcherForPhones;
169 private boolean mNotificationShown;
170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 class SessionState {
172 final ClientState client;
173 final IInputMethod method;
174 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 @Override
177 public String toString() {
178 return "SessionState{uid " + client.uid + " pid " + client.pid
179 + " method " + Integer.toHexString(
180 System.identityHashCode(method))
181 + " session " + Integer.toHexString(
182 System.identityHashCode(session))
183 + "}";
184 }
185
186 SessionState(ClientState _client, IInputMethod _method,
187 IInputMethodSession _session) {
188 client = _client;
189 method = _method;
190 session = _session;
191 }
192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 class ClientState {
195 final IInputMethodClient client;
196 final IInputContext inputContext;
197 final int uid;
198 final int pid;
199 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 boolean sessionRequested;
202 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 @Override
205 public String toString() {
206 return "ClientState{" + Integer.toHexString(
207 System.identityHashCode(this)) + " uid " + uid
208 + " pid " + pid + "}";
209 }
210
211 ClientState(IInputMethodClient _client, IInputContext _inputContext,
212 int _uid, int _pid) {
213 client = _client;
214 inputContext = _inputContext;
215 uid = _uid;
216 pid = _pid;
217 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
218 }
219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 final HashMap<IBinder, ClientState> mClients
222 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700225 * Set once the system is ready to run third party code.
226 */
227 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * Id of the currently selected input method.
231 */
232 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 /**
235 * The current binding sequence number, incremented every time there is
236 * a new bind performed.
237 */
238 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 /**
241 * The client that is currently bound to an input method.
242 */
243 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700246 * The last window token that gained focus.
247 */
248 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800249
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * The input context last provided by the current client.
252 */
253 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * The attributes last provided by the current client.
257 */
258 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
261 * The input method ID of the input method service that we are currently
262 * connected to or in the process of connecting to.
263 */
264 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
satokab751aa2010-09-14 19:17:36 +0900267 * The current subtype of the current input method.
268 */
269 private InputMethodSubtype mCurrentSubtype;
270
satok4e4569d2010-11-19 18:45:53 +0900271 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900272 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
273 mShortcutInputMethodsAndSubtypes =
274 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900275
276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 * Set to true if our ServiceConnection is currently actively bound to
278 * a service (whether or not we have gotten its IBinder back yet).
279 */
280 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
283 * Set if the client has asked for the input method to be shown.
284 */
285 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * Set if we were explicitly told to show the input method.
289 */
290 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * Set if we were forced to be shown.
294 */
295 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * Set if we last told the input method to show itself.
299 */
300 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * The Intent used to connect to the current input method.
304 */
305 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * The token we have made for the currently active input method, to
309 * identify it in the future.
310 */
311 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 /**
314 * If non-null, this is the input method service we are currently connected
315 * to.
316 */
317 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * Time that we last initiated a bind to the input method, to determine
321 * if we should try to disconnect and reconnect to it.
322 */
323 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 /**
326 * Have we called mCurMethod.bindInput()?
327 */
328 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 /**
331 * Currently enabled session. Only touched by service thread, not
332 * protected by a lock.
333 */
334 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 /**
337 * True if the screen is on. The value is true initially.
338 */
339 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800340
Joe Onorato857fd9b2011-01-27 15:08:35 -0800341 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
342 int mImeWindowVis;
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 AlertDialog.Builder mDialogBuilder;
345 AlertDialog mSwitchingDialog;
346 InputMethodInfo[] mIms;
347 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900348 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 class SettingsObserver extends ContentObserver {
351 SettingsObserver(Handler handler) {
352 super(handler);
353 ContentResolver resolver = mContext.getContentResolver();
354 resolver.registerContentObserver(Settings.Secure.getUriFor(
355 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900356 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900357 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
358 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900359 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 @Override public void onChange(boolean selfChange) {
363 synchronized (mMethodMap) {
364 updateFromSettingsLocked();
365 }
366 }
367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
370 @Override
371 public void onReceive(Context context, Intent intent) {
372 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
373 mScreenOn = true;
374 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
375 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700376 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
377 hideInputMethodMenu();
378 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800380 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382
383 // Inform the current client of the change in active status
384 try {
385 if (mCurClient != null && mCurClient.client != null) {
386 mCurClient.client.setActive(mScreenOn);
387 }
388 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800389 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 + mCurClient.pid + " uid " + mCurClient.uid);
391 }
392 }
393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800395 class MyPackageMonitor extends PackageMonitor {
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800398 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800400 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
402 final int N = mMethodList.size();
403 if (curInputMethodId != null) {
404 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800405 InputMethodInfo imi = mMethodList.get(i);
406 if (imi.getId().equals(curInputMethodId)) {
407 for (String pkg : packages) {
408 if (imi.getPackageName().equals(pkg)) {
409 if (!doit) {
410 return true;
411 }
satok723a27e2010-11-11 14:58:11 +0900412 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800413 chooseNewDefaultIMELocked();
414 return true;
415 }
416 }
417 }
418 }
419 }
420 }
421 return false;
422 }
423
424 @Override
425 public void onSomePackagesChanged() {
426 synchronized (mMethodMap) {
427 InputMethodInfo curIm = null;
428 String curInputMethodId = Settings.Secure.getString(mContext
429 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
430 final int N = mMethodList.size();
431 if (curInputMethodId != null) {
432 for (int i=0; i<N; i++) {
433 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900434 final String imiId = imi.getId();
435 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 curIm = imi;
437 }
satoke7c6998e2011-06-03 17:57:59 +0900438
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800439 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900440 if (isPackageModified(imi.getPackageName())) {
441 mFileManager.deleteAllInputMethodSubtypes(imiId);
442 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800443 if (change == PACKAGE_TEMPORARY_CHANGE
444 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800445 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800446 + imi.getComponent());
447 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449 }
450 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 buildInputMethodListLocked(mMethodList, mMethodMap);
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800456 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800457 int change = isPackageDisappearing(curIm.getPackageName());
458 if (change == PACKAGE_TEMPORARY_CHANGE
459 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800460 ServiceInfo si = null;
461 try {
462 si = mContext.getPackageManager().getServiceInfo(
463 curIm.getComponent(), 0);
464 } catch (PackageManager.NameNotFoundException ex) {
465 }
466 if (si == null) {
467 // Uh oh, current input method is no longer around!
468 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800469 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800470 mImeWindowVis = 0;
471 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
472 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 changed = true;
475 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900477 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 }
479 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 }
satokab751aa2010-09-14 19:17:36 +0900482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 if (curIm == null) {
484 // We currently don't have a default input method... is
485 // one now available?
486 changed = chooseNewDefaultIMELocked();
487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 if (changed) {
490 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 }
494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 class MethodCallback extends IInputMethodCallback.Stub {
497 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 MethodCallback(IInputMethod method) {
500 mMethod = method;
501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
satoke7c6998e2011-06-03 17:57:59 +0900503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public void finishedEvent(int seq, boolean handled) throws RemoteException {
505 }
506
satoke7c6998e2011-06-03 17:57:59 +0900507 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public void sessionCreated(IInputMethodSession session) throws RemoteException {
509 onSessionCreated(mMethod, session);
510 }
511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800512
Joe Onorato089de882010-04-12 08:18:45 -0700513 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800515 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 mHandler = new Handler(this);
517 mIWindowManager = IWindowManager.Stub.asInterface(
518 ServiceManager.getService(Context.WINDOW_SERVICE));
519 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public void executeMessage(Message msg) {
522 handleMessage(msg);
523 }
524 });
satok7cfc0ed2011-06-20 21:29:36 +0900525
satokf90a33e2011-07-19 11:55:52 +0900526 mKeyguardManager = (KeyguardManager)
527 mContext.getSystemService(Context.KEYGUARD_SERVICE);
satok7cfc0ed2011-06-20 21:29:36 +0900528 mNotificationManager = (NotificationManager)
529 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
530 mImeSwitcherNotification = new Notification();
531 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
532 mImeSwitcherNotification.when = 0;
533 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
534 mImeSwitcherNotification.tickerText = null;
535 mImeSwitcherNotification.defaults = 0; // please be quiet
536 mImeSwitcherNotification.sound = null;
537 mImeSwitcherNotification.vibrate = null;
538 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900539 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satok7cfc0ed2011-06-20 21:29:36 +0900540 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
541 com.android.internal.R.bool.show_ongoing_ime_switcher);
542
satoke7c6998e2011-06-03 17:57:59 +0900543 synchronized (mMethodMap) {
544 mFileManager = new InputMethodFileManager(mMethodMap);
545 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800546
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800547 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 IntentFilter screenOnOffFilt = new IntentFilter();
550 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
551 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700552 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
satok913a8922010-08-26 21:53:41 +0900555 mStatusBar = statusBar;
556 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900557 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900558
satokd87c2592010-09-29 11:52:06 +0900559 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900560 mSettings = new InputMethodSettings(
561 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900563 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
satokd87c2592010-09-29 11:52:06 +0900565 if (TextUtils.isEmpty(Settings.Secure.getString(
566 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900568 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
570 try {
satokd87c2592010-09-29 11:52:06 +0900571 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 imi.getPackageName(), 0).getResources();
573 if (res.getBoolean(imi.getIsDefaultResourceId())) {
574 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800575 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
577 } catch (PackageManager.NameNotFoundException ex) {
578 } catch (Resources.NotFoundException ex) {
579 }
580 }
581 }
satokd87c2592010-09-29 11:52:06 +0900582 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800584 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900587 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 mSettingsObserver = new SettingsObserver(mHandler);
592 updateFromSettingsLocked();
593 }
594
595 @Override
596 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
597 throws RemoteException {
598 try {
599 return super.onTransact(code, data, reply, flags);
600 } catch (RuntimeException e) {
601 // The input method manager only throws security exceptions, so let's
602 // log all others.
603 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800604 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 throw e;
607 }
608 }
609
610 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700611 synchronized (mMethodMap) {
612 if (!mSystemReady) {
613 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700614 try {
615 startInputInnerLocked();
616 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800617 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700618 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700619 }
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800622
satoke7c6998e2011-06-03 17:57:59 +0900623 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 public List<InputMethodInfo> getInputMethodList() {
625 synchronized (mMethodMap) {
626 return new ArrayList<InputMethodInfo>(mMethodList);
627 }
628 }
629
satoke7c6998e2011-06-03 17:57:59 +0900630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public List<InputMethodInfo> getEnabledInputMethodList() {
632 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900633 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
635 }
636
satokbb4aa062011-01-19 21:40:27 +0900637 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
638 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
639 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
640 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
641 for (InputMethodInfo imi: getEnabledInputMethodList()) {
642 enabledInputMethodAndSubtypes.put(
643 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
644 }
645 return enabledInputMethodAndSubtypes;
646 }
647
648 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
649 boolean allowsImplicitlySelectedSubtypes) {
650 if (imi == null && mCurMethodId != null) {
651 imi = mMethodMap.get(mCurMethodId);
652 }
satok7265d9b2011-02-14 15:47:30 +0900653 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900654 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900655 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
656 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900657 }
satok7265d9b2011-02-14 15:47:30 +0900658 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900659 }
660
satoke7c6998e2011-06-03 17:57:59 +0900661 @Override
satok16331c82010-12-20 23:48:46 +0900662 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
663 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900664 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900665 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900666 }
667 }
668
satoke7c6998e2011-06-03 17:57:59 +0900669 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 public void addClient(IInputMethodClient client,
671 IInputContext inputContext, int uid, int pid) {
672 synchronized (mMethodMap) {
673 mClients.put(client.asBinder(), new ClientState(client,
674 inputContext, uid, pid));
675 }
676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800677
satoke7c6998e2011-06-03 17:57:59 +0900678 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 public void removeClient(IInputMethodClient client) {
680 synchronized (mMethodMap) {
681 mClients.remove(client.asBinder());
682 }
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 void executeOrSendMessage(IInterface target, Message msg) {
686 if (target.asBinder() instanceof Binder) {
687 mCaller.sendMessage(msg);
688 } else {
689 handleMessage(msg);
690 msg.recycle();
691 }
692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700694 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800696 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 + mCurClient.client.asBinder());
698 if (mBoundToMethod) {
699 mBoundToMethod = false;
700 if (mCurMethod != null) {
701 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
702 MSG_UNBIND_INPUT, mCurMethod));
703 }
704 }
705 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
706 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
707 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 // Call setActive(false) on the old client
710 try {
711 mCurClient.client.setActive(false);
712 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 + mCurClient.pid + " uid " + mCurClient.uid);
715 }
716 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project10592532009-03-18 17:39:46 -0700718 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 private int getImeShowFlags() {
723 int flags = 0;
724 if (mShowForced) {
725 flags |= InputMethod.SHOW_FORCED
726 | InputMethod.SHOW_EXPLICIT;
727 } else if (mShowExplicitlyRequested) {
728 flags |= InputMethod.SHOW_EXPLICIT;
729 }
730 return flags;
731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 private int getAppShowFlags() {
734 int flags = 0;
735 if (mShowForced) {
736 flags |= InputMethodManager.SHOW_FORCED;
737 } else if (!mShowExplicitlyRequested) {
738 flags |= InputMethodManager.SHOW_IMPLICIT;
739 }
740 return flags;
741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
744 if (!mBoundToMethod) {
745 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
746 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
747 mBoundToMethod = true;
748 }
749 final SessionState session = mCurClient.curSession;
750 if (initial) {
751 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
752 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
753 } else {
754 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
755 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
756 }
757 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800758 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800759 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 return needResult
762 ? new InputBindResult(session.session, mCurId, mCurSeq)
763 : null;
764 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 InputBindResult startInputLocked(IInputMethodClient client,
767 IInputContext inputContext, EditorInfo attribute,
768 boolean initial, boolean needResult) {
769 // If no method is currently selected, do nothing.
770 if (mCurMethodId == null) {
771 return mNoBinding;
772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 ClientState cs = mClients.get(client.asBinder());
775 if (cs == null) {
776 throw new IllegalArgumentException("unknown client "
777 + client.asBinder());
778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 try {
781 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
782 // Check with the window manager to make sure this client actually
783 // has a window with focus. If not, reject. This is thread safe
784 // because if the focus changes some time before or after, the
785 // next client receiving focus that has any interest in input will
786 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800787 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
789 return null;
790 }
791 } catch (RemoteException e) {
792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if (mCurClient != cs) {
795 // If the client is changing, we need to switch over to the new
796 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700797 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800798 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 + cs.client.asBinder());
800
801 // If the screen is on, inform the new client it is active
802 if (mScreenOn) {
803 try {
804 cs.client.setActive(mScreenOn);
805 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800806 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 + cs.pid + " uid " + cs.uid);
808 }
809 }
810 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 // Bump up the sequence for this client and attach it.
813 mCurSeq++;
814 if (mCurSeq <= 0) mCurSeq = 1;
815 mCurClient = cs;
816 mCurInputContext = inputContext;
817 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Check if the input method is changing.
820 if (mCurId != null && mCurId.equals(mCurMethodId)) {
821 if (cs.curSession != null) {
822 // Fast case: if we are already connected to the input method,
823 // then just return it.
824 return attachNewInputLocked(initial, needResult);
825 }
826 if (mHaveConnection) {
827 if (mCurMethod != null) {
828 if (!cs.sessionRequested) {
829 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
832 MSG_CREATE_SESSION, mCurMethod,
833 new MethodCallback(mCurMethod)));
834 }
835 // Return to client, and we will get back with it when
836 // we have had a session made for it.
837 return new InputBindResult(null, mCurId, mCurSeq);
838 } else if (SystemClock.uptimeMillis()
839 < (mLastBindTime+TIME_TO_RECONNECT)) {
840 // In this case we have connected to the service, but
841 // don't yet have its interface. If it hasn't been too
842 // long since we did the connection, we'll return to
843 // the client and wait to get the service interface so
844 // we can report back. If it has been too long, we want
845 // to fall through so we can try a disconnect/reconnect
846 // to see if we can get back in touch with the service.
847 return new InputBindResult(null, mCurId, mCurSeq);
848 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800849 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
850 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852 }
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700855 return startInputInnerLocked();
856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700858 InputBindResult startInputInnerLocked() {
859 if (mCurMethodId == null) {
860 return mNoBinding;
861 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800862
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700863 if (!mSystemReady) {
864 // If the system is not yet ready, we shouldn't be running third
865 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700866 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 InputMethodInfo info = mMethodMap.get(mCurMethodId);
870 if (info == null) {
871 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800873
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700874 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
877 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700878 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
879 com.android.internal.R.string.input_method_binding_label);
880 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
881 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
883 mLastBindTime = SystemClock.uptimeMillis();
884 mHaveConnection = true;
885 mCurId = info.getId();
886 mCurToken = new Binder();
887 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800888 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 mIWindowManager.addWindowToken(mCurToken,
890 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
891 } catch (RemoteException e) {
892 }
893 return new InputBindResult(null, mCurId, mCurSeq);
894 } else {
895 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800896 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 + mCurIntent);
898 }
899 return null;
900 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800901
satoke7c6998e2011-06-03 17:57:59 +0900902 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 public InputBindResult startInput(IInputMethodClient client,
904 IInputContext inputContext, EditorInfo attribute,
905 boolean initial, boolean needResult) {
906 synchronized (mMethodMap) {
907 final long ident = Binder.clearCallingIdentity();
908 try {
909 return startInputLocked(client, inputContext, attribute,
910 initial, needResult);
911 } finally {
912 Binder.restoreCallingIdentity(ident);
913 }
914 }
915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
satoke7c6998e2011-06-03 17:57:59 +0900917 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 public void finishInput(IInputMethodClient client) {
919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
satoke7c6998e2011-06-03 17:57:59 +0900921 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public void onServiceConnected(ComponentName name, IBinder service) {
923 synchronized (mMethodMap) {
924 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
925 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700926 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800927 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700928 unbindCurrentMethodLocked(false);
929 return;
930 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800931 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700932 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
933 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800935 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700936 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700938 MSG_CREATE_SESSION, mCurMethod,
939 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 }
942 }
943 }
944
945 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
946 synchronized (mMethodMap) {
947 if (mCurMethod != null && method != null
948 && mCurMethod.asBinder() == method.asBinder()) {
949 if (mCurClient != null) {
950 mCurClient.curSession = new SessionState(mCurClient,
951 method, session);
952 mCurClient.sessionRequested = false;
953 InputBindResult res = attachNewInputLocked(true, true);
954 if (res.method != null) {
955 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
956 MSG_BIND_METHOD, mCurClient.client, res));
957 }
958 }
959 }
960 }
961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800962
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700963 void unbindCurrentMethodLocked(boolean reportToClient) {
964 if (mHaveConnection) {
965 mContext.unbindService(this);
966 mHaveConnection = false;
967 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800968
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700969 if (mCurToken != null) {
970 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800971 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700972 mIWindowManager.removeWindowToken(mCurToken);
973 } catch (RemoteException e) {
974 }
975 mCurToken = null;
976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800977
The Android Open Source Project10592532009-03-18 17:39:46 -0700978 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700981 if (reportToClient && mCurClient != null) {
982 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
983 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
984 }
985 }
986
Devin Taylor0c33ed22010-02-23 13:26:46 -0600987 private void finishSession(SessionState sessionState) {
988 if (sessionState != null && sessionState.session != null) {
989 try {
990 sessionState.session.finishSession();
991 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700992 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600993 }
994 }
995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700997 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (mCurMethod != null) {
999 for (ClientState cs : mClients.values()) {
1000 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001001 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 cs.curSession = null;
1003 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001004
1005 finishSession(mEnabledSession);
1006 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 mCurMethod = null;
1008 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001009 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
satoke7c6998e2011-06-03 17:57:59 +09001012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public void onServiceDisconnected(ComponentName name) {
1014 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001015 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 + " mCurIntent=" + mCurIntent);
1017 if (mCurMethod != null && mCurIntent != null
1018 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001019 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 // We consider this to be a new bind attempt, since the system
1021 // should now try to restart the service for us.
1022 mLastBindTime = SystemClock.uptimeMillis();
1023 mShowRequested = mInputShown;
1024 mInputShown = false;
1025 if (mCurClient != null) {
1026 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1027 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1028 }
1029 }
1030 }
1031 }
1032
satokf9f01002011-05-19 21:31:50 +09001033 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001035 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 long ident = Binder.clearCallingIdentity();
1037 try {
1038 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001039 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 return;
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 synchronized (mMethodMap) {
1044 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001046 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001048 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001049 mStatusBar.setIcon("ime", packageName, iconId, 0);
1050 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052 }
1053 } finally {
1054 Binder.restoreCallingIdentity(ident);
1055 }
1056 }
1057
satok7cfc0ed2011-06-20 21:29:36 +09001058 private boolean needsToShowImeSwitchOngoingNotification() {
1059 if (!mShowOngoingImeSwitcherForPhones) return false;
1060 synchronized (mMethodMap) {
1061 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1062 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001063 if (N > 2) return true;
1064 if (N < 1) return false;
1065 int nonAuxCount = 0;
1066 int auxCount = 0;
1067 InputMethodSubtype nonAuxSubtype = null;
1068 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001069 for(int i = 0; i < N; ++i) {
1070 final InputMethodInfo imi = imis.get(i);
1071 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1072 imi, true);
1073 final int subtypeCount = subtypes.size();
1074 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001075 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001076 } else {
1077 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001078 final InputMethodSubtype subtype = subtypes.get(j);
1079 if (!subtype.isAuxiliary()) {
1080 ++nonAuxCount;
1081 nonAuxSubtype = subtype;
1082 } else {
1083 ++auxCount;
1084 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001085 }
1086 }
1087 }
satok7cfc0ed2011-06-20 21:29:36 +09001088 }
satokb6359412011-06-28 17:47:41 +09001089 if (nonAuxCount > 1 || auxCount > 1) {
1090 return true;
1091 } else if (nonAuxCount == 1 && auxCount == 1) {
1092 if (nonAuxSubtype != null && auxSubtype != null
1093 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1094 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1095 return false;
1096 }
1097 return true;
1098 }
1099 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001100 }
satok7cfc0ed2011-06-20 21:29:36 +09001101 }
1102
satokf9f01002011-05-19 21:31:50 +09001103 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001104 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001105 int uid = Binder.getCallingUid();
1106 long ident = Binder.clearCallingIdentity();
1107 try {
1108 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001109 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001110 return;
1111 }
1112
1113 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001114 mImeWindowVis = vis;
1115 mBackDisposition = backDisposition;
1116 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001117 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1118 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1119 final PackageManager pm = mContext.getPackageManager();
1120 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1121 final CharSequence title = mRes.getText(
1122 com.android.internal.R.string.select_input_method);
1123 mImeSwitcherNotification.setLatestEventInfo(
1124 mContext, title, label, mImeSwitchPendingIntent);
1125 mNotificationManager.notify(
1126 com.android.internal.R.string.select_input_method,
1127 mImeSwitcherNotification);
1128 mNotificationShown = true;
1129 } else {
1130 if (mNotificationShown) {
1131 mNotificationManager.cancel(
1132 com.android.internal.R.string.select_input_method);
1133 mNotificationShown = false;
1134 }
1135 }
satok06487a52010-10-29 11:37:18 +09001136 }
1137 } finally {
1138 Binder.restoreCallingIdentity(ident);
1139 }
1140 }
1141
satoke7c6998e2011-06-03 17:57:59 +09001142 @Override
satokf9f01002011-05-19 21:31:50 +09001143 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1144 synchronized (mMethodMap) {
1145 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1146 for (int i = 0; i < spans.length; ++i) {
1147 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001148 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001149 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001150 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001151 }
1152 }
1153 }
1154 }
1155
satoke7c6998e2011-06-03 17:57:59 +09001156 @Override
satokf9f01002011-05-19 21:31:50 +09001157 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1158 synchronized (mMethodMap) {
1159 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1160 // TODO: Do not send the intent if the process of the targetImi is already dead.
1161 if (targetImi != null) {
1162 final String[] suggestions = span.getSuggestions();
1163 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001164 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001165 final Intent intent = new Intent();
1166 // Ensures that only a class in the original IME package will receive the
1167 // notification.
satok42c5a162011-05-26 16:46:14 +09001168 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001169 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1170 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1171 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1172 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1173 mContext.sendBroadcast(intent);
1174 return true;
1175 }
1176 }
1177 return false;
1178 }
1179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001181 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1182 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1183 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1184 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001186 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001187 // There is no input method selected, try to choose new applicable input method.
1188 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1189 id = Settings.Secure.getString(mContext.getContentResolver(),
1190 Settings.Secure.DEFAULT_INPUT_METHOD);
1191 }
1192 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 try {
satokab751aa2010-09-14 19:17:36 +09001194 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001196 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001197 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001198 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
satokf3db1af2010-11-23 13:34:33 +09001200 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001201 } else {
1202 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001203 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001204 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
satokab751aa2010-09-14 19:17:36 +09001208 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 InputMethodInfo info = mMethodMap.get(id);
1210 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001211 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001215 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001216 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1217 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001218 }
1219 if (subtype != mCurrentSubtype) {
1220 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001221 if (subtype != null) {
1222 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1223 }
satokcd7cd292010-11-20 15:46:23 +09001224 if (mCurMethod != null) {
1225 try {
satoke40dea02011-01-30 01:14:02 +09001226 final Configuration conf = mRes.getConfiguration();
1227 final boolean haveHardKeyboard = conf.keyboard
1228 != Configuration.KEYBOARD_NOKEYS;
1229 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001230 && conf.hardKeyboardHidden
1231 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001232 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1233 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1234 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001235 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1236 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001237 // If subtype is null, try to find the most applicable one from
1238 // getCurrentInputMethodSubtype.
1239 if (subtype == null) {
1240 subtype = getCurrentInputMethodSubtype();
1241 }
1242 mCurMethod.changeInputMethodSubtype(subtype);
1243 } catch (RemoteException e) {
1244 return;
satokab751aa2010-09-14 19:17:36 +09001245 }
1246 }
1247 }
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 return;
1250 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 final long ident = Binder.clearCallingIdentity();
1253 try {
satokab751aa2010-09-14 19:17:36 +09001254 // Set a subtype to this input method.
1255 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001256 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1257 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1258 // because mCurMethodId is stored as a history in
1259 // setSelectedInputMethodAndSubtypeLocked().
1260 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261
1262 if (ActivityManagerNative.isSystemReady()) {
1263 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001264 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 intent.putExtra("input_method_id", id);
1266 mContext.sendBroadcast(intent);
1267 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001268 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 } finally {
1270 Binder.restoreCallingIdentity(ident);
1271 }
1272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
satok42c5a162011-05-26 16:46:14 +09001274 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001275 public boolean showSoftInput(IInputMethodClient client, int flags,
1276 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001277 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 long ident = Binder.clearCallingIdentity();
1279 try {
1280 synchronized (mMethodMap) {
1281 if (mCurClient == null || client == null
1282 || mCurClient.client.asBinder() != client.asBinder()) {
1283 try {
1284 // We need to check if this is the current client with
1285 // focus in the window manager, to allow this call to
1286 // be made before input is started in it.
1287 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001288 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001289 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001292 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001295
Joe Onorato8a9b2202010-02-26 18:56:32 -08001296 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001297 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 } finally {
1300 Binder.restoreCallingIdentity(ident);
1301 }
1302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001303
The Android Open Source Project4df24232009-03-05 14:34:35 -08001304 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 mShowRequested = true;
1306 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1307 mShowExplicitlyRequested = true;
1308 }
1309 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1310 mShowExplicitlyRequested = true;
1311 mShowForced = true;
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
Dianne Hackborncc278702009-09-02 23:07:23 -07001314 if (!mSystemReady) {
1315 return false;
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Project4df24232009-03-05 14:34:35 -08001318 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001320 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1321 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1322 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001324 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 } else if (mHaveConnection && SystemClock.uptimeMillis()
1326 < (mLastBindTime+TIME_TO_RECONNECT)) {
1327 // The client has asked to have the input method shown, but
1328 // we have been sitting here too long with a connection to the
1329 // service and no interface received, so let's disconnect/connect
1330 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 SystemClock.uptimeMillis()-mLastBindTime,1);
1333 mContext.unbindService(this);
1334 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
The Android Open Source Project4df24232009-03-05 14:34:35 -08001337 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
satok42c5a162011-05-26 16:46:14 +09001340 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001341 public boolean hideSoftInput(IInputMethodClient client, int flags,
1342 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001343 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 long ident = Binder.clearCallingIdentity();
1345 try {
1346 synchronized (mMethodMap) {
1347 if (mCurClient == null || client == null
1348 || mCurClient.client.asBinder() != client.asBinder()) {
1349 try {
1350 // We need to check if this is the current client with
1351 // focus in the window manager, to allow this call to
1352 // be made before input is started in it.
1353 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001354 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1355 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001356 mImeWindowVis = 0;
1357 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1358 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001362 mImeWindowVis = 0;
1363 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001364 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
Joe Onorato8a9b2202010-02-26 18:56:32 -08001368 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001369 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
1371 } finally {
1372 Binder.restoreCallingIdentity(ident);
1373 }
1374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001375
The Android Open Source Project4df24232009-03-05 14:34:35 -08001376 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1378 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001381 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001384 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001386 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001388 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001390 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1391 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1392 res = true;
1393 } else {
1394 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396 mInputShown = false;
1397 mShowRequested = false;
1398 mShowExplicitlyRequested = false;
1399 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001400 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402
satok42c5a162011-05-26 16:46:14 +09001403 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001404 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1406 boolean first, int windowFlags) {
1407 long ident = Binder.clearCallingIdentity();
1408 try {
1409 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001410 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 + " viewHasFocus=" + viewHasFocus
1412 + " isTextEditor=" + isTextEditor
1413 + " softInputMode=#" + Integer.toHexString(softInputMode)
1414 + " first=" + first + " flags=#"
1415 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 if (mCurClient == null || client == null
1418 || mCurClient.client.asBinder() != client.asBinder()) {
1419 try {
1420 // We need to check if this is the current client with
1421 // focus in the window manager, to allow this call to
1422 // be made before input is started in it.
1423 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 return;
1426 }
1427 } catch (RemoteException e) {
1428 }
1429 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001430
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001431 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001432 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001433 return;
1434 }
1435 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001437 // Should we auto-show the IME even if the caller has not
1438 // specified what should be done with it?
1439 // We only do this automatically if the window can resize
1440 // to accommodate the IME (so what the user sees will give
1441 // them good context without input information being obscured
1442 // by the IME) or if running on a large screen where there
1443 // is more room for the target window + IME.
1444 final boolean doAutoShow =
1445 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1446 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1447 || mRes.getConfiguration().isLayoutSizeAtLeast(
1448 Configuration.SCREENLAYOUT_SIZE_LARGE);
1449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1451 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001452 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1454 // There is no focus view, and this window will
1455 // be behind any soft input window, so hide the
1456 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001457 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001458 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001460 } else if (isTextEditor && doAutoShow && (softInputMode &
1461 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 // There is a focus view, and we are navigating forward
1463 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001464 // We only do this automatically if the window an resize
1465 // to accomodate the IME (so what the user sees will give
1466 // them good context without input information being obscured
1467 // by the IME) or if running on a large screen where there
1468 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001470 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
1472 break;
1473 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1474 // Do nothing.
1475 break;
1476 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1477 if ((softInputMode &
1478 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001479 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001480 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482 break;
1483 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001484 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001485 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 break;
1487 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1488 if ((softInputMode &
1489 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001491 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
1493 break;
1494 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001496 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 break;
1498 }
1499 }
1500 } finally {
1501 Binder.restoreCallingIdentity(ident);
1502 }
1503 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001504
satok42c5a162011-05-26 16:46:14 +09001505 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1507 synchronized (mMethodMap) {
1508 if (mCurClient == null || client == null
1509 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001510 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001511 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513
satok440aab52010-11-25 09:43:11 +09001514 // Always call subtype picker, because subtype picker is a superset of input method
1515 // picker.
satokab751aa2010-09-14 19:17:36 +09001516 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1517 }
1518 }
1519
satok42c5a162011-05-26 16:46:14 +09001520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001522 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1523 }
1524
satok42c5a162011-05-26 16:46:14 +09001525 @Override
satok28203512010-11-24 11:06:49 +09001526 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1527 synchronized (mMethodMap) {
1528 if (subtype != null) {
1529 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1530 mMethodMap.get(id), subtype.hashCode()));
1531 } else {
1532 setInputMethod(token, id);
1533 }
1534 }
satokab751aa2010-09-14 19:17:36 +09001535 }
1536
satok42c5a162011-05-26 16:46:14 +09001537 @Override
satokb416a71e2010-11-25 20:42:14 +09001538 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001539 IInputMethodClient client, String inputMethodId) {
satokb416a71e2010-11-25 20:42:14 +09001540 synchronized (mMethodMap) {
1541 if (mCurClient == null || client == null
1542 || mCurClient.client.asBinder() != client.asBinder()) {
1543 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1544 }
satok7fee71f2010-12-17 18:54:26 +09001545 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1546 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09001547 }
1548 }
1549
satok4fc87d62011-05-20 16:13:43 +09001550 @Override
satok735cf382010-11-11 20:40:09 +09001551 public boolean switchToLastInputMethod(IBinder token) {
1552 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001553 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001554 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001555 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001556 lastImi = mMethodMap.get(lastIme.first);
1557 } else {
1558 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001559 }
satok4fc87d62011-05-20 16:13:43 +09001560 String targetLastImiId = null;
1561 int subtypeId = NOT_A_SUBTYPE_ID;
1562 if (lastIme != null && lastImi != null) {
1563 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1564 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1565 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1566 : mCurrentSubtype.hashCode();
1567 // If the last IME is the same as the current IME and the last subtype is not
1568 // defined, there is no need to switch to the last IME.
1569 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1570 targetLastImiId = lastIme.first;
1571 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1572 }
1573 }
1574
1575 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1576 // This is a safety net. If the currentSubtype can't be added to the history
1577 // and the framework couldn't find the last ime, we will make the last ime be
1578 // the most applicable enabled keyboard subtype of the system imes.
1579 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1580 if (enabled != null) {
1581 final int N = enabled.size();
1582 final String locale = mCurrentSubtype == null
1583 ? mRes.getConfiguration().locale.toString()
1584 : mCurrentSubtype.getLocale();
1585 for (int i = 0; i < N; ++i) {
1586 final InputMethodInfo imi = enabled.get(i);
1587 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1588 InputMethodSubtype keyboardSubtype =
1589 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1590 SUBTYPE_MODE_KEYBOARD, locale, true);
1591 if (keyboardSubtype != null) {
1592 targetLastImiId = imi.getId();
1593 subtypeId = getSubtypeIdFromHashCode(
1594 imi, keyboardSubtype.hashCode());
1595 if(keyboardSubtype.getLocale().equals(locale)) {
1596 break;
1597 }
1598 }
1599 }
1600 }
1601 }
1602 }
1603
1604 if (!TextUtils.isEmpty(targetLastImiId)) {
1605 if (DEBUG) {
1606 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1607 + ", from: " + mCurMethodId + ", " + subtypeId);
1608 }
1609 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1610 return true;
1611 } else {
1612 return false;
1613 }
satok735cf382010-11-11 20:40:09 +09001614 }
1615 }
1616
satoke7c6998e2011-06-03 17:57:59 +09001617 @Override
satok68f1b782011-04-11 14:26:04 +09001618 public InputMethodSubtype getLastInputMethodSubtype() {
1619 synchronized (mMethodMap) {
1620 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1621 // TODO: Handle the case of the last IME with no subtypes
1622 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1623 || TextUtils.isEmpty(lastIme.second)) return null;
1624 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1625 if (lastImi == null) return null;
1626 try {
1627 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001628 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1629 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1630 return null;
1631 }
1632 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001633 } catch (NumberFormatException e) {
1634 return null;
1635 }
1636 }
1637 }
1638
satoke7c6998e2011-06-03 17:57:59 +09001639 @Override
satok91e88122011-07-18 11:11:42 +09001640 public boolean setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
1641 // By this IPC call, only a process which shares the same uid with the IME can add
1642 // additional input method subtypes to the IME.
1643 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return false;
satoke7c6998e2011-06-03 17:57:59 +09001644 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001645 final InputMethodInfo imi = mMethodMap.get(imiId);
satoke7c6998e2011-06-03 17:57:59 +09001646 if (imi == null) return false;
satok91e88122011-07-18 11:11:42 +09001647 final PackageManager pm = mContext.getPackageManager();
1648 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1649 if (packageInfos != null) {
1650 final int packageNum = packageInfos.length;
1651 for (int i = 0; i < packageNum; ++i) {
1652 if (packageInfos[i].equals(imi.getPackageName())) {
1653 mFileManager.addInputMethodSubtypes(imi, subtypes);
1654 buildInputMethodListLocked(mMethodList, mMethodMap);
1655 return true;
1656 }
1657 }
1658 }
satoke7c6998e2011-06-03 17:57:59 +09001659 }
satok91e88122011-07-18 11:11:42 +09001660 return false;
satoke7c6998e2011-06-03 17:57:59 +09001661 }
1662
satok28203512010-11-24 11:06:49 +09001663 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 synchronized (mMethodMap) {
1665 if (token == null) {
1666 if (mContext.checkCallingOrSelfPermission(
1667 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1668 != PackageManager.PERMISSION_GRANTED) {
1669 throw new SecurityException(
1670 "Using null token requires permission "
1671 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1672 }
1673 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001674 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1675 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 return;
1677 }
1678
1679 long ident = Binder.clearCallingIdentity();
1680 try {
satokab751aa2010-09-14 19:17:36 +09001681 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } finally {
1683 Binder.restoreCallingIdentity(ident);
1684 }
1685 }
1686 }
1687
satok42c5a162011-05-26 16:46:14 +09001688 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 public void hideMySoftInput(IBinder token, int flags) {
1690 synchronized (mMethodMap) {
1691 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001692 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1693 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 return;
1695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 long ident = Binder.clearCallingIdentity();
1697 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 hideCurrentInputLocked(flags, null);
1699 } finally {
1700 Binder.restoreCallingIdentity(ident);
1701 }
1702 }
1703 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001704
satok42c5a162011-05-26 16:46:14 +09001705 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001706 public void showMySoftInput(IBinder token, int flags) {
1707 synchronized (mMethodMap) {
1708 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001709 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1710 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 return;
1712 }
1713 long ident = Binder.clearCallingIdentity();
1714 try {
1715 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 } finally {
1717 Binder.restoreCallingIdentity(ident);
1718 }
1719 }
1720 }
1721
1722 void setEnabledSessionInMainThread(SessionState session) {
1723 if (mEnabledSession != session) {
1724 if (mEnabledSession != null) {
1725 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001726 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 mEnabledSession.method.setSessionEnabled(
1728 mEnabledSession.session, false);
1729 } catch (RemoteException e) {
1730 }
1731 }
1732 mEnabledSession = session;
1733 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001734 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 session.method.setSessionEnabled(
1736 session.session, true);
1737 } catch (RemoteException e) {
1738 }
1739 }
1740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
satok42c5a162011-05-26 16:46:14 +09001742 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 public boolean handleMessage(Message msg) {
1744 HandlerCaller.SomeArgs args;
1745 switch (msg.what) {
1746 case MSG_SHOW_IM_PICKER:
1747 showInputMethodMenu();
1748 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001749
satokab751aa2010-09-14 19:17:36 +09001750 case MSG_SHOW_IM_SUBTYPE_PICKER:
1751 showInputMethodSubtypeMenu();
1752 return true;
1753
satok47a44912010-10-06 16:03:58 +09001754 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001755 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001756 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001757 return true;
1758
1759 case MSG_SHOW_IM_CONFIG:
1760 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001761 return true;
1762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 case MSG_UNBIND_INPUT:
1766 try {
1767 ((IInputMethod)msg.obj).unbindInput();
1768 } catch (RemoteException e) {
1769 // There is nothing interesting about the method dying.
1770 }
1771 return true;
1772 case MSG_BIND_INPUT:
1773 args = (HandlerCaller.SomeArgs)msg.obj;
1774 try {
1775 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1776 } catch (RemoteException e) {
1777 }
1778 return true;
1779 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1783 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 } catch (RemoteException e) {
1785 }
1786 return true;
1787 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001790 ((IInputMethod)args.arg1).hideSoftInput(0,
1791 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 } catch (RemoteException e) {
1793 }
1794 return true;
1795 case MSG_ATTACH_TOKEN:
1796 args = (HandlerCaller.SomeArgs)msg.obj;
1797 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001798 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1800 } catch (RemoteException e) {
1801 }
1802 return true;
1803 case MSG_CREATE_SESSION:
1804 args = (HandlerCaller.SomeArgs)msg.obj;
1805 try {
1806 ((IInputMethod)args.arg1).createSession(
1807 (IInputMethodCallback)args.arg2);
1808 } catch (RemoteException e) {
1809 }
1810 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 case MSG_START_INPUT:
1814 args = (HandlerCaller.SomeArgs)msg.obj;
1815 try {
1816 SessionState session = (SessionState)args.arg1;
1817 setEnabledSessionInMainThread(session);
1818 session.method.startInput((IInputContext)args.arg2,
1819 (EditorInfo)args.arg3);
1820 } catch (RemoteException e) {
1821 }
1822 return true;
1823 case MSG_RESTART_INPUT:
1824 args = (HandlerCaller.SomeArgs)msg.obj;
1825 try {
1826 SessionState session = (SessionState)args.arg1;
1827 setEnabledSessionInMainThread(session);
1828 session.method.restartInput((IInputContext)args.arg2,
1829 (EditorInfo)args.arg3);
1830 } catch (RemoteException e) {
1831 }
1832 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 case MSG_UNBIND_METHOD:
1837 try {
1838 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1839 } catch (RemoteException e) {
1840 // There is nothing interesting about the last client dying.
1841 }
1842 return true;
1843 case MSG_BIND_METHOD:
1844 args = (HandlerCaller.SomeArgs)msg.obj;
1845 try {
1846 ((IInputMethodClient)args.arg1).onBindMethod(
1847 (InputBindResult)args.arg2);
1848 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001849 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851 return true;
1852 }
1853 return false;
1854 }
1855
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001856 private boolean isSystemIme(InputMethodInfo inputMethod) {
1857 return (inputMethod.getServiceInfo().applicationInfo.flags
1858 & ApplicationInfo.FLAG_SYSTEM) != 0;
1859 }
1860
Ken Wakasa586f0512011-01-20 22:31:01 +09001861 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1862 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1863 final int subtypeCount = imi.getSubtypeCount();
1864 for (int i = 0; i < subtypeCount; ++i) {
1865 subtypes.add(imi.getSubtypeAt(i));
1866 }
1867 return subtypes;
1868 }
1869
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001870 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001871 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001872 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001873 // We'd prefer to fall back on a system IME, since that is safer.
1874 int i=enabled.size();
1875 while (i > 0) {
1876 i--;
1877 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1878 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1879 break;
1880 }
1881 }
satokab751aa2010-09-14 19:17:36 +09001882 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001883 if (DEBUG) {
1884 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1885 }
satok723a27e2010-11-11 14:58:11 +09001886 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001887 return true;
1888 }
1889
1890 return false;
1891 }
1892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1894 HashMap<String, InputMethodInfo> map) {
1895 list.clear();
1896 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001899 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001900 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1901 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1902 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1903 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904
1905 List<ResolveInfo> services = pm.queryIntentServices(
1906 new Intent(InputMethod.SERVICE_INTERFACE),
1907 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001908
satoke7c6998e2011-06-03 17:57:59 +09001909 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1910 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 for (int i = 0; i < services.size(); ++i) {
1912 ResolveInfo ri = services.get(i);
1913 ServiceInfo si = ri.serviceInfo;
1914 ComponentName compName = new ComponentName(si.packageName, si.name);
1915 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1916 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001917 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 + ": it does not require the permission "
1919 + android.Manifest.permission.BIND_INPUT_METHOD);
1920 continue;
1921 }
1922
Joe Onorato8a9b2202010-02-26 18:56:32 -08001923 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924
1925 try {
satoke7c6998e2011-06-03 17:57:59 +09001926 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001928 final String id = p.getId();
1929 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930
Amith Yamasanie861ec12010-03-24 21:39:27 -07001931 // System IMEs are enabled by default, unless there's a hard keyboard
1932 // and the system IME was explicitly disabled
1933 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1934 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001935 }
1936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001938 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001942 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001944 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001947
1948 String defaultIme = Settings.Secure.getString(mContext
1949 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001950 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001951 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001952 updateFromSettingsLocked();
1953 }
1954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001958
satokab751aa2010-09-14 19:17:36 +09001959 private void showInputMethodMenu() {
1960 showInputMethodMenuInternal(false);
1961 }
1962
1963 private void showInputMethodSubtypeMenu() {
1964 showInputMethodMenuInternal(true);
1965 }
1966
satok217f5482010-12-15 05:19:19 +09001967 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001968 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001969 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001970 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1971 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001972 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001973 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001974 }
satok217f5482010-12-15 05:19:19 +09001975 mContext.startActivity(intent);
1976 }
1977
1978 private void showConfigureInputMethods() {
1979 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1980 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1981 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1982 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001983 mContext.startActivity(intent);
1984 }
1985
satokab751aa2010-09-14 19:17:36 +09001986 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 String lastInputMethodId = Settings.Secure.getString(context
1994 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001995 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001996 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001998 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001999 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2000 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002001 if (immis == null || immis.size() == 0) {
2002 return;
2003 }
2004
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002005 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006
Ken Wakasa761eb372011-03-04 19:06:18 +09002007 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2008 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2009 new Comparator<InputMethodInfo>() {
2010 @Override
2011 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2012 if (imi2 == null) return 0;
2013 if (imi1 == null) return 1;
2014 if (pm == null) {
2015 return imi1.getId().compareTo(imi2.getId());
2016 }
2017 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2018 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2019 return imiId1.toString().compareTo(imiId2.toString());
2020 }
2021 });
satok913a8922010-08-26 21:53:41 +09002022
Ken Wakasa761eb372011-03-04 19:06:18 +09002023 sortedImmis.putAll(immis);
2024
2025 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2026 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2027
2028 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002029 if (imi == null) continue;
2030 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002031 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002032 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2033 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002034 }
satokbb4aa062011-01-19 21:40:27 +09002035 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002036 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002037 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002038 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002039 if (DEBUG) {
2040 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2041 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002042 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002043 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2044 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2045 // We show all enabled IMEs and subtypes when an IME is shown.
2046 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002047 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002048 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002049 final String mode = subtype.getMode();
2050 title = TextUtils.concat(subtype.getDisplayName(context,
2051 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2052 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002053 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2054 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002055 // Removing this subtype from enabledSubtypeSet because we no longer
2056 // need to add an entry of this subtype to imList to avoid duplicated
2057 // entries.
2058 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002059 }
satokab751aa2010-09-14 19:17:36 +09002060 }
2061 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002062 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2063 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002064 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002065 }
satok913a8922010-08-26 21:53:41 +09002066
Ken Wakasa761eb372011-03-04 19:06:18 +09002067 final int N = imList.size();
2068 mItems = new CharSequence[N];
2069 for (int i = 0; i < N; ++i) {
2070 mItems[i] = imList.get(i).first;
2071 }
satokab751aa2010-09-14 19:17:36 +09002072 mIms = new InputMethodInfo[N];
2073 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002074 int checkedItem = 0;
2075 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002076 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002077 mIms[i] = value.first;
2078 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002079 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002080 int subtypeId = mSubtypeIds[i];
2081 if ((subtypeId == NOT_A_SUBTYPE_ID)
2082 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2083 || (subtypeId == lastInputMethodSubtypeId)) {
2084 checkedItem = i;
2085 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 }
satokab751aa2010-09-14 19:17:36 +09002088
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002089 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002090 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002091 public void onClick(DialogInterface dialog, int which) {
2092 hideInputMethodMenu();
2093 }
2094 };
satokd87c2592010-09-29 11:52:06 +09002095
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002096 TypedArray a = context.obtainStyledAttributes(null,
2097 com.android.internal.R.styleable.DialogPreference,
2098 com.android.internal.R.attr.alertDialogStyle, 0);
2099 mDialogBuilder = new AlertDialog.Builder(context)
2100 .setTitle(com.android.internal.R.string.select_input_method)
2101 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002102 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002103 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002105 }
2106 })
2107 .setIcon(a.getDrawable(
2108 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2109 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002110
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002111 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2112 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002113 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002114 public void onClick(DialogInterface dialog, int which) {
2115 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002116 if (mIms == null || mIms.length <= which
2117 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002118 return;
2119 }
2120 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002121 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002122 hideInputMethodMenu();
2123 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002124 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002125 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002126 subtypeId = NOT_A_SUBTYPE_ID;
2127 }
2128 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002129 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002132 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133
satokf90a33e2011-07-19 11:55:52 +09002134 if (showSubtypes && !(mKeyguardManager.isKeyguardLocked()
2135 && mKeyguardManager.isKeyguardSecure())) {
satok82beadf2010-12-27 19:03:06 +09002136 mDialogBuilder.setPositiveButton(
2137 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002138 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002139 @Override
satok7f35c8c2010-10-07 21:13:11 +09002140 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002141 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002142 }
2143 });
2144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002146 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 mSwitchingDialog.getWindow().setType(
2148 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002149 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 mSwitchingDialog.show();
2151 }
2152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002155 synchronized (mMethodMap) {
2156 hideInputMethodMenuLocked();
2157 }
2158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002159
The Android Open Source Project10592532009-03-18 17:39:46 -07002160 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002161 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162
The Android Open Source Project10592532009-03-18 17:39:46 -07002163 if (mSwitchingDialog != null) {
2164 mSwitchingDialog.dismiss();
2165 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project10592532009-03-18 17:39:46 -07002168 mDialogBuilder = null;
2169 mItems = null;
2170 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002174
satok42c5a162011-05-26 16:46:14 +09002175 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 public boolean setInputMethodEnabled(String id, boolean enabled) {
2177 synchronized (mMethodMap) {
2178 if (mContext.checkCallingOrSelfPermission(
2179 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2180 != PackageManager.PERMISSION_GRANTED) {
2181 throw new SecurityException(
2182 "Requires permission "
2183 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2184 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 long ident = Binder.clearCallingIdentity();
2187 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002188 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 } finally {
2190 Binder.restoreCallingIdentity(ident);
2191 }
2192 }
2193 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002194
2195 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2196 // Make sure this is a valid input method.
2197 InputMethodInfo imm = mMethodMap.get(id);
2198 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002199 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002200 }
2201
satokd87c2592010-09-29 11:52:06 +09002202 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2203 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002204
satokd87c2592010-09-29 11:52:06 +09002205 if (enabled) {
2206 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2207 if (pair.first.equals(id)) {
2208 // We are enabling this input method, but it is already enabled.
2209 // Nothing to do. The previous state was enabled.
2210 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002211 }
2212 }
satokd87c2592010-09-29 11:52:06 +09002213 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2214 // Previous state was disabled.
2215 return false;
2216 } else {
2217 StringBuilder builder = new StringBuilder();
2218 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2219 builder, enabledInputMethodsList, id)) {
2220 // Disabled input method is currently selected, switch to another one.
2221 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2222 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002223 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2224 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2225 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002226 }
2227 // Previous state was enabled.
2228 return true;
2229 } else {
2230 // We are disabling the input method but it is already disabled.
2231 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002232 return false;
2233 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002234 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002235 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002236
satok57ffc002011-01-25 00:11:47 +09002237 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2238 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002239 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002240 }
2241
satok723a27e2010-11-11 14:58:11 +09002242 private void saveCurrentInputMethodAndSubtypeToHistory() {
2243 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2244 if (mCurrentSubtype != null) {
2245 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2246 }
satok57ffc002011-01-25 00:11:47 +09002247 if (canAddToLastInputMethod(mCurrentSubtype)) {
2248 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2249 }
satokab751aa2010-09-14 19:17:36 +09002250 }
2251
satok723a27e2010-11-11 14:58:11 +09002252 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2253 boolean setSubtypeOnly) {
2254 // Update the history of InputMethod and Subtype
2255 saveCurrentInputMethodAndSubtypeToHistory();
2256
2257 // Set Subtype here
2258 if (imi == null || subtypeId < 0) {
2259 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002260 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002261 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002262 if (subtypeId < imi.getSubtypeCount()) {
2263 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2264 mSettings.putSelectedSubtype(subtype.hashCode());
2265 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002266 } else {
2267 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2268 mCurrentSubtype = null;
2269 }
satokab751aa2010-09-14 19:17:36 +09002270 }
satok723a27e2010-11-11 14:58:11 +09002271
2272 if (!setSubtypeOnly) {
2273 // Set InputMethod here
2274 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2275 }
2276 }
2277
2278 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2279 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2280 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2281 // newDefaultIme is empty when there is no candidate for the selected IME.
2282 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2283 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2284 if (subtypeHashCode != null) {
2285 try {
2286 lastSubtypeId = getSubtypeIdFromHashCode(
2287 imi, Integer.valueOf(subtypeHashCode));
2288 } catch (NumberFormatException e) {
2289 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2290 }
2291 }
2292 }
2293 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002294 }
2295
2296 private int getSelectedInputMethodSubtypeId(String id) {
2297 InputMethodInfo imi = mMethodMap.get(id);
2298 if (imi == null) {
2299 return NOT_A_SUBTYPE_ID;
2300 }
satokab751aa2010-09-14 19:17:36 +09002301 int subtypeId;
2302 try {
2303 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2304 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2305 } catch (SettingNotFoundException e) {
2306 return NOT_A_SUBTYPE_ID;
2307 }
satok723a27e2010-11-11 14:58:11 +09002308 return getSubtypeIdFromHashCode(imi, subtypeId);
2309 }
2310
2311 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002312 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002313 final int subtypeCount = imi.getSubtypeCount();
2314 for (int i = 0; i < subtypeCount; ++i) {
2315 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002316 if (subtypeHashCode == ims.hashCode()) {
2317 return i;
2318 }
satokab751aa2010-09-14 19:17:36 +09002319 }
2320 }
2321 return NOT_A_SUBTYPE_ID;
2322 }
2323
satokdf31ae62011-01-15 06:19:44 +09002324 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2325 Resources res, List<InputMethodSubtype> subtypes) {
2326 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002327 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2328 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2329 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002330 final int N = subtypes.size();
2331 boolean containsKeyboardSubtype = false;
2332 for (int i = 0; i < N; ++i) {
2333 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002334 final String locale = subtype.getLocale();
2335 final String mode = subtype.getMode();
2336 // When system locale starts with subtype's locale, that subtype will be applicable
2337 // for system locale
2338 // For instance, it's clearly applicable for cases like system locale = en_US and
2339 // subtype = en, but it is not necessarily considered applicable for cases like system
2340 // locale = en and subtype = en_US.
2341 // We just call systemLocale.startsWith(locale) in this function because there is no
2342 // need to find applicable subtypes aggressively unlike
2343 // findLastResortApplicableSubtypeLocked.
2344 if (systemLocale.startsWith(locale)) {
2345 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2346 // If more applicable subtypes are contained, skip.
2347 if (applicableSubtype != null
2348 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2349 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002350 if (!containsKeyboardSubtype
2351 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2352 containsKeyboardSubtype = true;
2353 }
2354 }
2355 }
satok4a28bde2011-06-29 21:03:40 +09002356 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002357 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002358 if (!containsKeyboardSubtype) {
2359 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002360 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002361 if (lastResortKeyboardSubtype != null) {
2362 applicableSubtypes.add(lastResortKeyboardSubtype);
2363 }
2364 }
2365 return applicableSubtypes;
2366 }
2367
satok4e4569d2010-11-19 18:45:53 +09002368 /**
2369 * If there are no selected subtypes, tries finding the most applicable one according to the
2370 * given locale.
2371 * @param subtypes this function will search the most applicable subtype in subtypes
2372 * @param mode subtypes will be filtered by mode
2373 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09002374 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2375 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002376 * @return the most applicable subtypeId
2377 */
satokdf31ae62011-01-15 06:19:44 +09002378 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2379 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09002380 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002381 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002382 return null;
satok8fbb1e82010-11-02 23:15:58 +09002383 }
satok4e4569d2010-11-19 18:45:53 +09002384 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002385 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002386 }
satok8fbb1e82010-11-02 23:15:58 +09002387 final String language = locale.substring(0, 2);
2388 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002389 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09002390 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002391 final int N = subtypes.size();
2392 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002393 InputMethodSubtype subtype = subtypes.get(i);
2394 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002395 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2396 // and all subtypes with all modes can be candidates.
2397 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09002398 if (firstMatchedModeSubtype == null) {
2399 firstMatchedModeSubtype = subtype;
2400 }
satok9ef02832010-11-04 21:17:48 +09002401 if (locale.equals(subtypeLocale)) {
2402 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002403 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002404 break;
2405 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2406 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002407 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002408 partialMatchFound = true;
2409 }
satok8fbb1e82010-11-02 23:15:58 +09002410 }
2411 }
2412
satok7599a7fb2010-12-22 13:45:23 +09002413 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2414 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002415 }
2416
satok8fbb1e82010-11-02 23:15:58 +09002417 // The first subtype applicable to the system locale will be defined as the most applicable
2418 // subtype.
2419 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002420 if (applicableSubtype != null) {
2421 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2422 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2423 }
satok8fbb1e82010-11-02 23:15:58 +09002424 }
satokcd7cd292010-11-20 15:46:23 +09002425 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002426 }
2427
satok4e4569d2010-11-19 18:45:53 +09002428 // If there are no selected shortcuts, tries finding the most applicable ones.
2429 private Pair<InputMethodInfo, InputMethodSubtype>
2430 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2431 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2432 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002433 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002434 boolean foundInSystemIME = false;
2435
2436 // Search applicable subtype for each InputMethodInfo
2437 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002438 final String imiId = imi.getId();
2439 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2440 continue;
2441 }
satokcd7cd292010-11-20 15:46:23 +09002442 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002443 final List<InputMethodSubtype> enabledSubtypes =
2444 getEnabledInputMethodSubtypeList(imi, true);
2445 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002446 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002447 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002448 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002449 }
satokdf31ae62011-01-15 06:19:44 +09002450 // 2. Search by the system locale from enabledSubtypes.
2451 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002452 if (subtype == null) {
2453 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002454 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002455 }
satok7599a7fb2010-12-22 13:45:23 +09002456 // 4. Search by the current subtype's locale from all subtypes.
2457 if (subtype == null && mCurrentSubtype != null) {
2458 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002459 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002460 }
2461 // 5. Search by the system locale from all subtypes.
2462 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002463 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09002464 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002465 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002466 }
satokcd7cd292010-11-20 15:46:23 +09002467 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002468 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002469 // The current input method is the most applicable IME.
2470 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002471 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002472 break;
satok7599a7fb2010-12-22 13:45:23 +09002473 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002474 // The system input method is 2nd applicable IME.
2475 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002476 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002477 if ((imi.getServiceInfo().applicationInfo.flags
2478 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2479 foundInSystemIME = true;
2480 }
satok4e4569d2010-11-19 18:45:53 +09002481 }
2482 }
2483 }
2484 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002485 if (mostApplicableIMI != null) {
2486 Slog.w(TAG, "Most applicable shortcut input method was:"
2487 + mostApplicableIMI.getId());
2488 if (mostApplicableSubtype != null) {
2489 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2490 + "," + mostApplicableSubtype.getMode() + ","
2491 + mostApplicableSubtype.getLocale());
2492 }
2493 }
satok4e4569d2010-11-19 18:45:53 +09002494 }
satokcd7cd292010-11-20 15:46:23 +09002495 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002496 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002497 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002498 } else {
2499 return null;
2500 }
2501 }
2502
satokab751aa2010-09-14 19:17:36 +09002503 /**
2504 * @return Return the current subtype of this input method.
2505 */
satok42c5a162011-05-26 16:46:14 +09002506 @Override
satokab751aa2010-09-14 19:17:36 +09002507 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002508 boolean subtypeIsSelected = false;
2509 try {
2510 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2511 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2512 } catch (SettingNotFoundException e) {
2513 }
satok3ef8b292010-11-23 06:06:29 +09002514 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002515 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002516 String lastInputMethodId = Settings.Secure.getString(
2517 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002518 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2519 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002520 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2521 if (imi != null) {
2522 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002523 // the most applicable subtype from explicitly or implicitly enabled
2524 // subtypes.
2525 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2526 getEnabledInputMethodSubtypeList(imi, true);
2527 // If there is only one explicitly or implicitly enabled subtype,
2528 // just returns it.
2529 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2530 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2531 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2532 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2533 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2534 SUBTYPE_MODE_KEYBOARD, null, true);
2535 if (mCurrentSubtype == null) {
2536 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2537 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2538 true);
2539 }
2540 }
satok4e4569d2010-11-19 18:45:53 +09002541 }
satokcd7cd292010-11-20 15:46:23 +09002542 } else {
satok3ef8b292010-11-23 06:06:29 +09002543 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002544 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002545 }
satok8fbb1e82010-11-02 23:15:58 +09002546 }
satok3ef8b292010-11-23 06:06:29 +09002547 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002548 }
satokab751aa2010-09-14 19:17:36 +09002549 }
2550
satokf3db1af2010-11-23 13:34:33 +09002551 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2552 InputMethodSubtype subtype) {
2553 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2554 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2555 } else {
2556 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2557 subtypes.add(subtype);
2558 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2559 }
2560 }
2561
satok4e4569d2010-11-19 18:45:53 +09002562 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002563 @Override
satok4e4569d2010-11-19 18:45:53 +09002564 public List getShortcutInputMethodsAndSubtypes() {
2565 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002566 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002567 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002568 // If there are no selected shortcut subtypes, the framework will try to find
2569 // the most applicable subtype from all subtypes whose mode is
2570 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002571 Pair<InputMethodInfo, InputMethodSubtype> info =
2572 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2573 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09002574 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002575 ret.add(info.first);
2576 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09002577 }
satok3da92232011-01-11 22:46:30 +09002578 return ret;
satokf3db1af2010-11-23 13:34:33 +09002579 }
satokf3db1af2010-11-23 13:34:33 +09002580 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2581 ret.add(imi);
2582 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2583 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002584 }
2585 }
satokf3db1af2010-11-23 13:34:33 +09002586 return ret;
satok4e4569d2010-11-19 18:45:53 +09002587 }
2588 }
2589
satok42c5a162011-05-26 16:46:14 +09002590 @Override
satokb66d2872010-11-10 01:04:04 +09002591 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2592 synchronized (mMethodMap) {
2593 if (subtype != null && mCurMethodId != null) {
2594 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2595 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2596 if (subtypeId != NOT_A_SUBTYPE_ID) {
2597 setInputMethodLocked(mCurMethodId, subtypeId);
2598 return true;
2599 }
2600 }
2601 return false;
2602 }
2603 }
2604
satokd87c2592010-09-29 11:52:06 +09002605 /**
2606 * Utility class for putting and getting settings for InputMethod
2607 * TODO: Move all putters and getters of settings to this class.
2608 */
2609 private static class InputMethodSettings {
2610 // The string for enabled input method is saved as follows:
2611 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2612 private static final char INPUT_METHOD_SEPARATER = ':';
2613 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002614 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002615 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2616
satok723a27e2010-11-11 14:58:11 +09002617 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002618 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2619
satokdf31ae62011-01-15 06:19:44 +09002620 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002621 private final ContentResolver mResolver;
2622 private final HashMap<String, InputMethodInfo> mMethodMap;
2623 private final ArrayList<InputMethodInfo> mMethodList;
2624
2625 private String mEnabledInputMethodsStrCache;
2626
2627 private static void buildEnabledInputMethodsSettingString(
2628 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2629 String id = pair.first;
2630 ArrayList<String> subtypes = pair.second;
2631 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002632 // Inputmethod and subtypes are saved in the settings as follows:
2633 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2634 for (String subtypeId: subtypes) {
2635 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002636 }
2637 }
2638
2639 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002640 Resources res, ContentResolver resolver,
2641 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2642 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002643 mResolver = resolver;
2644 mMethodMap = methodMap;
2645 mMethodList = methodList;
2646 }
2647
2648 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2649 return createEnabledInputMethodListLocked(
2650 getEnabledInputMethodsAndSubtypeListLocked());
2651 }
2652
satok7f35c8c2010-10-07 21:13:11 +09002653 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002654 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2655 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002656 getEnabledInputMethodsAndSubtypeListLocked());
2657 }
2658
satok67ddf9c2010-11-17 09:45:54 +09002659 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2660 InputMethodInfo imi) {
2661 List<Pair<String, ArrayList<String>>> imsList =
2662 getEnabledInputMethodsAndSubtypeListLocked();
2663 ArrayList<InputMethodSubtype> enabledSubtypes =
2664 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002665 if (imi != null) {
2666 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2667 InputMethodInfo info = mMethodMap.get(imsPair.first);
2668 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002669 final int subtypeCount = info.getSubtypeCount();
2670 for (int i = 0; i < subtypeCount; ++i) {
2671 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002672 for (String s: imsPair.second) {
2673 if (String.valueOf(ims.hashCode()).equals(s)) {
2674 enabledSubtypes.add(ims);
2675 }
satok67ddf9c2010-11-17 09:45:54 +09002676 }
2677 }
satok884ef9a2010-11-18 10:39:46 +09002678 break;
satok67ddf9c2010-11-17 09:45:54 +09002679 }
satok67ddf9c2010-11-17 09:45:54 +09002680 }
2681 }
2682 return enabledSubtypes;
2683 }
2684
satokd87c2592010-09-29 11:52:06 +09002685 // At the initial boot, the settings for input methods are not set,
2686 // so we need to enable IME in that case.
2687 public void enableAllIMEsIfThereIsNoEnabledIME() {
2688 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2689 StringBuilder sb = new StringBuilder();
2690 final int N = mMethodList.size();
2691 for (int i = 0; i < N; i++) {
2692 InputMethodInfo imi = mMethodList.get(i);
2693 Slog.i(TAG, "Adding: " + imi.getId());
2694 if (i > 0) sb.append(':');
2695 sb.append(imi.getId());
2696 }
2697 putEnabledInputMethodsStr(sb.toString());
2698 }
2699 }
2700
satokbb4aa062011-01-19 21:40:27 +09002701 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002702 ArrayList<Pair<String, ArrayList<String>>> imsList
2703 = new ArrayList<Pair<String, ArrayList<String>>>();
2704 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2705 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2706 return imsList;
2707 }
satok723a27e2010-11-11 14:58:11 +09002708 mInputMethodSplitter.setString(enabledInputMethodsStr);
2709 while (mInputMethodSplitter.hasNext()) {
2710 String nextImsStr = mInputMethodSplitter.next();
2711 mSubtypeSplitter.setString(nextImsStr);
2712 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002713 ArrayList<String> subtypeHashes = new ArrayList<String>();
2714 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002715 String imeId = mSubtypeSplitter.next();
2716 while (mSubtypeSplitter.hasNext()) {
2717 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002718 }
2719 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2720 }
2721 }
2722 return imsList;
2723 }
2724
2725 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2726 if (reloadInputMethodStr) {
2727 getEnabledInputMethodsStr();
2728 }
2729 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2730 // Add in the newly enabled input method.
2731 putEnabledInputMethodsStr(id);
2732 } else {
2733 putEnabledInputMethodsStr(
2734 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2735 }
2736 }
2737
2738 /**
2739 * Build and put a string of EnabledInputMethods with removing specified Id.
2740 * @return the specified id was removed or not.
2741 */
2742 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2743 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2744 boolean isRemoved = false;
2745 boolean needsAppendSeparator = false;
2746 for (Pair<String, ArrayList<String>> ims: imsList) {
2747 String curId = ims.first;
2748 if (curId.equals(id)) {
2749 // We are disabling this input method, and it is
2750 // currently enabled. Skip it to remove from the
2751 // new list.
2752 isRemoved = true;
2753 } else {
2754 if (needsAppendSeparator) {
2755 builder.append(INPUT_METHOD_SEPARATER);
2756 } else {
2757 needsAppendSeparator = true;
2758 }
2759 buildEnabledInputMethodsSettingString(builder, ims);
2760 }
2761 }
2762 if (isRemoved) {
2763 // Update the setting with the new list of input methods.
2764 putEnabledInputMethodsStr(builder.toString());
2765 }
2766 return isRemoved;
2767 }
2768
2769 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2770 List<Pair<String, ArrayList<String>>> imsList) {
2771 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2772 for (Pair<String, ArrayList<String>> ims: imsList) {
2773 InputMethodInfo info = mMethodMap.get(ims.first);
2774 if (info != null) {
2775 res.add(info);
2776 }
2777 }
2778 return res;
2779 }
2780
satok7f35c8c2010-10-07 21:13:11 +09002781 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002782 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002783 List<Pair<String, ArrayList<String>>> imsList) {
2784 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2785 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2786 for (Pair<String, ArrayList<String>> ims : imsList) {
2787 InputMethodInfo info = mMethodMap.get(ims.first);
2788 if (info != null) {
2789 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2790 }
2791 }
2792 return res;
2793 }
2794
satokd87c2592010-09-29 11:52:06 +09002795 private void putEnabledInputMethodsStr(String str) {
2796 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2797 mEnabledInputMethodsStrCache = str;
2798 }
2799
2800 private String getEnabledInputMethodsStr() {
2801 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2802 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002803 if (DEBUG) {
2804 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2805 }
satokd87c2592010-09-29 11:52:06 +09002806 return mEnabledInputMethodsStrCache;
2807 }
satok723a27e2010-11-11 14:58:11 +09002808
2809 private void saveSubtypeHistory(
2810 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2811 StringBuilder builder = new StringBuilder();
2812 boolean isImeAdded = false;
2813 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2814 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2815 newSubtypeId);
2816 isImeAdded = true;
2817 }
2818 for (Pair<String, String> ime: savedImes) {
2819 String imeId = ime.first;
2820 String subtypeId = ime.second;
2821 if (TextUtils.isEmpty(subtypeId)) {
2822 subtypeId = NOT_A_SUBTYPE_ID_STR;
2823 }
2824 if (isImeAdded) {
2825 builder.append(INPUT_METHOD_SEPARATER);
2826 } else {
2827 isImeAdded = true;
2828 }
2829 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2830 subtypeId);
2831 }
2832 // Remove the last INPUT_METHOD_SEPARATER
2833 putSubtypeHistoryStr(builder.toString());
2834 }
2835
2836 public void addSubtypeToHistory(String imeId, String subtypeId) {
2837 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2838 for (Pair<String, String> ime: subtypeHistory) {
2839 if (ime.first.equals(imeId)) {
2840 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002841 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002842 + ime.second);
2843 }
2844 // We should break here
2845 subtypeHistory.remove(ime);
2846 break;
2847 }
2848 }
satokbb4aa062011-01-19 21:40:27 +09002849 if (DEBUG) {
2850 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2851 }
satok723a27e2010-11-11 14:58:11 +09002852 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2853 }
2854
2855 private void putSubtypeHistoryStr(String str) {
2856 if (DEBUG) {
2857 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2858 }
2859 Settings.Secure.putString(
2860 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2861 }
2862
2863 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2864 // Gets the first one from the history
2865 return getLastSubtypeForInputMethodLockedInternal(null);
2866 }
2867
2868 public String getLastSubtypeForInputMethodLocked(String imeId) {
2869 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2870 if (ime != null) {
2871 return ime.second;
2872 } else {
2873 return null;
2874 }
2875 }
2876
2877 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2878 List<Pair<String, ArrayList<String>>> enabledImes =
2879 getEnabledInputMethodsAndSubtypeListLocked();
2880 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002881 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002882 final String imeInTheHistory = imeAndSubtype.first;
2883 // If imeId is empty, returns the first IME and subtype in the history
2884 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2885 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002886 final String subtypeHashCode =
2887 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2888 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002889 if (!TextUtils.isEmpty(subtypeHashCode)) {
2890 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002891 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002892 }
2893 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2894 }
2895 }
2896 }
2897 if (DEBUG) {
2898 Slog.d(TAG, "No enabled IME found in the history");
2899 }
2900 return null;
2901 }
2902
satokdf31ae62011-01-15 06:19:44 +09002903 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002904 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2905 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2906 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002907 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2908 if (explicitlyEnabledSubtypes.size() == 0) {
2909 // If there are no explicitly enabled subtypes, applicable subtypes are
2910 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002911 InputMethodInfo ime = mMethodMap.get(imeId);
2912 // If IME is enabled and no subtypes are enabled, applicable subtypes
2913 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002914 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002915 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002916 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002917 if (implicitlySelectedSubtypes != null) {
2918 final int N = implicitlySelectedSubtypes.size();
2919 for (int i = 0; i < N; ++i) {
2920 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2921 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2922 return subtypeHashCode;
2923 }
2924 }
2925 }
2926 }
2927 } else {
satokf6cafb62011-01-17 16:29:02 +09002928 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002929 if (s.equals(subtypeHashCode)) {
2930 // If both imeId and subtypeId are enabled, return subtypeId.
2931 return s;
2932 }
satok723a27e2010-11-11 14:58:11 +09002933 }
2934 }
2935 // If imeId was enabled but subtypeId was disabled.
2936 return NOT_A_SUBTYPE_ID_STR;
2937 }
2938 }
2939 // If both imeId and subtypeId are disabled, return null
2940 return null;
2941 }
2942
2943 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2944 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2945 final String subtypeHistoryStr = getSubtypeHistoryStr();
2946 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2947 return imsList;
2948 }
2949 mInputMethodSplitter.setString(subtypeHistoryStr);
2950 while (mInputMethodSplitter.hasNext()) {
2951 String nextImsStr = mInputMethodSplitter.next();
2952 mSubtypeSplitter.setString(nextImsStr);
2953 if (mSubtypeSplitter.hasNext()) {
2954 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2955 // The first element is ime id.
2956 String imeId = mSubtypeSplitter.next();
2957 while (mSubtypeSplitter.hasNext()) {
2958 subtypeId = mSubtypeSplitter.next();
2959 break;
2960 }
2961 imsList.add(new Pair<String, String>(imeId, subtypeId));
2962 }
2963 }
2964 return imsList;
2965 }
2966
2967 private String getSubtypeHistoryStr() {
2968 if (DEBUG) {
2969 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2970 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2971 }
2972 return Settings.Secure.getString(
2973 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2974 }
2975
2976 public void putSelectedInputMethod(String imeId) {
2977 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2978 }
2979
2980 public void putSelectedSubtype(int subtypeId) {
2981 Settings.Secure.putInt(
2982 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2983 }
satokd87c2592010-09-29 11:52:06 +09002984 }
2985
satoke7c6998e2011-06-03 17:57:59 +09002986 private static class InputMethodFileManager {
2987 private static final String SYSTEM_PATH = "system";
2988 private static final String INPUT_METHOD_PATH = "inputmethod";
2989 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2990 private static final String NODE_SUBTYPES = "subtypes";
2991 private static final String NODE_SUBTYPE = "subtype";
2992 private static final String NODE_IMI = "imi";
2993 private static final String ATTR_ID = "id";
2994 private static final String ATTR_LABEL = "label";
2995 private static final String ATTR_ICON = "icon";
2996 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2997 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2998 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2999 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3000 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3001 private final HashMap<String, InputMethodInfo> mMethodMap;
3002 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3003 new HashMap<String, List<InputMethodSubtype>>();
3004 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3005 if (methodMap == null) {
3006 throw new NullPointerException("methodMap is null");
3007 }
3008 mMethodMap = methodMap;
3009 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3010 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3011 if (!inputMethodDir.mkdirs()) {
3012 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3013 }
3014 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3015 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3016 if (!subtypeFile.exists()) {
3017 // If "subtypes.xml" doesn't exist, create a blank file.
3018 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3019 methodMap);
3020 } else {
3021 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3022 }
3023 }
3024
3025 private void deleteAllInputMethodSubtypes(String imiId) {
3026 synchronized (mMethodMap) {
3027 mSubtypesMap.remove(imiId);
3028 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3029 mMethodMap);
3030 }
3031 }
3032
3033 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003034 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003035 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003036 final HashSet<InputMethodSubtype> existingSubtypes =
3037 new HashSet<InputMethodSubtype>();
3038 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3039 existingSubtypes.add(imi.getSubtypeAt(i));
3040 }
3041
satoke7c6998e2011-06-03 17:57:59 +09003042 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3043 final int N = additionalSubtypes.length;
3044 for (int i = 0; i < N; ++i) {
3045 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003046 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003047 subtypes.add(subtype);
3048 }
3049 }
satok4a28bde2011-06-29 21:03:40 +09003050 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003051 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3052 mMethodMap);
3053 }
3054 }
3055
3056 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3057 synchronized (mMethodMap) {
3058 return mSubtypesMap;
3059 }
3060 }
3061
3062 private static void writeAdditionalInputMethodSubtypes(
3063 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3064 HashMap<String, InputMethodInfo> methodMap) {
3065 // Safety net for the case that this function is called before methodMap is set.
3066 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3067 FileOutputStream fos = null;
3068 try {
3069 fos = subtypesFile.startWrite();
3070 final XmlSerializer out = new FastXmlSerializer();
3071 out.setOutput(fos, "utf-8");
3072 out.startDocument(null, true);
3073 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3074 out.startTag(null, NODE_SUBTYPES);
3075 for (String imiId : allSubtypes.keySet()) {
3076 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3077 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3078 continue;
3079 }
3080 out.startTag(null, NODE_IMI);
3081 out.attribute(null, ATTR_ID, imiId);
3082 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3083 final int N = subtypesList.size();
3084 for (int i = 0; i < N; ++i) {
3085 final InputMethodSubtype subtype = subtypesList.get(i);
3086 out.startTag(null, NODE_SUBTYPE);
3087 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3088 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3089 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3090 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3091 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3092 out.attribute(null, ATTR_IS_AUXILIARY,
3093 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3094 out.endTag(null, NODE_SUBTYPE);
3095 }
3096 out.endTag(null, NODE_IMI);
3097 }
3098 out.endTag(null, NODE_SUBTYPES);
3099 out.endDocument();
3100 subtypesFile.finishWrite(fos);
3101 } catch (java.io.IOException e) {
3102 Slog.w(TAG, "Error writing subtypes", e);
3103 if (fos != null) {
3104 subtypesFile.failWrite(fos);
3105 }
3106 }
3107 }
3108
3109 private static void readAdditionalInputMethodSubtypes(
3110 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3111 if (allSubtypes == null || subtypesFile == null) return;
3112 allSubtypes.clear();
3113 FileInputStream fis = null;
3114 try {
3115 fis = subtypesFile.openRead();
3116 final XmlPullParser parser = Xml.newPullParser();
3117 parser.setInput(fis, null);
3118 int type = parser.getEventType();
3119 // Skip parsing until START_TAG
3120 while ((type = parser.next()) != XmlPullParser.START_TAG
3121 && type != XmlPullParser.END_DOCUMENT) {}
3122 String firstNodeName = parser.getName();
3123 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3124 throw new XmlPullParserException("Xml doesn't start with subtypes");
3125 }
3126 final int depth =parser.getDepth();
3127 String currentImiId = null;
3128 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3129 while (((type = parser.next()) != XmlPullParser.END_TAG
3130 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3131 if (type != XmlPullParser.START_TAG)
3132 continue;
3133 final String nodeName = parser.getName();
3134 if (NODE_IMI.equals(nodeName)) {
3135 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3136 if (TextUtils.isEmpty(currentImiId)) {
3137 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3138 continue;
3139 }
3140 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3141 allSubtypes.put(currentImiId, tempSubtypesArray);
3142 } else if (NODE_SUBTYPE.equals(nodeName)) {
3143 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3144 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3145 continue;
3146 }
3147 final int icon = Integer.valueOf(
3148 parser.getAttributeValue(null, ATTR_ICON));
3149 final int label = Integer.valueOf(
3150 parser.getAttributeValue(null, ATTR_LABEL));
3151 final String imeSubtypeLocale =
3152 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3153 final String imeSubtypeMode =
3154 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3155 final String imeSubtypeExtraValue =
3156 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003157 final boolean isAuxiliary = "1".equals(String.valueOf(
3158 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003159 final InputMethodSubtype subtype =
3160 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3161 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3162 tempSubtypesArray.add(subtype);
3163 }
3164 }
3165 } catch (XmlPullParserException e) {
3166 Slog.w(TAG, "Error reading subtypes: " + e);
3167 return;
3168 } catch (java.io.IOException e) {
3169 Slog.w(TAG, "Error reading subtypes: " + e);
3170 return;
3171 } catch (NumberFormatException e) {
3172 Slog.w(TAG, "Error reading subtypes: " + e);
3173 return;
3174 } finally {
3175 if (fis != null) {
3176 try {
3177 fis.close();
3178 } catch (java.io.IOException e1) {
3179 Slog.w(TAG, "Failed to close.");
3180 }
3181 }
3182 }
3183 }
3184 }
3185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 @Override
3189 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3190 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3191 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3194 + Binder.getCallingPid()
3195 + ", uid=" + Binder.getCallingUid());
3196 return;
3197 }
3198
3199 IInputMethod method;
3200 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 synchronized (mMethodMap) {
3205 p.println("Current Input Method Manager state:");
3206 int N = mMethodList.size();
3207 p.println(" Input Methods:");
3208 for (int i=0; i<N; i++) {
3209 InputMethodInfo info = mMethodList.get(i);
3210 p.println(" InputMethod #" + i + ":");
3211 info.dump(p, " ");
3212 }
3213 p.println(" Clients:");
3214 for (ClientState ci : mClients.values()) {
3215 p.println(" Client " + ci + ":");
3216 p.println(" client=" + ci.client);
3217 p.println(" inputContext=" + ci.inputContext);
3218 p.println(" sessionRequested=" + ci.sessionRequested);
3219 p.println(" curSession=" + ci.curSession);
3220 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003221 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003223 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3224 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3226 + " mBoundToMethod=" + mBoundToMethod);
3227 p.println(" mCurToken=" + mCurToken);
3228 p.println(" mCurIntent=" + mCurIntent);
3229 method = mCurMethod;
3230 p.println(" mCurMethod=" + mCurMethod);
3231 p.println(" mEnabledSession=" + mEnabledSession);
3232 p.println(" mShowRequested=" + mShowRequested
3233 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3234 + " mShowForced=" + mShowForced
3235 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003236 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003238
Jeff Brownb88102f2010-09-08 11:49:43 -07003239 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 pw.flush();
3242 try {
3243 client.client.asBinder().dump(fd, args);
3244 } catch (RemoteException e) {
3245 p.println("Input method client dead: " + e);
3246 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003247 } else {
3248 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003250
Jeff Brownb88102f2010-09-08 11:49:43 -07003251 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 pw.flush();
3254 try {
3255 method.asBinder().dump(fd, args);
3256 } catch (RemoteException e) {
3257 p.println("Input method service dead: " + e);
3258 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003259 } else {
3260 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 }
3262 }
3263}