blob: 93ece705cd9511c50a10e2fdd9895f8658497349 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.pm.PackageManager;
52import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070054import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.Resources;
56import android.content.res.TypedArray;
57import android.database.ContentObserver;
satok7cfc0ed2011-06-20 21:29:36 +090058import android.graphics.BitmapFactory;
Joe Onorato857fd9b2011-01-27 15:08:35 -080059import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090061import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Handler;
63import android.os.IBinder;
64import android.os.IInterface;
65import android.os.Message;
66import android.os.Parcel;
67import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080068import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.ServiceManager;
70import android.os.SystemClock;
71import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070072import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090073import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090075import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090077import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090078import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.util.PrintWriterPrinter;
80import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090081import android.util.Slog;
82import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.view.IWindowManager;
84import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090085import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.inputmethod.InputBinding;
87import android.view.inputmethod.InputMethod;
88import android.view.inputmethod.InputMethodInfo;
89import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090090import android.view.inputmethod.InputMethodSubtype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
satoke7c6998e2011-06-03 17:57:59 +090092import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090094import java.io.FileInputStream;
95import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.io.IOException;
97import java.io.PrintWriter;
98import java.util.ArrayList;
satoke7c6998e2011-06-03 17:57:59 +090099import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900100import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900102import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.util.List;
satoke7c6998e2011-06-03 17:57:59 +0900104import java.util.Set;
satok913a8922010-08-26 21:53:41 +0900105import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107/**
108 * This class provides a system service that manages input methods.
109 */
110public class InputMethodManagerService extends IInputMethodManager.Stub
111 implements ServiceConnection, Handler.Callback {
112 static final boolean DEBUG = false;
113 static final String TAG = "InputManagerService";
114
115 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900116 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900117 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900118 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 static final int MSG_UNBIND_INPUT = 1000;
121 static final int MSG_BIND_INPUT = 1010;
122 static final int MSG_SHOW_SOFT_INPUT = 1020;
123 static final int MSG_HIDE_SOFT_INPUT = 1030;
124 static final int MSG_ATTACH_TOKEN = 1040;
125 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 static final int MSG_START_INPUT = 2000;
128 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_UNBIND_METHOD = 3000;
131 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
satokf9f01002011-05-19 21:31:50 +0900135 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
136
satokab751aa2010-09-14 19:17:36 +0900137 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900138 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900139 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
140 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900141 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800144 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900146 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 final SettingsObserver mSettingsObserver;
Joe Onorato089de882010-04-12 08:18:45 -0700148 final StatusBarManagerService mStatusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final IWindowManager mIWindowManager;
150 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900151 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 // All known input methods. mMethodMap also serves as the global
156 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900157 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
158 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900159 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
160 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800161
satok7cfc0ed2011-06-20 21:29:36 +0900162 // Ongoing notification
163 private final NotificationManager mNotificationManager;
satokf90a33e2011-07-19 11:55:52 +0900164 private final KeyguardManager mKeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +0900165 private final Notification mImeSwitcherNotification;
166 private final PendingIntent mImeSwitchPendingIntent;
167 private final boolean mShowOngoingImeSwitcherForPhones;
168 private boolean mNotificationShown;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 class SessionState {
171 final ClientState client;
172 final IInputMethod method;
173 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 @Override
176 public String toString() {
177 return "SessionState{uid " + client.uid + " pid " + client.pid
178 + " method " + Integer.toHexString(
179 System.identityHashCode(method))
180 + " session " + Integer.toHexString(
181 System.identityHashCode(session))
182 + "}";
183 }
184
185 SessionState(ClientState _client, IInputMethod _method,
186 IInputMethodSession _session) {
187 client = _client;
188 method = _method;
189 session = _session;
190 }
191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 class ClientState {
194 final IInputMethodClient client;
195 final IInputContext inputContext;
196 final int uid;
197 final int pid;
198 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 boolean sessionRequested;
201 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 @Override
204 public String toString() {
205 return "ClientState{" + Integer.toHexString(
206 System.identityHashCode(this)) + " uid " + uid
207 + " pid " + pid + "}";
208 }
209
210 ClientState(IInputMethodClient _client, IInputContext _inputContext,
211 int _uid, int _pid) {
212 client = _client;
213 inputContext = _inputContext;
214 uid = _uid;
215 pid = _pid;
216 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
217 }
218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 final HashMap<IBinder, ClientState> mClients
221 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700224 * Set once the system is ready to run third party code.
225 */
226 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * Id of the currently selected input method.
230 */
231 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
234 * The current binding sequence number, incremented every time there is
235 * a new bind performed.
236 */
237 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /**
240 * The client that is currently bound to an input method.
241 */
242 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700245 * The last window token that gained focus.
246 */
247 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 * The input context last provided by the current client.
251 */
252 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
255 * The attributes last provided by the current client.
256 */
257 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * The input method ID of the input method service that we are currently
261 * connected to or in the process of connecting to.
262 */
263 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
satokab751aa2010-09-14 19:17:36 +0900266 * The current subtype of the current input method.
267 */
268 private InputMethodSubtype mCurrentSubtype;
269
satok4e4569d2010-11-19 18:45:53 +0900270 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900271 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
272 mShortcutInputMethodsAndSubtypes =
273 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900274
275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * Set to true if our ServiceConnection is currently actively bound to
277 * a service (whether or not we have gotten its IBinder back yet).
278 */
279 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
282 * Set if the client has asked for the input method to be shown.
283 */
284 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * Set if we were explicitly told to show the input method.
288 */
289 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * Set if we were forced to be shown.
293 */
294 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
297 * Set if we last told the input method to show itself.
298 */
299 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * The Intent used to connect to the current input method.
303 */
304 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 /**
307 * The token we have made for the currently active input method, to
308 * identify it in the future.
309 */
310 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * If non-null, this is the input method service we are currently connected
314 * to.
315 */
316 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * Time that we last initiated a bind to the input method, to determine
320 * if we should try to disconnect and reconnect to it.
321 */
322 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Have we called mCurMethod.bindInput()?
326 */
327 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Currently enabled session. Only touched by service thread, not
331 * protected by a lock.
332 */
333 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 /**
336 * True if the screen is on. The value is true initially.
337 */
338 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
Joe Onorato857fd9b2011-01-27 15:08:35 -0800340 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
341 int mImeWindowVis;
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 AlertDialog.Builder mDialogBuilder;
344 AlertDialog mSwitchingDialog;
345 InputMethodInfo[] mIms;
346 CharSequence[] mItems;
satokab751aa2010-09-14 19:17:36 +0900347 int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 class SettingsObserver extends ContentObserver {
350 SettingsObserver(Handler handler) {
351 super(handler);
352 ContentResolver resolver = mContext.getContentResolver();
353 resolver.registerContentObserver(Settings.Secure.getUriFor(
354 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900355 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900356 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900358 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 @Override public void onChange(boolean selfChange) {
362 synchronized (mMethodMap) {
363 updateFromSettingsLocked();
364 }
365 }
366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
369 @Override
370 public void onReceive(Context context, Intent intent) {
371 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
372 mScreenOn = true;
373 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
374 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700375 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
376 hideInputMethodMenu();
377 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800379 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
381
382 // Inform the current client of the change in active status
383 try {
384 if (mCurClient != null && mCurClient.client != null) {
385 mCurClient.client.setActive(mScreenOn);
386 }
387 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800388 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 + mCurClient.pid + " uid " + mCurClient.uid);
390 }
391 }
392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800394 class MyPackageMonitor extends PackageMonitor {
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800397 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
401 final int N = mMethodList.size();
402 if (curInputMethodId != null) {
403 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800404 InputMethodInfo imi = mMethodList.get(i);
405 if (imi.getId().equals(curInputMethodId)) {
406 for (String pkg : packages) {
407 if (imi.getPackageName().equals(pkg)) {
408 if (!doit) {
409 return true;
410 }
satok723a27e2010-11-11 14:58:11 +0900411 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800412 chooseNewDefaultIMELocked();
413 return true;
414 }
415 }
416 }
417 }
418 }
419 }
420 return false;
421 }
422
423 @Override
424 public void onSomePackagesChanged() {
425 synchronized (mMethodMap) {
426 InputMethodInfo curIm = null;
427 String curInputMethodId = Settings.Secure.getString(mContext
428 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
429 final int N = mMethodList.size();
430 if (curInputMethodId != null) {
431 for (int i=0; i<N; i++) {
432 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900433 final String imiId = imi.getId();
434 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800435 curIm = imi;
436 }
satoke7c6998e2011-06-03 17:57:59 +0900437
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900439 if (isPackageModified(imi.getPackageName())) {
440 mFileManager.deleteAllInputMethodSubtypes(imiId);
441 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 if (change == PACKAGE_TEMPORARY_CHANGE
443 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800444 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800445 + imi.getComponent());
446 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448 }
449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800450
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800451 buildInputMethodListLocked(mMethodList, mMethodMap);
452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800454
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800455 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800456 int change = isPackageDisappearing(curIm.getPackageName());
457 if (change == PACKAGE_TEMPORARY_CHANGE
458 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800459 ServiceInfo si = null;
460 try {
461 si = mContext.getPackageManager().getServiceInfo(
462 curIm.getComponent(), 0);
463 } catch (PackageManager.NameNotFoundException ex) {
464 }
465 if (si == null) {
466 // Uh oh, current input method is no longer around!
467 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800468 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800469 mImeWindowVis = 0;
470 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
471 mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800473 changed = true;
474 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800475 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900476 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800477 }
478 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800479 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480 }
satokab751aa2010-09-14 19:17:36 +0900481
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800482 if (curIm == null) {
483 // We currently don't have a default input method... is
484 // one now available?
485 changed = chooseNewDefaultIMELocked();
486 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800487
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 if (changed) {
489 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491 }
492 }
493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 class MethodCallback extends IInputMethodCallback.Stub {
496 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 MethodCallback(IInputMethod method) {
499 mMethod = method;
500 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800501
satoke7c6998e2011-06-03 17:57:59 +0900502 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 public void finishedEvent(int seq, boolean handled) throws RemoteException {
504 }
505
satoke7c6998e2011-06-03 17:57:59 +0900506 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 public void sessionCreated(IInputMethodSession session) throws RemoteException {
508 onSessionCreated(mMethod, session);
509 }
510 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800511
Joe Onorato089de882010-04-12 08:18:45 -0700512 public InputMethodManagerService(Context context, StatusBarManagerService statusBar) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800514 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 mHandler = new Handler(this);
516 mIWindowManager = IWindowManager.Stub.asInterface(
517 ServiceManager.getService(Context.WINDOW_SERVICE));
518 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900519 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public void executeMessage(Message msg) {
521 handleMessage(msg);
522 }
523 });
satok7cfc0ed2011-06-20 21:29:36 +0900524
satokf90a33e2011-07-19 11:55:52 +0900525 mKeyguardManager = (KeyguardManager)
526 mContext.getSystemService(Context.KEYGUARD_SERVICE);
satok7cfc0ed2011-06-20 21:29:36 +0900527 mNotificationManager = (NotificationManager)
528 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
529 mImeSwitcherNotification = new Notification();
530 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
531 mImeSwitcherNotification.when = 0;
532 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
533 mImeSwitcherNotification.tickerText = null;
534 mImeSwitcherNotification.defaults = 0; // please be quiet
535 mImeSwitcherNotification.sound = null;
536 mImeSwitcherNotification.vibrate = null;
537 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900538 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satok7cfc0ed2011-06-20 21:29:36 +0900539 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
540 com.android.internal.R.bool.show_ongoing_ime_switcher);
541
satoke7c6998e2011-06-03 17:57:59 +0900542 synchronized (mMethodMap) {
543 mFileManager = new InputMethodFileManager(mMethodMap);
544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800546 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 IntentFilter screenOnOffFilt = new IntentFilter();
549 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
550 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700551 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800553
satok913a8922010-08-26 21:53:41 +0900554 mStatusBar = statusBar;
555 statusBar.setIconVisibility("ime", false);
satok7cfc0ed2011-06-20 21:29:36 +0900556 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900557
satokd87c2592010-09-29 11:52:06 +0900558 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900559 mSettings = new InputMethodSettings(
560 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900562 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
satokd87c2592010-09-29 11:52:06 +0900564 if (TextUtils.isEmpty(Settings.Secure.getString(
565 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900567 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
569 try {
satokd87c2592010-09-29 11:52:06 +0900570 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 imi.getPackageName(), 0).getResources();
572 if (res.getBoolean(imi.getIsDefaultResourceId())) {
573 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800574 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576 } catch (PackageManager.NameNotFoundException ex) {
577 } catch (Resources.NotFoundException ex) {
578 }
579 }
580 }
satokd87c2592010-09-29 11:52:06 +0900581 if (defIm == null && mMethodList.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 defIm = mMethodList.get(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800583 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900586 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 mSettingsObserver = new SettingsObserver(mHandler);
591 updateFromSettingsLocked();
592 }
593
594 @Override
595 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
596 throws RemoteException {
597 try {
598 return super.onTransact(code, data, reply, flags);
599 } catch (RuntimeException e) {
600 // The input method manager only throws security exceptions, so let's
601 // log all others.
602 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800603 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605 throw e;
606 }
607 }
608
609 public void systemReady() {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700610 synchronized (mMethodMap) {
611 if (!mSystemReady) {
612 mSystemReady = true;
Dianne Hackborncc278702009-09-02 23:07:23 -0700613 try {
614 startInputInnerLocked();
615 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800616 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700617 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700618 }
619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800621
satoke7c6998e2011-06-03 17:57:59 +0900622 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 public List<InputMethodInfo> getInputMethodList() {
624 synchronized (mMethodMap) {
625 return new ArrayList<InputMethodInfo>(mMethodList);
626 }
627 }
628
satoke7c6998e2011-06-03 17:57:59 +0900629 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public List<InputMethodInfo> getEnabledInputMethodList() {
631 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900632 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634 }
635
satokbb4aa062011-01-19 21:40:27 +0900636 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
637 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
638 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
639 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
640 for (InputMethodInfo imi: getEnabledInputMethodList()) {
641 enabledInputMethodAndSubtypes.put(
642 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
643 }
644 return enabledInputMethodAndSubtypes;
645 }
646
647 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
648 boolean allowsImplicitlySelectedSubtypes) {
649 if (imi == null && mCurMethodId != null) {
650 imi = mMethodMap.get(mCurMethodId);
651 }
satok7265d9b2011-02-14 15:47:30 +0900652 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900653 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900654 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
655 enabledSubtypes = getApplicableSubtypesLocked(mRes, getSubtypes(imi));
satokbb4aa062011-01-19 21:40:27 +0900656 }
satok7265d9b2011-02-14 15:47:30 +0900657 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900658 }
659
satoke7c6998e2011-06-03 17:57:59 +0900660 @Override
satok16331c82010-12-20 23:48:46 +0900661 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
662 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900663 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900664 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900665 }
666 }
667
satoke7c6998e2011-06-03 17:57:59 +0900668 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 public void addClient(IInputMethodClient client,
670 IInputContext inputContext, int uid, int pid) {
671 synchronized (mMethodMap) {
672 mClients.put(client.asBinder(), new ClientState(client,
673 inputContext, uid, pid));
674 }
675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800676
satoke7c6998e2011-06-03 17:57:59 +0900677 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 public void removeClient(IInputMethodClient client) {
679 synchronized (mMethodMap) {
680 mClients.remove(client.asBinder());
681 }
682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 void executeOrSendMessage(IInterface target, Message msg) {
685 if (target.asBinder() instanceof Binder) {
686 mCaller.sendMessage(msg);
687 } else {
688 handleMessage(msg);
689 msg.recycle();
690 }
691 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700693 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800695 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 + mCurClient.client.asBinder());
697 if (mBoundToMethod) {
698 mBoundToMethod = false;
699 if (mCurMethod != null) {
700 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
701 MSG_UNBIND_INPUT, mCurMethod));
702 }
703 }
704 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
705 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
706 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 // Call setActive(false) on the old client
709 try {
710 mCurClient.client.setActive(false);
711 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800712 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 + mCurClient.pid + " uid " + mCurClient.uid);
714 }
715 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800716
The Android Open Source Project10592532009-03-18 17:39:46 -0700717 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 private int getImeShowFlags() {
722 int flags = 0;
723 if (mShowForced) {
724 flags |= InputMethod.SHOW_FORCED
725 | InputMethod.SHOW_EXPLICIT;
726 } else if (mShowExplicitlyRequested) {
727 flags |= InputMethod.SHOW_EXPLICIT;
728 }
729 return flags;
730 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 private int getAppShowFlags() {
733 int flags = 0;
734 if (mShowForced) {
735 flags |= InputMethodManager.SHOW_FORCED;
736 } else if (!mShowExplicitlyRequested) {
737 flags |= InputMethodManager.SHOW_IMPLICIT;
738 }
739 return flags;
740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
743 if (!mBoundToMethod) {
744 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
745 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
746 mBoundToMethod = true;
747 }
748 final SessionState session = mCurClient.curSession;
749 if (initial) {
750 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
751 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
752 } else {
753 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
754 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
755 }
756 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800757 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800758 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760 return needResult
761 ? new InputBindResult(session.session, mCurId, mCurSeq)
762 : null;
763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 InputBindResult startInputLocked(IInputMethodClient client,
766 IInputContext inputContext, EditorInfo attribute,
767 boolean initial, boolean needResult) {
768 // If no method is currently selected, do nothing.
769 if (mCurMethodId == null) {
770 return mNoBinding;
771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 ClientState cs = mClients.get(client.asBinder());
774 if (cs == null) {
775 throw new IllegalArgumentException("unknown client "
776 + client.asBinder());
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 try {
780 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
781 // Check with the window manager to make sure this client actually
782 // has a window with focus. If not, reject. This is thread safe
783 // because if the focus changes some time before or after, the
784 // next client receiving focus that has any interest in input will
785 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800786 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
788 return null;
789 }
790 } catch (RemoteException e) {
791 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 if (mCurClient != cs) {
794 // If the client is changing, we need to switch over to the new
795 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700796 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800797 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 + cs.client.asBinder());
799
800 // If the screen is on, inform the new client it is active
801 if (mScreenOn) {
802 try {
803 cs.client.setActive(mScreenOn);
804 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800805 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 + cs.pid + " uid " + cs.uid);
807 }
808 }
809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 // Bump up the sequence for this client and attach it.
812 mCurSeq++;
813 if (mCurSeq <= 0) mCurSeq = 1;
814 mCurClient = cs;
815 mCurInputContext = inputContext;
816 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 // Check if the input method is changing.
819 if (mCurId != null && mCurId.equals(mCurMethodId)) {
820 if (cs.curSession != null) {
821 // Fast case: if we are already connected to the input method,
822 // then just return it.
823 return attachNewInputLocked(initial, needResult);
824 }
825 if (mHaveConnection) {
826 if (mCurMethod != null) {
827 if (!cs.sessionRequested) {
828 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800829 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
831 MSG_CREATE_SESSION, mCurMethod,
832 new MethodCallback(mCurMethod)));
833 }
834 // Return to client, and we will get back with it when
835 // we have had a session made for it.
836 return new InputBindResult(null, mCurId, mCurSeq);
837 } else if (SystemClock.uptimeMillis()
838 < (mLastBindTime+TIME_TO_RECONNECT)) {
839 // In this case we have connected to the service, but
840 // don't yet have its interface. If it hasn't been too
841 // long since we did the connection, we'll return to
842 // the client and wait to get the service interface so
843 // we can report back. If it has been too long, we want
844 // to fall through so we can try a disconnect/reconnect
845 // to see if we can get back in touch with the service.
846 return new InputBindResult(null, mCurId, mCurSeq);
847 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800848 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
849 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851 }
852 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800853
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700854 return startInputInnerLocked();
855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700857 InputBindResult startInputInnerLocked() {
858 if (mCurMethodId == null) {
859 return mNoBinding;
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700862 if (!mSystemReady) {
863 // If the system is not yet ready, we shouldn't be running third
864 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700865 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 InputMethodInfo info = mMethodMap.get(mCurMethodId);
869 if (info == null) {
870 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700873 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
876 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700877 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
878 com.android.internal.R.string.input_method_binding_label);
879 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
880 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
882 mLastBindTime = SystemClock.uptimeMillis();
883 mHaveConnection = true;
884 mCurId = info.getId();
885 mCurToken = new Binder();
886 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800887 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 mIWindowManager.addWindowToken(mCurToken,
889 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
890 } catch (RemoteException e) {
891 }
892 return new InputBindResult(null, mCurId, mCurSeq);
893 } else {
894 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800895 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 + mCurIntent);
897 }
898 return null;
899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
satoke7c6998e2011-06-03 17:57:59 +0900901 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 public InputBindResult startInput(IInputMethodClient client,
903 IInputContext inputContext, EditorInfo attribute,
904 boolean initial, boolean needResult) {
905 synchronized (mMethodMap) {
906 final long ident = Binder.clearCallingIdentity();
907 try {
908 return startInputLocked(client, inputContext, attribute,
909 initial, needResult);
910 } finally {
911 Binder.restoreCallingIdentity(ident);
912 }
913 }
914 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800915
satoke7c6998e2011-06-03 17:57:59 +0900916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void finishInput(IInputMethodClient client) {
918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
satoke7c6998e2011-06-03 17:57:59 +0900920 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 public void onServiceConnected(ComponentName name, IBinder service) {
922 synchronized (mMethodMap) {
923 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
924 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700925 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800926 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700927 unbindCurrentMethodLocked(false);
928 return;
929 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800930 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700931 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
932 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700937 MSG_CREATE_SESSION, mCurMethod,
938 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 }
941 }
942 }
943
944 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
945 synchronized (mMethodMap) {
946 if (mCurMethod != null && method != null
947 && mCurMethod.asBinder() == method.asBinder()) {
948 if (mCurClient != null) {
949 mCurClient.curSession = new SessionState(mCurClient,
950 method, session);
951 mCurClient.sessionRequested = false;
952 InputBindResult res = attachNewInputLocked(true, true);
953 if (res.method != null) {
954 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
955 MSG_BIND_METHOD, mCurClient.client, res));
956 }
957 }
958 }
959 }
960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700962 void unbindCurrentMethodLocked(boolean reportToClient) {
963 if (mHaveConnection) {
964 mContext.unbindService(this);
965 mHaveConnection = false;
966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800967
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700968 if (mCurToken != null) {
969 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800970 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700971 mIWindowManager.removeWindowToken(mCurToken);
972 } catch (RemoteException e) {
973 }
974 mCurToken = null;
975 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800976
The Android Open Source Project10592532009-03-18 17:39:46 -0700977 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700978 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 if (reportToClient && mCurClient != null) {
981 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
982 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
983 }
984 }
985
Devin Taylor0c33ed22010-02-23 13:26:46 -0600986 private void finishSession(SessionState sessionState) {
987 if (sessionState != null && sessionState.session != null) {
988 try {
989 sessionState.session.finishSession();
990 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700991 Slog.w(TAG, "Session failed to close due to remote exception", e);
Devin Taylor0c33ed22010-02-23 13:26:46 -0600992 }
993 }
994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800995
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700996 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 if (mCurMethod != null) {
998 for (ClientState cs : mClients.values()) {
999 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001000 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 cs.curSession = null;
1002 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001003
1004 finishSession(mEnabledSession);
1005 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 mCurMethod = null;
1007 }
Joe Onorato0cbda992010-05-02 16:28:15 -07001008 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
satoke7c6998e2011-06-03 17:57:59 +09001011 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 public void onServiceDisconnected(ComponentName name) {
1013 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001014 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 + " mCurIntent=" + mCurIntent);
1016 if (mCurMethod != null && mCurIntent != null
1017 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001018 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 // We consider this to be a new bind attempt, since the system
1020 // should now try to restart the service for us.
1021 mLastBindTime = SystemClock.uptimeMillis();
1022 mShowRequested = mInputShown;
1023 mInputShown = false;
1024 if (mCurClient != null) {
1025 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1026 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1027 }
1028 }
1029 }
1030 }
1031
satokf9f01002011-05-19 21:31:50 +09001032 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001034 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 long ident = Binder.clearCallingIdentity();
1036 try {
1037 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001038 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 return;
1040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 synchronized (mMethodMap) {
1043 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001044 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001045 mStatusBar.setIconVisibility("ime", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001047 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Joe Onorato0cbda992010-05-02 16:28:15 -07001048 mStatusBar.setIcon("ime", packageName, iconId, 0);
1049 mStatusBar.setIconVisibility("ime", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
1051 }
1052 } finally {
1053 Binder.restoreCallingIdentity(ident);
1054 }
1055 }
1056
satok7cfc0ed2011-06-20 21:29:36 +09001057 private boolean needsToShowImeSwitchOngoingNotification() {
1058 if (!mShowOngoingImeSwitcherForPhones) return false;
1059 synchronized (mMethodMap) {
1060 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1061 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001062 if (N > 2) return true;
1063 if (N < 1) return false;
1064 int nonAuxCount = 0;
1065 int auxCount = 0;
1066 InputMethodSubtype nonAuxSubtype = null;
1067 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001068 for(int i = 0; i < N; ++i) {
1069 final InputMethodInfo imi = imis.get(i);
1070 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1071 imi, true);
1072 final int subtypeCount = subtypes.size();
1073 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001074 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001075 } else {
1076 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001077 final InputMethodSubtype subtype = subtypes.get(j);
1078 if (!subtype.isAuxiliary()) {
1079 ++nonAuxCount;
1080 nonAuxSubtype = subtype;
1081 } else {
1082 ++auxCount;
1083 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001084 }
1085 }
1086 }
satok7cfc0ed2011-06-20 21:29:36 +09001087 }
satokb6359412011-06-28 17:47:41 +09001088 if (nonAuxCount > 1 || auxCount > 1) {
1089 return true;
1090 } else if (nonAuxCount == 1 && auxCount == 1) {
1091 if (nonAuxSubtype != null && auxSubtype != null
1092 && nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1093 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1094 return false;
1095 }
1096 return true;
1097 }
1098 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001099 }
satok7cfc0ed2011-06-20 21:29:36 +09001100 }
1101
satokf9f01002011-05-19 21:31:50 +09001102 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001103 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001104 int uid = Binder.getCallingUid();
1105 long ident = Binder.clearCallingIdentity();
1106 try {
1107 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001108 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001109 return;
1110 }
1111
1112 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001113 mImeWindowVis = vis;
1114 mBackDisposition = backDisposition;
1115 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
satok7cfc0ed2011-06-20 21:29:36 +09001116 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
1117 if (iconVisibility && needsToShowImeSwitchOngoingNotification()) {
1118 final PackageManager pm = mContext.getPackageManager();
1119 final CharSequence label = mMethodMap.get(mCurMethodId).loadLabel(pm);
1120 final CharSequence title = mRes.getText(
1121 com.android.internal.R.string.select_input_method);
1122 mImeSwitcherNotification.setLatestEventInfo(
1123 mContext, title, label, mImeSwitchPendingIntent);
1124 mNotificationManager.notify(
1125 com.android.internal.R.string.select_input_method,
1126 mImeSwitcherNotification);
1127 mNotificationShown = true;
1128 } else {
1129 if (mNotificationShown) {
1130 mNotificationManager.cancel(
1131 com.android.internal.R.string.select_input_method);
1132 mNotificationShown = false;
1133 }
1134 }
satok06487a52010-10-29 11:37:18 +09001135 }
1136 } finally {
1137 Binder.restoreCallingIdentity(ident);
1138 }
1139 }
1140
satoke7c6998e2011-06-03 17:57:59 +09001141 @Override
satokf9f01002011-05-19 21:31:50 +09001142 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1143 synchronized (mMethodMap) {
1144 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1145 for (int i = 0; i < spans.length; ++i) {
1146 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001147 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001148 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001149 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001150 }
1151 }
1152 }
1153 }
1154
satoke7c6998e2011-06-03 17:57:59 +09001155 @Override
satokf9f01002011-05-19 21:31:50 +09001156 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1157 synchronized (mMethodMap) {
1158 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1159 // TODO: Do not send the intent if the process of the targetImi is already dead.
1160 if (targetImi != null) {
1161 final String[] suggestions = span.getSuggestions();
1162 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001163 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001164 final Intent intent = new Intent();
1165 // Ensures that only a class in the original IME package will receive the
1166 // notification.
satok42c5a162011-05-26 16:46:14 +09001167 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001168 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1169 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1170 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1171 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1172 mContext.sendBroadcast(intent);
1173 return true;
1174 }
1175 }
1176 return false;
1177 }
1178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001180 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1181 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1182 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1183 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001185 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001186 // There is no input method selected, try to choose new applicable input method.
1187 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1188 id = Settings.Secure.getString(mContext.getContentResolver(),
1189 Settings.Secure.DEFAULT_INPUT_METHOD);
1190 }
1191 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 try {
satokab751aa2010-09-14 19:17:36 +09001193 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001195 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001196 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001197 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
satokf3db1af2010-11-23 13:34:33 +09001199 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001200 } else {
1201 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001202 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001203 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 }
1205 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
satokab751aa2010-09-14 19:17:36 +09001207 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 InputMethodInfo info = mMethodMap.get(id);
1209 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001210 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001214 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001215 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1216 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001217 }
1218 if (subtype != mCurrentSubtype) {
1219 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001220 if (subtype != null) {
1221 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1222 }
satokcd7cd292010-11-20 15:46:23 +09001223 if (mCurMethod != null) {
1224 try {
satoke40dea02011-01-30 01:14:02 +09001225 final Configuration conf = mRes.getConfiguration();
1226 final boolean haveHardKeyboard = conf.keyboard
1227 != Configuration.KEYBOARD_NOKEYS;
1228 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001229 && conf.hardKeyboardHidden
1230 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001231 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1232 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1233 : 0;
Joe Onorato857fd9b2011-01-27 15:08:35 -08001234 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1235 mBackDisposition);
satokcd7cd292010-11-20 15:46:23 +09001236 // If subtype is null, try to find the most applicable one from
1237 // getCurrentInputMethodSubtype.
1238 if (subtype == null) {
1239 subtype = getCurrentInputMethodSubtype();
1240 }
1241 mCurMethod.changeInputMethodSubtype(subtype);
1242 } catch (RemoteException e) {
1243 return;
satokab751aa2010-09-14 19:17:36 +09001244 }
1245 }
1246 }
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 return;
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 final long ident = Binder.clearCallingIdentity();
1252 try {
satokab751aa2010-09-14 19:17:36 +09001253 // Set a subtype to this input method.
1254 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001255 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1256 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1257 // because mCurMethodId is stored as a history in
1258 // setSelectedInputMethodAndSubtypeLocked().
1259 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260
1261 if (ActivityManagerNative.isSystemReady()) {
1262 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001263 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 intent.putExtra("input_method_id", id);
1265 mContext.sendBroadcast(intent);
1266 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001267 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 } finally {
1269 Binder.restoreCallingIdentity(ident);
1270 }
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
satok42c5a162011-05-26 16:46:14 +09001273 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001274 public boolean showSoftInput(IInputMethodClient client, int flags,
1275 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001276 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 long ident = Binder.clearCallingIdentity();
1278 try {
1279 synchronized (mMethodMap) {
1280 if (mCurClient == null || client == null
1281 || mCurClient.client.asBinder() != client.asBinder()) {
1282 try {
1283 // We need to check if this is the current client with
1284 // focus in the window manager, to allow this call to
1285 // be made before input is started in it.
1286 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001287 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001288 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001291 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
Joe Onorato8a9b2202010-02-26 18:56:32 -08001295 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001296 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
1298 } finally {
1299 Binder.restoreCallingIdentity(ident);
1300 }
1301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001302
The Android Open Source Project4df24232009-03-05 14:34:35 -08001303 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 mShowRequested = true;
1305 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1306 mShowExplicitlyRequested = true;
1307 }
1308 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1309 mShowExplicitlyRequested = true;
1310 mShowForced = true;
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
Dianne Hackborncc278702009-09-02 23:07:23 -07001313 if (!mSystemReady) {
1314 return false;
1315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Project4df24232009-03-05 14:34:35 -08001317 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001319 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1320 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1321 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001323 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 } else if (mHaveConnection && SystemClock.uptimeMillis()
1325 < (mLastBindTime+TIME_TO_RECONNECT)) {
1326 // The client has asked to have the input method shown, but
1327 // we have been sitting here too long with a connection to the
1328 // service and no interface received, so let's disconnect/connect
1329 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 SystemClock.uptimeMillis()-mLastBindTime,1);
1332 mContext.unbindService(this);
1333 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1334 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001335
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
satok42c5a162011-05-26 16:46:14 +09001339 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001340 public boolean hideSoftInput(IInputMethodClient client, int flags,
1341 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001342 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 long ident = Binder.clearCallingIdentity();
1344 try {
1345 synchronized (mMethodMap) {
1346 if (mCurClient == null || client == null
1347 || mCurClient.client.asBinder() != client.asBinder()) {
1348 try {
1349 // We need to check if this is the current client with
1350 // focus in the window manager, to allow this call to
1351 // be made before input is started in it.
1352 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001353 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1354 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001355 mImeWindowVis = 0;
1356 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis,
1357 mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001358 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 }
1360 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001361 mImeWindowVis = 0;
1362 mStatusBar.setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
Joe Onorato8a9b2202010-02-26 18:56:32 -08001367 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001368 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370 } finally {
1371 Binder.restoreCallingIdentity(ident);
1372 }
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Project4df24232009-03-05 14:34:35 -08001375 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1377 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001380 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001385 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001387 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001389 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1390 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1391 res = true;
1392 } else {
1393 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 }
1395 mInputShown = false;
1396 mShowRequested = false;
1397 mShowExplicitlyRequested = false;
1398 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001399 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
satok42c5a162011-05-26 16:46:14 +09001402 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001403 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1405 boolean first, int windowFlags) {
1406 long ident = Binder.clearCallingIdentity();
1407 try {
1408 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001409 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 + " viewHasFocus=" + viewHasFocus
1411 + " isTextEditor=" + isTextEditor
1412 + " softInputMode=#" + Integer.toHexString(softInputMode)
1413 + " first=" + first + " flags=#"
1414 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if (mCurClient == null || client == null
1417 || mCurClient.client.asBinder() != client.asBinder()) {
1418 try {
1419 // We need to check if this is the current client with
1420 // focus in the window manager, to allow this call to
1421 // be made before input is started in it.
1422 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 return;
1425 }
1426 } catch (RemoteException e) {
1427 }
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001430 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001431 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001432 return;
1433 }
1434 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001435
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001436 // Should we auto-show the IME even if the caller has not
1437 // specified what should be done with it?
1438 // We only do this automatically if the window can resize
1439 // to accommodate the IME (so what the user sees will give
1440 // them good context without input information being obscured
1441 // by the IME) or if running on a large screen where there
1442 // is more room for the target window + IME.
1443 final boolean doAutoShow =
1444 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1445 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1446 || mRes.getConfiguration().isLayoutSizeAtLeast(
1447 Configuration.SCREENLAYOUT_SIZE_LARGE);
1448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1450 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001451 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1453 // There is no focus view, and this window will
1454 // be behind any soft input window, so hide the
1455 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001456 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001457 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001459 } else if (isTextEditor && doAutoShow && (softInputMode &
1460 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 // There is a focus view, and we are navigating forward
1462 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001463 // We only do this automatically if the window an resize
1464 // to accomodate the IME (so what the user sees will give
1465 // them good context without input information being obscured
1466 // by the IME) or if running on a large screen where there
1467 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001468 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001469 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471 break;
1472 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1473 // Do nothing.
1474 break;
1475 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1476 if ((softInputMode &
1477 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001478 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001479 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 break;
1482 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001483 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001484 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 break;
1486 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1487 if ((softInputMode &
1488 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001489 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001490 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
1492 break;
1493 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001494 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001495 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 break;
1497 }
1498 }
1499 } finally {
1500 Binder.restoreCallingIdentity(ident);
1501 }
1502 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001503
satok42c5a162011-05-26 16:46:14 +09001504 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1506 synchronized (mMethodMap) {
1507 if (mCurClient == null || client == null
1508 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001509 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001510 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512
satok440aab52010-11-25 09:43:11 +09001513 // Always call subtype picker, because subtype picker is a superset of input method
1514 // picker.
satokab751aa2010-09-14 19:17:36 +09001515 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1516 }
1517 }
1518
satok42c5a162011-05-26 16:46:14 +09001519 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001521 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1522 }
1523
satok42c5a162011-05-26 16:46:14 +09001524 @Override
satok28203512010-11-24 11:06:49 +09001525 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1526 synchronized (mMethodMap) {
1527 if (subtype != null) {
1528 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1529 mMethodMap.get(id), subtype.hashCode()));
1530 } else {
1531 setInputMethod(token, id);
1532 }
1533 }
satokab751aa2010-09-14 19:17:36 +09001534 }
1535
satok42c5a162011-05-26 16:46:14 +09001536 @Override
satokb416a712010-11-25 20:42:14 +09001537 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001538 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001539 synchronized (mMethodMap) {
1540 if (mCurClient == null || client == null
1541 || mCurClient.client.asBinder() != client.asBinder()) {
1542 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1543 }
satok7fee71f2010-12-17 18:54:26 +09001544 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1545 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001546 }
1547 }
1548
satok4fc87d62011-05-20 16:13:43 +09001549 @Override
satok735cf382010-11-11 20:40:09 +09001550 public boolean switchToLastInputMethod(IBinder token) {
1551 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001552 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001553 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001554 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001555 lastImi = mMethodMap.get(lastIme.first);
1556 } else {
1557 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001558 }
satok4fc87d62011-05-20 16:13:43 +09001559 String targetLastImiId = null;
1560 int subtypeId = NOT_A_SUBTYPE_ID;
1561 if (lastIme != null && lastImi != null) {
1562 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1563 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1564 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1565 : mCurrentSubtype.hashCode();
1566 // If the last IME is the same as the current IME and the last subtype is not
1567 // defined, there is no need to switch to the last IME.
1568 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1569 targetLastImiId = lastIme.first;
1570 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1571 }
1572 }
1573
1574 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1575 // This is a safety net. If the currentSubtype can't be added to the history
1576 // and the framework couldn't find the last ime, we will make the last ime be
1577 // the most applicable enabled keyboard subtype of the system imes.
1578 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1579 if (enabled != null) {
1580 final int N = enabled.size();
1581 final String locale = mCurrentSubtype == null
1582 ? mRes.getConfiguration().locale.toString()
1583 : mCurrentSubtype.getLocale();
1584 for (int i = 0; i < N; ++i) {
1585 final InputMethodInfo imi = enabled.get(i);
1586 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1587 InputMethodSubtype keyboardSubtype =
1588 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1589 SUBTYPE_MODE_KEYBOARD, locale, true);
1590 if (keyboardSubtype != null) {
1591 targetLastImiId = imi.getId();
1592 subtypeId = getSubtypeIdFromHashCode(
1593 imi, keyboardSubtype.hashCode());
1594 if(keyboardSubtype.getLocale().equals(locale)) {
1595 break;
1596 }
1597 }
1598 }
1599 }
1600 }
1601 }
1602
1603 if (!TextUtils.isEmpty(targetLastImiId)) {
1604 if (DEBUG) {
1605 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1606 + ", from: " + mCurMethodId + ", " + subtypeId);
1607 }
1608 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1609 return true;
1610 } else {
1611 return false;
1612 }
satok735cf382010-11-11 20:40:09 +09001613 }
1614 }
1615
satoke7c6998e2011-06-03 17:57:59 +09001616 @Override
satok68f1b782011-04-11 14:26:04 +09001617 public InputMethodSubtype getLastInputMethodSubtype() {
1618 synchronized (mMethodMap) {
1619 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1620 // TODO: Handle the case of the last IME with no subtypes
1621 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1622 || TextUtils.isEmpty(lastIme.second)) return null;
1623 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1624 if (lastImi == null) return null;
1625 try {
1626 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001627 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1628 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1629 return null;
1630 }
1631 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001632 } catch (NumberFormatException e) {
1633 return null;
1634 }
1635 }
1636 }
1637
satoke7c6998e2011-06-03 17:57:59 +09001638 @Override
1639 public boolean setAdditionalInputMethodSubtypes(IBinder token, InputMethodSubtype[] subtypes) {
1640 if (token == null || mCurToken != token) {
1641 return false;
1642 }
1643 if (subtypes == null || subtypes.length == 0) return false;
1644 synchronized (mMethodMap) {
1645 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1646 if (imi == null) return false;
1647 final int N = subtypes.length;
satok4a28bde2011-06-29 21:03:40 +09001648 mFileManager.addInputMethodSubtypes(imi, subtypes);
satoke7c6998e2011-06-03 17:57:59 +09001649 buildInputMethodListLocked(mMethodList, mMethodMap);
1650 return true;
1651 }
1652 }
1653
satok28203512010-11-24 11:06:49 +09001654 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 synchronized (mMethodMap) {
1656 if (token == null) {
1657 if (mContext.checkCallingOrSelfPermission(
1658 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1659 != PackageManager.PERMISSION_GRANTED) {
1660 throw new SecurityException(
1661 "Using null token requires permission "
1662 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1663 }
1664 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001665 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1666 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 return;
1668 }
1669
1670 long ident = Binder.clearCallingIdentity();
1671 try {
satokab751aa2010-09-14 19:17:36 +09001672 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 } finally {
1674 Binder.restoreCallingIdentity(ident);
1675 }
1676 }
1677 }
1678
satok42c5a162011-05-26 16:46:14 +09001679 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 public void hideMySoftInput(IBinder token, int flags) {
1681 synchronized (mMethodMap) {
1682 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001683 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1684 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 return;
1686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 long ident = Binder.clearCallingIdentity();
1688 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001689 hideCurrentInputLocked(flags, null);
1690 } finally {
1691 Binder.restoreCallingIdentity(ident);
1692 }
1693 }
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
satok42c5a162011-05-26 16:46:14 +09001696 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001697 public void showMySoftInput(IBinder token, int flags) {
1698 synchronized (mMethodMap) {
1699 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001700 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1701 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001702 return;
1703 }
1704 long ident = Binder.clearCallingIdentity();
1705 try {
1706 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 } finally {
1708 Binder.restoreCallingIdentity(ident);
1709 }
1710 }
1711 }
1712
1713 void setEnabledSessionInMainThread(SessionState session) {
1714 if (mEnabledSession != session) {
1715 if (mEnabledSession != null) {
1716 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001717 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 mEnabledSession.method.setSessionEnabled(
1719 mEnabledSession.session, false);
1720 } catch (RemoteException e) {
1721 }
1722 }
1723 mEnabledSession = session;
1724 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001725 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 session.method.setSessionEnabled(
1727 session.session, true);
1728 } catch (RemoteException e) {
1729 }
1730 }
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
satok42c5a162011-05-26 16:46:14 +09001733 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 public boolean handleMessage(Message msg) {
1735 HandlerCaller.SomeArgs args;
1736 switch (msg.what) {
1737 case MSG_SHOW_IM_PICKER:
1738 showInputMethodMenu();
1739 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740
satokab751aa2010-09-14 19:17:36 +09001741 case MSG_SHOW_IM_SUBTYPE_PICKER:
1742 showInputMethodSubtypeMenu();
1743 return true;
1744
satok47a44912010-10-06 16:03:58 +09001745 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001746 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001747 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001748 return true;
1749
1750 case MSG_SHOW_IM_CONFIG:
1751 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001752 return true;
1753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 case MSG_UNBIND_INPUT:
1757 try {
1758 ((IInputMethod)msg.obj).unbindInput();
1759 } catch (RemoteException e) {
1760 // There is nothing interesting about the method dying.
1761 }
1762 return true;
1763 case MSG_BIND_INPUT:
1764 args = (HandlerCaller.SomeArgs)msg.obj;
1765 try {
1766 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1767 } catch (RemoteException e) {
1768 }
1769 return true;
1770 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001771 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001773 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1774 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 } catch (RemoteException e) {
1776 }
1777 return true;
1778 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001779 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001781 ((IInputMethod)args.arg1).hideSoftInput(0,
1782 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 } catch (RemoteException e) {
1784 }
1785 return true;
1786 case MSG_ATTACH_TOKEN:
1787 args = (HandlerCaller.SomeArgs)msg.obj;
1788 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001789 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1791 } catch (RemoteException e) {
1792 }
1793 return true;
1794 case MSG_CREATE_SESSION:
1795 args = (HandlerCaller.SomeArgs)msg.obj;
1796 try {
1797 ((IInputMethod)args.arg1).createSession(
1798 (IInputMethodCallback)args.arg2);
1799 } catch (RemoteException e) {
1800 }
1801 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 case MSG_START_INPUT:
1805 args = (HandlerCaller.SomeArgs)msg.obj;
1806 try {
1807 SessionState session = (SessionState)args.arg1;
1808 setEnabledSessionInMainThread(session);
1809 session.method.startInput((IInputContext)args.arg2,
1810 (EditorInfo)args.arg3);
1811 } catch (RemoteException e) {
1812 }
1813 return true;
1814 case MSG_RESTART_INPUT:
1815 args = (HandlerCaller.SomeArgs)msg.obj;
1816 try {
1817 SessionState session = (SessionState)args.arg1;
1818 setEnabledSessionInMainThread(session);
1819 session.method.restartInput((IInputContext)args.arg2,
1820 (EditorInfo)args.arg3);
1821 } catch (RemoteException e) {
1822 }
1823 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 case MSG_UNBIND_METHOD:
1828 try {
1829 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1830 } catch (RemoteException e) {
1831 // There is nothing interesting about the last client dying.
1832 }
1833 return true;
1834 case MSG_BIND_METHOD:
1835 args = (HandlerCaller.SomeArgs)msg.obj;
1836 try {
1837 ((IInputMethodClient)args.arg1).onBindMethod(
1838 (InputBindResult)args.arg2);
1839 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001840 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842 return true;
1843 }
1844 return false;
1845 }
1846
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001847 private boolean isSystemIme(InputMethodInfo inputMethod) {
1848 return (inputMethod.getServiceInfo().applicationInfo.flags
1849 & ApplicationInfo.FLAG_SYSTEM) != 0;
1850 }
1851
Ken Wakasa586f0512011-01-20 22:31:01 +09001852 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1853 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1854 final int subtypeCount = imi.getSubtypeCount();
1855 for (int i = 0; i < subtypeCount; ++i) {
1856 subtypes.add(imi.getSubtypeAt(i));
1857 }
1858 return subtypes;
1859 }
1860
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001861 private boolean chooseNewDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001862 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001863 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001864 // We'd prefer to fall back on a system IME, since that is safer.
1865 int i=enabled.size();
1866 while (i > 0) {
1867 i--;
1868 if ((enabled.get(i).getServiceInfo().applicationInfo.flags
1869 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1870 break;
1871 }
1872 }
satokab751aa2010-09-14 19:17:36 +09001873 InputMethodInfo imi = enabled.get(i);
satok03eb319a2010-11-11 18:17:42 +09001874 if (DEBUG) {
1875 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1876 }
satok723a27e2010-11-11 14:58:11 +09001877 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001878 return true;
1879 }
1880
1881 return false;
1882 }
1883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1885 HashMap<String, InputMethodInfo> map) {
1886 list.clear();
1887 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001890 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001891 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1892 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1893 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1894 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895
1896 List<ResolveInfo> services = pm.queryIntentServices(
1897 new Intent(InputMethod.SERVICE_INTERFACE),
1898 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001899
satoke7c6998e2011-06-03 17:57:59 +09001900 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1901 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 for (int i = 0; i < services.size(); ++i) {
1903 ResolveInfo ri = services.get(i);
1904 ServiceInfo si = ri.serviceInfo;
1905 ComponentName compName = new ComponentName(si.packageName, si.name);
1906 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1907 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001908 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 + ": it does not require the permission "
1910 + android.Manifest.permission.BIND_INPUT_METHOD);
1911 continue;
1912 }
1913
Joe Onorato8a9b2202010-02-26 18:56:32 -08001914 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915
1916 try {
satoke7c6998e2011-06-03 17:57:59 +09001917 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001919 final String id = p.getId();
1920 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921
Amith Yamasanie861ec12010-03-24 21:39:27 -07001922 // System IMEs are enabled by default, unless there's a hard keyboard
1923 // and the system IME was explicitly disabled
1924 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1925 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001926 }
1927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001929 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001933 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
1937 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001938
1939 String defaultIme = Settings.Secure.getString(mContext
1940 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09001941 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001942 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001943 updateFromSettingsLocked();
1944 }
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001949
satokab751aa2010-09-14 19:17:36 +09001950 private void showInputMethodMenu() {
1951 showInputMethodMenuInternal(false);
1952 }
1953
1954 private void showInputMethodSubtypeMenu() {
1955 showInputMethodMenuInternal(true);
1956 }
1957
satok217f5482010-12-15 05:19:19 +09001958 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09001959 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09001960 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09001961 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1962 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09001963 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09001964 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09001965 }
satok217f5482010-12-15 05:19:19 +09001966 mContext.startActivity(intent);
1967 }
1968
1969 private void showConfigureInputMethods() {
1970 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
1971 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1972 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1973 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09001974 mContext.startActivity(intent);
1975 }
1976
satokab751aa2010-09-14 19:17:36 +09001977 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001978 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 final Context context = mContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 final PackageManager pm = context.getPackageManager();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 String lastInputMethodId = Settings.Secure.getString(context
1985 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09001986 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001988
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001989 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001990 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
1991 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09001992 if (immis == null || immis.size() == 0) {
1993 return;
1994 }
1995
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07001996 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997
Ken Wakasa761eb372011-03-04 19:06:18 +09001998 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
1999 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2000 new Comparator<InputMethodInfo>() {
2001 @Override
2002 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2003 if (imi2 == null) return 0;
2004 if (imi1 == null) return 1;
2005 if (pm == null) {
2006 return imi1.getId().compareTo(imi2.getId());
2007 }
2008 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2009 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2010 return imiId1.toString().compareTo(imiId2.toString());
2011 }
2012 });
satok913a8922010-08-26 21:53:41 +09002013
Ken Wakasa761eb372011-03-04 19:06:18 +09002014 sortedImmis.putAll(immis);
2015
2016 final ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>> imList =
2017 new ArrayList<Pair<CharSequence, Pair<InputMethodInfo, Integer>>>();
2018
2019 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002020 if (imi == null) continue;
2021 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002022 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002023 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2024 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002025 }
satokbb4aa062011-01-19 21:40:27 +09002026 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa81f6e3d2011-03-04 09:59:52 +09002027 final CharSequence label = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002028 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002029 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002030 if (DEBUG) {
2031 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2032 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002033 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002034 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2035 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2036 // We show all enabled IMEs and subtypes when an IME is shown.
2037 if (enabledSubtypeSet.contains(subtypeHashCode)
satok7dca6cd2011-06-29 18:06:25 +09002038 && (mInputShown || !subtype.isAuxiliary())) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002039 final CharSequence title;
satok4a28bde2011-06-29 21:03:40 +09002040 final String mode = subtype.getMode();
2041 title = TextUtils.concat(subtype.getDisplayName(context,
2042 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
2043 (TextUtils.isEmpty(label) ? "" : " (" + label + ")"));
Ken Wakasa761eb372011-03-04 19:06:18 +09002044 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2045 title, new Pair<InputMethodInfo, Integer>(imi, j)));
satok4a28bde2011-06-29 21:03:40 +09002046 // Removing this subtype from enabledSubtypeSet because we no longer
2047 // need to add an entry of this subtype to imList to avoid duplicated
2048 // entries.
2049 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002050 }
satokab751aa2010-09-14 19:17:36 +09002051 }
2052 } else {
Ken Wakasa761eb372011-03-04 19:06:18 +09002053 imList.add(new Pair<CharSequence, Pair<InputMethodInfo, Integer>>(
2054 label, new Pair<InputMethodInfo, Integer>(imi, NOT_A_SUBTYPE_ID)));
satokab751aa2010-09-14 19:17:36 +09002055 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002056 }
satok913a8922010-08-26 21:53:41 +09002057
Ken Wakasa761eb372011-03-04 19:06:18 +09002058 final int N = imList.size();
2059 mItems = new CharSequence[N];
2060 for (int i = 0; i < N; ++i) {
2061 mItems[i] = imList.get(i).first;
2062 }
satokab751aa2010-09-14 19:17:36 +09002063 mIms = new InputMethodInfo[N];
2064 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002065 int checkedItem = 0;
2066 for (int i = 0; i < N; ++i) {
Ken Wakasa761eb372011-03-04 19:06:18 +09002067 Pair<InputMethodInfo, Integer> value = imList.get(i).second;
satokab751aa2010-09-14 19:17:36 +09002068 mIms[i] = value.first;
2069 mSubtypeIds[i] = value.second;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002070 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002071 int subtypeId = mSubtypeIds[i];
2072 if ((subtypeId == NOT_A_SUBTYPE_ID)
2073 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2074 || (subtypeId == lastInputMethodSubtypeId)) {
2075 checkedItem = i;
2076 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
satokab751aa2010-09-14 19:17:36 +09002079
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002080 AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002081 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002082 public void onClick(DialogInterface dialog, int which) {
2083 hideInputMethodMenu();
2084 }
2085 };
satokd87c2592010-09-29 11:52:06 +09002086
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002087 TypedArray a = context.obtainStyledAttributes(null,
2088 com.android.internal.R.styleable.DialogPreference,
2089 com.android.internal.R.attr.alertDialogStyle, 0);
2090 mDialogBuilder = new AlertDialog.Builder(context)
2091 .setTitle(com.android.internal.R.string.select_input_method)
2092 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002093 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002094 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002096 }
2097 })
2098 .setIcon(a.getDrawable(
2099 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2100 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002101
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002102 mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
2103 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002104 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002105 public void onClick(DialogInterface dialog, int which) {
2106 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002107 if (mIms == null || mIms.length <= which
2108 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002109 return;
2110 }
2111 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002112 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002113 hideInputMethodMenu();
2114 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002115 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002116 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002117 subtypeId = NOT_A_SUBTYPE_ID;
2118 }
2119 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002120 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002123 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124
satokf90a33e2011-07-19 11:55:52 +09002125 if (showSubtypes && !(mKeyguardManager.isKeyguardLocked()
2126 && mKeyguardManager.isKeyguardSecure())) {
satok82beadf2010-12-27 19:03:06 +09002127 mDialogBuilder.setPositiveButton(
2128 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002129 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002130 @Override
satok7f35c8c2010-10-07 21:13:11 +09002131 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002132 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002133 }
2134 });
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002137 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 mSwitchingDialog.getWindow().setType(
2139 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002140 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 mSwitchingDialog.show();
2142 }
2143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002146 synchronized (mMethodMap) {
2147 hideInputMethodMenuLocked();
2148 }
2149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002150
The Android Open Source Project10592532009-03-18 17:39:46 -07002151 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002152 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
The Android Open Source Project10592532009-03-18 17:39:46 -07002154 if (mSwitchingDialog != null) {
2155 mSwitchingDialog.dismiss();
2156 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002158
The Android Open Source Project10592532009-03-18 17:39:46 -07002159 mDialogBuilder = null;
2160 mItems = null;
2161 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
satok42c5a162011-05-26 16:46:14 +09002166 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 public boolean setInputMethodEnabled(String id, boolean enabled) {
2168 synchronized (mMethodMap) {
2169 if (mContext.checkCallingOrSelfPermission(
2170 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2171 != PackageManager.PERMISSION_GRANTED) {
2172 throw new SecurityException(
2173 "Requires permission "
2174 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2175 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 long ident = Binder.clearCallingIdentity();
2178 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002179 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 } finally {
2181 Binder.restoreCallingIdentity(ident);
2182 }
2183 }
2184 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002185
2186 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2187 // Make sure this is a valid input method.
2188 InputMethodInfo imm = mMethodMap.get(id);
2189 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002190 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002191 }
2192
satokd87c2592010-09-29 11:52:06 +09002193 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2194 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002195
satokd87c2592010-09-29 11:52:06 +09002196 if (enabled) {
2197 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2198 if (pair.first.equals(id)) {
2199 // We are enabling this input method, but it is already enabled.
2200 // Nothing to do. The previous state was enabled.
2201 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002202 }
2203 }
satokd87c2592010-09-29 11:52:06 +09002204 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2205 // Previous state was disabled.
2206 return false;
2207 } else {
2208 StringBuilder builder = new StringBuilder();
2209 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2210 builder, enabledInputMethodsList, id)) {
2211 // Disabled input method is currently selected, switch to another one.
2212 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2213 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002214 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2215 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2216 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002217 }
2218 // Previous state was enabled.
2219 return true;
2220 } else {
2221 // We are disabling the input method but it is already disabled.
2222 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002223 return false;
2224 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002225 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002226 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227
satok57ffc002011-01-25 00:11:47 +09002228 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2229 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002230 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002231 }
2232
satok723a27e2010-11-11 14:58:11 +09002233 private void saveCurrentInputMethodAndSubtypeToHistory() {
2234 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2235 if (mCurrentSubtype != null) {
2236 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2237 }
satok57ffc002011-01-25 00:11:47 +09002238 if (canAddToLastInputMethod(mCurrentSubtype)) {
2239 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2240 }
satokab751aa2010-09-14 19:17:36 +09002241 }
2242
satok723a27e2010-11-11 14:58:11 +09002243 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2244 boolean setSubtypeOnly) {
2245 // Update the history of InputMethod and Subtype
2246 saveCurrentInputMethodAndSubtypeToHistory();
2247
2248 // Set Subtype here
2249 if (imi == null || subtypeId < 0) {
2250 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002251 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002252 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002253 if (subtypeId < imi.getSubtypeCount()) {
2254 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2255 mSettings.putSelectedSubtype(subtype.hashCode());
2256 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002257 } else {
2258 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2259 mCurrentSubtype = null;
2260 }
satokab751aa2010-09-14 19:17:36 +09002261 }
satok723a27e2010-11-11 14:58:11 +09002262
2263 if (!setSubtypeOnly) {
2264 // Set InputMethod here
2265 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2266 }
2267 }
2268
2269 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2270 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2271 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2272 // newDefaultIme is empty when there is no candidate for the selected IME.
2273 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2274 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2275 if (subtypeHashCode != null) {
2276 try {
2277 lastSubtypeId = getSubtypeIdFromHashCode(
2278 imi, Integer.valueOf(subtypeHashCode));
2279 } catch (NumberFormatException e) {
2280 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2281 }
2282 }
2283 }
2284 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002285 }
2286
2287 private int getSelectedInputMethodSubtypeId(String id) {
2288 InputMethodInfo imi = mMethodMap.get(id);
2289 if (imi == null) {
2290 return NOT_A_SUBTYPE_ID;
2291 }
satokab751aa2010-09-14 19:17:36 +09002292 int subtypeId;
2293 try {
2294 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2295 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2296 } catch (SettingNotFoundException e) {
2297 return NOT_A_SUBTYPE_ID;
2298 }
satok723a27e2010-11-11 14:58:11 +09002299 return getSubtypeIdFromHashCode(imi, subtypeId);
2300 }
2301
2302 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002303 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002304 final int subtypeCount = imi.getSubtypeCount();
2305 for (int i = 0; i < subtypeCount; ++i) {
2306 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002307 if (subtypeHashCode == ims.hashCode()) {
2308 return i;
2309 }
satokab751aa2010-09-14 19:17:36 +09002310 }
2311 }
2312 return NOT_A_SUBTYPE_ID;
2313 }
2314
satokdf31ae62011-01-15 06:19:44 +09002315 private static ArrayList<InputMethodSubtype> getApplicableSubtypesLocked(
2316 Resources res, List<InputMethodSubtype> subtypes) {
2317 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002318 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
2319 HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
2320 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002321 final int N = subtypes.size();
2322 boolean containsKeyboardSubtype = false;
2323 for (int i = 0; i < N; ++i) {
2324 InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002325 final String locale = subtype.getLocale();
2326 final String mode = subtype.getMode();
2327 // When system locale starts with subtype's locale, that subtype will be applicable
2328 // for system locale
2329 // For instance, it's clearly applicable for cases like system locale = en_US and
2330 // subtype = en, but it is not necessarily considered applicable for cases like system
2331 // locale = en and subtype = en_US.
2332 // We just call systemLocale.startsWith(locale) in this function because there is no
2333 // need to find applicable subtypes aggressively unlike
2334 // findLastResortApplicableSubtypeLocked.
2335 if (systemLocale.startsWith(locale)) {
2336 InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
2337 // If more applicable subtypes are contained, skip.
2338 if (applicableSubtype != null
2339 && systemLocale.equals(applicableSubtype.getLocale())) continue;
2340 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002341 if (!containsKeyboardSubtype
2342 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2343 containsKeyboardSubtype = true;
2344 }
2345 }
2346 }
satok4a28bde2011-06-29 21:03:40 +09002347 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002348 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002349 if (!containsKeyboardSubtype) {
2350 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002351 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002352 if (lastResortKeyboardSubtype != null) {
2353 applicableSubtypes.add(lastResortKeyboardSubtype);
2354 }
2355 }
2356 return applicableSubtypes;
2357 }
2358
satok4e4569d2010-11-19 18:45:53 +09002359 /**
2360 * If there are no selected subtypes, tries finding the most applicable one according to the
2361 * given locale.
2362 * @param subtypes this function will search the most applicable subtype in subtypes
2363 * @param mode subtypes will be filtered by mode
2364 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002365 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2366 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002367 * @return the most applicable subtypeId
2368 */
satokdf31ae62011-01-15 06:19:44 +09002369 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2370 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002371 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002372 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002373 return null;
satok8fbb1e82010-11-02 23:15:58 +09002374 }
satok4e4569d2010-11-19 18:45:53 +09002375 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002376 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002377 }
satok8fbb1e82010-11-02 23:15:58 +09002378 final String language = locale.substring(0, 2);
2379 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002380 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002381 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002382 final int N = subtypes.size();
2383 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002384 InputMethodSubtype subtype = subtypes.get(i);
2385 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002386 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2387 // and all subtypes with all modes can be candidates.
2388 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002389 if (firstMatchedModeSubtype == null) {
2390 firstMatchedModeSubtype = subtype;
2391 }
satok9ef02832010-11-04 21:17:48 +09002392 if (locale.equals(subtypeLocale)) {
2393 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002394 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002395 break;
2396 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2397 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002398 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002399 partialMatchFound = true;
2400 }
satok8fbb1e82010-11-02 23:15:58 +09002401 }
2402 }
2403
satok7599a7f2010-12-22 13:45:23 +09002404 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2405 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002406 }
2407
satok8fbb1e82010-11-02 23:15:58 +09002408 // The first subtype applicable to the system locale will be defined as the most applicable
2409 // subtype.
2410 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002411 if (applicableSubtype != null) {
2412 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2413 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2414 }
satok8fbb1e82010-11-02 23:15:58 +09002415 }
satokcd7cd292010-11-20 15:46:23 +09002416 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002417 }
2418
satok4e4569d2010-11-19 18:45:53 +09002419 // If there are no selected shortcuts, tries finding the most applicable ones.
2420 private Pair<InputMethodInfo, InputMethodSubtype>
2421 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2422 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2423 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002424 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002425 boolean foundInSystemIME = false;
2426
2427 // Search applicable subtype for each InputMethodInfo
2428 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002429 final String imiId = imi.getId();
2430 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2431 continue;
2432 }
satokcd7cd292010-11-20 15:46:23 +09002433 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002434 final List<InputMethodSubtype> enabledSubtypes =
2435 getEnabledInputMethodSubtypeList(imi, true);
2436 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002437 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002438 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002439 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002440 }
satokdf31ae62011-01-15 06:19:44 +09002441 // 2. Search by the system locale from enabledSubtypes.
2442 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002443 if (subtype == null) {
2444 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002445 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002446 }
satok7599a7f2010-12-22 13:45:23 +09002447 // 4. Search by the current subtype's locale from all subtypes.
2448 if (subtype == null && mCurrentSubtype != null) {
2449 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002450 mRes, getSubtypes(imi), mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002451 }
2452 // 5. Search by the system locale from all subtypes.
2453 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002454 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002455 subtype = findLastResortApplicableSubtypeLocked(
Ken Wakasa586f0512011-01-20 22:31:01 +09002456 mRes, getSubtypes(imi), mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002457 }
satokcd7cd292010-11-20 15:46:23 +09002458 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002459 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002460 // The current input method is the most applicable IME.
2461 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002462 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002463 break;
satok7599a7f2010-12-22 13:45:23 +09002464 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002465 // The system input method is 2nd applicable IME.
2466 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002467 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002468 if ((imi.getServiceInfo().applicationInfo.flags
2469 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2470 foundInSystemIME = true;
2471 }
satok4e4569d2010-11-19 18:45:53 +09002472 }
2473 }
2474 }
2475 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002476 if (mostApplicableIMI != null) {
2477 Slog.w(TAG, "Most applicable shortcut input method was:"
2478 + mostApplicableIMI.getId());
2479 if (mostApplicableSubtype != null) {
2480 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2481 + "," + mostApplicableSubtype.getMode() + ","
2482 + mostApplicableSubtype.getLocale());
2483 }
2484 }
satok4e4569d2010-11-19 18:45:53 +09002485 }
satokcd7cd292010-11-20 15:46:23 +09002486 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002487 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002488 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002489 } else {
2490 return null;
2491 }
2492 }
2493
satokab751aa2010-09-14 19:17:36 +09002494 /**
2495 * @return Return the current subtype of this input method.
2496 */
satok42c5a162011-05-26 16:46:14 +09002497 @Override
satokab751aa2010-09-14 19:17:36 +09002498 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002499 boolean subtypeIsSelected = false;
2500 try {
2501 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2502 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2503 } catch (SettingNotFoundException e) {
2504 }
satok3ef8b292010-11-23 06:06:29 +09002505 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002506 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002507 String lastInputMethodId = Settings.Secure.getString(
2508 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002509 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2510 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002511 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2512 if (imi != null) {
2513 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002514 // the most applicable subtype from explicitly or implicitly enabled
2515 // subtypes.
2516 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2517 getEnabledInputMethodSubtypeList(imi, true);
2518 // If there is only one explicitly or implicitly enabled subtype,
2519 // just returns it.
2520 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2521 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2522 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2523 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2524 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2525 SUBTYPE_MODE_KEYBOARD, null, true);
2526 if (mCurrentSubtype == null) {
2527 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2528 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2529 true);
2530 }
2531 }
satok4e4569d2010-11-19 18:45:53 +09002532 }
satokcd7cd292010-11-20 15:46:23 +09002533 } else {
satok3ef8b292010-11-23 06:06:29 +09002534 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002535 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002536 }
satok8fbb1e82010-11-02 23:15:58 +09002537 }
satok3ef8b292010-11-23 06:06:29 +09002538 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002539 }
satokab751aa2010-09-14 19:17:36 +09002540 }
2541
satokf3db1af2010-11-23 13:34:33 +09002542 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2543 InputMethodSubtype subtype) {
2544 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2545 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2546 } else {
2547 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2548 subtypes.add(subtype);
2549 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2550 }
2551 }
2552
satok4e4569d2010-11-19 18:45:53 +09002553 // TODO: We should change the return type from List to List<Parcelable>
satoke7c6998e2011-06-03 17:57:59 +09002554 @Override
satok4e4569d2010-11-19 18:45:53 +09002555 public List getShortcutInputMethodsAndSubtypes() {
2556 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002557 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002558 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002559 // If there are no selected shortcut subtypes, the framework will try to find
2560 // the most applicable subtype from all subtypes whose mode is
2561 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002562 Pair<InputMethodInfo, InputMethodSubtype> info =
2563 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2564 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002565 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002566 ret.add(info.first);
2567 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002568 }
satok3da92232011-01-11 22:46:30 +09002569 return ret;
satokf3db1af2010-11-23 13:34:33 +09002570 }
satokf3db1af2010-11-23 13:34:33 +09002571 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2572 ret.add(imi);
2573 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2574 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002575 }
2576 }
satokf3db1af2010-11-23 13:34:33 +09002577 return ret;
satok4e4569d2010-11-19 18:45:53 +09002578 }
2579 }
2580
satok42c5a162011-05-26 16:46:14 +09002581 @Override
satokb66d2872010-11-10 01:04:04 +09002582 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2583 synchronized (mMethodMap) {
2584 if (subtype != null && mCurMethodId != null) {
2585 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2586 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2587 if (subtypeId != NOT_A_SUBTYPE_ID) {
2588 setInputMethodLocked(mCurMethodId, subtypeId);
2589 return true;
2590 }
2591 }
2592 return false;
2593 }
2594 }
2595
satokd87c2592010-09-29 11:52:06 +09002596 /**
2597 * Utility class for putting and getting settings for InputMethod
2598 * TODO: Move all putters and getters of settings to this class.
2599 */
2600 private static class InputMethodSettings {
2601 // The string for enabled input method is saved as follows:
2602 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2603 private static final char INPUT_METHOD_SEPARATER = ':';
2604 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002605 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002606 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2607
satok723a27e2010-11-11 14:58:11 +09002608 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002609 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2610
satokdf31ae62011-01-15 06:19:44 +09002611 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002612 private final ContentResolver mResolver;
2613 private final HashMap<String, InputMethodInfo> mMethodMap;
2614 private final ArrayList<InputMethodInfo> mMethodList;
2615
2616 private String mEnabledInputMethodsStrCache;
2617
2618 private static void buildEnabledInputMethodsSettingString(
2619 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2620 String id = pair.first;
2621 ArrayList<String> subtypes = pair.second;
2622 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002623 // Inputmethod and subtypes are saved in the settings as follows:
2624 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2625 for (String subtypeId: subtypes) {
2626 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002627 }
2628 }
2629
2630 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002631 Resources res, ContentResolver resolver,
2632 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2633 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002634 mResolver = resolver;
2635 mMethodMap = methodMap;
2636 mMethodList = methodList;
2637 }
2638
2639 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2640 return createEnabledInputMethodListLocked(
2641 getEnabledInputMethodsAndSubtypeListLocked());
2642 }
2643
satok7f35c8c2010-10-07 21:13:11 +09002644 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002645 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2646 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002647 getEnabledInputMethodsAndSubtypeListLocked());
2648 }
2649
satok67ddf9c2010-11-17 09:45:54 +09002650 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2651 InputMethodInfo imi) {
2652 List<Pair<String, ArrayList<String>>> imsList =
2653 getEnabledInputMethodsAndSubtypeListLocked();
2654 ArrayList<InputMethodSubtype> enabledSubtypes =
2655 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002656 if (imi != null) {
2657 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2658 InputMethodInfo info = mMethodMap.get(imsPair.first);
2659 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002660 final int subtypeCount = info.getSubtypeCount();
2661 for (int i = 0; i < subtypeCount; ++i) {
2662 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002663 for (String s: imsPair.second) {
2664 if (String.valueOf(ims.hashCode()).equals(s)) {
2665 enabledSubtypes.add(ims);
2666 }
satok67ddf9c2010-11-17 09:45:54 +09002667 }
2668 }
satok884ef9a2010-11-18 10:39:46 +09002669 break;
satok67ddf9c2010-11-17 09:45:54 +09002670 }
satok67ddf9c2010-11-17 09:45:54 +09002671 }
2672 }
2673 return enabledSubtypes;
2674 }
2675
satokd87c2592010-09-29 11:52:06 +09002676 // At the initial boot, the settings for input methods are not set,
2677 // so we need to enable IME in that case.
2678 public void enableAllIMEsIfThereIsNoEnabledIME() {
2679 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2680 StringBuilder sb = new StringBuilder();
2681 final int N = mMethodList.size();
2682 for (int i = 0; i < N; i++) {
2683 InputMethodInfo imi = mMethodList.get(i);
2684 Slog.i(TAG, "Adding: " + imi.getId());
2685 if (i > 0) sb.append(':');
2686 sb.append(imi.getId());
2687 }
2688 putEnabledInputMethodsStr(sb.toString());
2689 }
2690 }
2691
satokbb4aa062011-01-19 21:40:27 +09002692 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002693 ArrayList<Pair<String, ArrayList<String>>> imsList
2694 = new ArrayList<Pair<String, ArrayList<String>>>();
2695 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2696 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2697 return imsList;
2698 }
satok723a27e2010-11-11 14:58:11 +09002699 mInputMethodSplitter.setString(enabledInputMethodsStr);
2700 while (mInputMethodSplitter.hasNext()) {
2701 String nextImsStr = mInputMethodSplitter.next();
2702 mSubtypeSplitter.setString(nextImsStr);
2703 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002704 ArrayList<String> subtypeHashes = new ArrayList<String>();
2705 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002706 String imeId = mSubtypeSplitter.next();
2707 while (mSubtypeSplitter.hasNext()) {
2708 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002709 }
2710 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2711 }
2712 }
2713 return imsList;
2714 }
2715
2716 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2717 if (reloadInputMethodStr) {
2718 getEnabledInputMethodsStr();
2719 }
2720 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2721 // Add in the newly enabled input method.
2722 putEnabledInputMethodsStr(id);
2723 } else {
2724 putEnabledInputMethodsStr(
2725 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2726 }
2727 }
2728
2729 /**
2730 * Build and put a string of EnabledInputMethods with removing specified Id.
2731 * @return the specified id was removed or not.
2732 */
2733 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2734 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2735 boolean isRemoved = false;
2736 boolean needsAppendSeparator = false;
2737 for (Pair<String, ArrayList<String>> ims: imsList) {
2738 String curId = ims.first;
2739 if (curId.equals(id)) {
2740 // We are disabling this input method, and it is
2741 // currently enabled. Skip it to remove from the
2742 // new list.
2743 isRemoved = true;
2744 } else {
2745 if (needsAppendSeparator) {
2746 builder.append(INPUT_METHOD_SEPARATER);
2747 } else {
2748 needsAppendSeparator = true;
2749 }
2750 buildEnabledInputMethodsSettingString(builder, ims);
2751 }
2752 }
2753 if (isRemoved) {
2754 // Update the setting with the new list of input methods.
2755 putEnabledInputMethodsStr(builder.toString());
2756 }
2757 return isRemoved;
2758 }
2759
2760 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2761 List<Pair<String, ArrayList<String>>> imsList) {
2762 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2763 for (Pair<String, ArrayList<String>> ims: imsList) {
2764 InputMethodInfo info = mMethodMap.get(ims.first);
2765 if (info != null) {
2766 res.add(info);
2767 }
2768 }
2769 return res;
2770 }
2771
satok7f35c8c2010-10-07 21:13:11 +09002772 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002773 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002774 List<Pair<String, ArrayList<String>>> imsList) {
2775 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2776 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2777 for (Pair<String, ArrayList<String>> ims : imsList) {
2778 InputMethodInfo info = mMethodMap.get(ims.first);
2779 if (info != null) {
2780 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2781 }
2782 }
2783 return res;
2784 }
2785
satokd87c2592010-09-29 11:52:06 +09002786 private void putEnabledInputMethodsStr(String str) {
2787 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2788 mEnabledInputMethodsStrCache = str;
2789 }
2790
2791 private String getEnabledInputMethodsStr() {
2792 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2793 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002794 if (DEBUG) {
2795 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2796 }
satokd87c2592010-09-29 11:52:06 +09002797 return mEnabledInputMethodsStrCache;
2798 }
satok723a27e2010-11-11 14:58:11 +09002799
2800 private void saveSubtypeHistory(
2801 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2802 StringBuilder builder = new StringBuilder();
2803 boolean isImeAdded = false;
2804 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2805 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2806 newSubtypeId);
2807 isImeAdded = true;
2808 }
2809 for (Pair<String, String> ime: savedImes) {
2810 String imeId = ime.first;
2811 String subtypeId = ime.second;
2812 if (TextUtils.isEmpty(subtypeId)) {
2813 subtypeId = NOT_A_SUBTYPE_ID_STR;
2814 }
2815 if (isImeAdded) {
2816 builder.append(INPUT_METHOD_SEPARATER);
2817 } else {
2818 isImeAdded = true;
2819 }
2820 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2821 subtypeId);
2822 }
2823 // Remove the last INPUT_METHOD_SEPARATER
2824 putSubtypeHistoryStr(builder.toString());
2825 }
2826
2827 public void addSubtypeToHistory(String imeId, String subtypeId) {
2828 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2829 for (Pair<String, String> ime: subtypeHistory) {
2830 if (ime.first.equals(imeId)) {
2831 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002832 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002833 + ime.second);
2834 }
2835 // We should break here
2836 subtypeHistory.remove(ime);
2837 break;
2838 }
2839 }
satokbb4aa062011-01-19 21:40:27 +09002840 if (DEBUG) {
2841 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2842 }
satok723a27e2010-11-11 14:58:11 +09002843 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2844 }
2845
2846 private void putSubtypeHistoryStr(String str) {
2847 if (DEBUG) {
2848 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2849 }
2850 Settings.Secure.putString(
2851 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2852 }
2853
2854 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2855 // Gets the first one from the history
2856 return getLastSubtypeForInputMethodLockedInternal(null);
2857 }
2858
2859 public String getLastSubtypeForInputMethodLocked(String imeId) {
2860 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2861 if (ime != null) {
2862 return ime.second;
2863 } else {
2864 return null;
2865 }
2866 }
2867
2868 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
2869 List<Pair<String, ArrayList<String>>> enabledImes =
2870 getEnabledInputMethodsAndSubtypeListLocked();
2871 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09002872 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09002873 final String imeInTheHistory = imeAndSubtype.first;
2874 // If imeId is empty, returns the first IME and subtype in the history
2875 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
2876 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09002877 final String subtypeHashCode =
2878 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
2879 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09002880 if (!TextUtils.isEmpty(subtypeHashCode)) {
2881 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002882 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002883 }
2884 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
2885 }
2886 }
2887 }
2888 if (DEBUG) {
2889 Slog.d(TAG, "No enabled IME found in the history");
2890 }
2891 return null;
2892 }
2893
satokdf31ae62011-01-15 06:19:44 +09002894 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09002895 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
2896 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
2897 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09002898 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
2899 if (explicitlyEnabledSubtypes.size() == 0) {
2900 // If there are no explicitly enabled subtypes, applicable subtypes are
2901 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09002902 InputMethodInfo ime = mMethodMap.get(imeId);
2903 // If IME is enabled and no subtypes are enabled, applicable subtypes
2904 // are enabled implicitly, so needs to treat them to be enabled.
Ken Wakasa586f0512011-01-20 22:31:01 +09002905 if (ime != null && ime.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09002906 List<InputMethodSubtype> implicitlySelectedSubtypes =
Ken Wakasa586f0512011-01-20 22:31:01 +09002907 getApplicableSubtypesLocked(mRes, getSubtypes(ime));
satokdf31ae62011-01-15 06:19:44 +09002908 if (implicitlySelectedSubtypes != null) {
2909 final int N = implicitlySelectedSubtypes.size();
2910 for (int i = 0; i < N; ++i) {
2911 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
2912 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
2913 return subtypeHashCode;
2914 }
2915 }
2916 }
2917 }
2918 } else {
satokf6cafb62011-01-17 16:29:02 +09002919 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09002920 if (s.equals(subtypeHashCode)) {
2921 // If both imeId and subtypeId are enabled, return subtypeId.
2922 return s;
2923 }
satok723a27e2010-11-11 14:58:11 +09002924 }
2925 }
2926 // If imeId was enabled but subtypeId was disabled.
2927 return NOT_A_SUBTYPE_ID_STR;
2928 }
2929 }
2930 // If both imeId and subtypeId are disabled, return null
2931 return null;
2932 }
2933
2934 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
2935 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
2936 final String subtypeHistoryStr = getSubtypeHistoryStr();
2937 if (TextUtils.isEmpty(subtypeHistoryStr)) {
2938 return imsList;
2939 }
2940 mInputMethodSplitter.setString(subtypeHistoryStr);
2941 while (mInputMethodSplitter.hasNext()) {
2942 String nextImsStr = mInputMethodSplitter.next();
2943 mSubtypeSplitter.setString(nextImsStr);
2944 if (mSubtypeSplitter.hasNext()) {
2945 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2946 // The first element is ime id.
2947 String imeId = mSubtypeSplitter.next();
2948 while (mSubtypeSplitter.hasNext()) {
2949 subtypeId = mSubtypeSplitter.next();
2950 break;
2951 }
2952 imsList.add(new Pair<String, String>(imeId, subtypeId));
2953 }
2954 }
2955 return imsList;
2956 }
2957
2958 private String getSubtypeHistoryStr() {
2959 if (DEBUG) {
2960 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
2961 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
2962 }
2963 return Settings.Secure.getString(
2964 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
2965 }
2966
2967 public void putSelectedInputMethod(String imeId) {
2968 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
2969 }
2970
2971 public void putSelectedSubtype(int subtypeId) {
2972 Settings.Secure.putInt(
2973 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
2974 }
satokd87c2592010-09-29 11:52:06 +09002975 }
2976
satoke7c6998e2011-06-03 17:57:59 +09002977 private static class InputMethodFileManager {
2978 private static final String SYSTEM_PATH = "system";
2979 private static final String INPUT_METHOD_PATH = "inputmethod";
2980 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
2981 private static final String NODE_SUBTYPES = "subtypes";
2982 private static final String NODE_SUBTYPE = "subtype";
2983 private static final String NODE_IMI = "imi";
2984 private static final String ATTR_ID = "id";
2985 private static final String ATTR_LABEL = "label";
2986 private static final String ATTR_ICON = "icon";
2987 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
2988 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
2989 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
2990 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
2991 private final AtomicFile mAdditionalInputMethodSubtypeFile;
2992 private final HashMap<String, InputMethodInfo> mMethodMap;
2993 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
2994 new HashMap<String, List<InputMethodSubtype>>();
2995 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
2996 if (methodMap == null) {
2997 throw new NullPointerException("methodMap is null");
2998 }
2999 mMethodMap = methodMap;
3000 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3001 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3002 if (!inputMethodDir.mkdirs()) {
3003 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3004 }
3005 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3006 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3007 if (!subtypeFile.exists()) {
3008 // If "subtypes.xml" doesn't exist, create a blank file.
3009 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3010 methodMap);
3011 } else {
3012 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3013 }
3014 }
3015
3016 private void deleteAllInputMethodSubtypes(String imiId) {
3017 synchronized (mMethodMap) {
3018 mSubtypesMap.remove(imiId);
3019 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3020 mMethodMap);
3021 }
3022 }
3023
3024 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003025 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003026 synchronized (mMethodMap) {
satok4a28bde2011-06-29 21:03:40 +09003027 final HashSet<InputMethodSubtype> existingSubtypes =
3028 new HashSet<InputMethodSubtype>();
3029 for (int i = 0; i < imi.getSubtypeCount(); ++i) {
3030 existingSubtypes.add(imi.getSubtypeAt(i));
3031 }
3032
satoke7c6998e2011-06-03 17:57:59 +09003033 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3034 final int N = additionalSubtypes.length;
3035 for (int i = 0; i < N; ++i) {
3036 final InputMethodSubtype subtype = additionalSubtypes[i];
satok4a28bde2011-06-29 21:03:40 +09003037 if (!subtypes.contains(subtype) && !existingSubtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003038 subtypes.add(subtype);
3039 }
3040 }
satok4a28bde2011-06-29 21:03:40 +09003041 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003042 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3043 mMethodMap);
3044 }
3045 }
3046
3047 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3048 synchronized (mMethodMap) {
3049 return mSubtypesMap;
3050 }
3051 }
3052
3053 private static void writeAdditionalInputMethodSubtypes(
3054 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3055 HashMap<String, InputMethodInfo> methodMap) {
3056 // Safety net for the case that this function is called before methodMap is set.
3057 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3058 FileOutputStream fos = null;
3059 try {
3060 fos = subtypesFile.startWrite();
3061 final XmlSerializer out = new FastXmlSerializer();
3062 out.setOutput(fos, "utf-8");
3063 out.startDocument(null, true);
3064 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3065 out.startTag(null, NODE_SUBTYPES);
3066 for (String imiId : allSubtypes.keySet()) {
3067 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3068 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3069 continue;
3070 }
3071 out.startTag(null, NODE_IMI);
3072 out.attribute(null, ATTR_ID, imiId);
3073 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3074 final int N = subtypesList.size();
3075 for (int i = 0; i < N; ++i) {
3076 final InputMethodSubtype subtype = subtypesList.get(i);
3077 out.startTag(null, NODE_SUBTYPE);
3078 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3079 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3080 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3081 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3082 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3083 out.attribute(null, ATTR_IS_AUXILIARY,
3084 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3085 out.endTag(null, NODE_SUBTYPE);
3086 }
3087 out.endTag(null, NODE_IMI);
3088 }
3089 out.endTag(null, NODE_SUBTYPES);
3090 out.endDocument();
3091 subtypesFile.finishWrite(fos);
3092 } catch (java.io.IOException e) {
3093 Slog.w(TAG, "Error writing subtypes", e);
3094 if (fos != null) {
3095 subtypesFile.failWrite(fos);
3096 }
3097 }
3098 }
3099
3100 private static void readAdditionalInputMethodSubtypes(
3101 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3102 if (allSubtypes == null || subtypesFile == null) return;
3103 allSubtypes.clear();
3104 FileInputStream fis = null;
3105 try {
3106 fis = subtypesFile.openRead();
3107 final XmlPullParser parser = Xml.newPullParser();
3108 parser.setInput(fis, null);
3109 int type = parser.getEventType();
3110 // Skip parsing until START_TAG
3111 while ((type = parser.next()) != XmlPullParser.START_TAG
3112 && type != XmlPullParser.END_DOCUMENT) {}
3113 String firstNodeName = parser.getName();
3114 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3115 throw new XmlPullParserException("Xml doesn't start with subtypes");
3116 }
3117 final int depth =parser.getDepth();
3118 String currentImiId = null;
3119 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3120 while (((type = parser.next()) != XmlPullParser.END_TAG
3121 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3122 if (type != XmlPullParser.START_TAG)
3123 continue;
3124 final String nodeName = parser.getName();
3125 if (NODE_IMI.equals(nodeName)) {
3126 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3127 if (TextUtils.isEmpty(currentImiId)) {
3128 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3129 continue;
3130 }
3131 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3132 allSubtypes.put(currentImiId, tempSubtypesArray);
3133 } else if (NODE_SUBTYPE.equals(nodeName)) {
3134 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3135 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3136 continue;
3137 }
3138 final int icon = Integer.valueOf(
3139 parser.getAttributeValue(null, ATTR_ICON));
3140 final int label = Integer.valueOf(
3141 parser.getAttributeValue(null, ATTR_LABEL));
3142 final String imeSubtypeLocale =
3143 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3144 final String imeSubtypeMode =
3145 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3146 final String imeSubtypeExtraValue =
3147 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003148 final boolean isAuxiliary = "1".equals(String.valueOf(
3149 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003150 final InputMethodSubtype subtype =
3151 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3152 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3153 tempSubtypesArray.add(subtype);
3154 }
3155 }
3156 } catch (XmlPullParserException e) {
3157 Slog.w(TAG, "Error reading subtypes: " + e);
3158 return;
3159 } catch (java.io.IOException e) {
3160 Slog.w(TAG, "Error reading subtypes: " + e);
3161 return;
3162 } catch (NumberFormatException e) {
3163 Slog.w(TAG, "Error reading subtypes: " + e);
3164 return;
3165 } finally {
3166 if (fis != null) {
3167 try {
3168 fis.close();
3169 } catch (java.io.IOException e1) {
3170 Slog.w(TAG, "Failed to close.");
3171 }
3172 }
3173 }
3174 }
3175 }
3176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 @Override
3180 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3181 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3182 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3185 + Binder.getCallingPid()
3186 + ", uid=" + Binder.getCallingUid());
3187 return;
3188 }
3189
3190 IInputMethod method;
3191 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 synchronized (mMethodMap) {
3196 p.println("Current Input Method Manager state:");
3197 int N = mMethodList.size();
3198 p.println(" Input Methods:");
3199 for (int i=0; i<N; i++) {
3200 InputMethodInfo info = mMethodList.get(i);
3201 p.println(" InputMethod #" + i + ":");
3202 info.dump(p, " ");
3203 }
3204 p.println(" Clients:");
3205 for (ClientState ci : mClients.values()) {
3206 p.println(" Client " + ci + ":");
3207 p.println(" client=" + ci.client);
3208 p.println(" inputContext=" + ci.inputContext);
3209 p.println(" sessionRequested=" + ci.sessionRequested);
3210 p.println(" curSession=" + ci.curSession);
3211 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003212 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003214 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3215 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3217 + " mBoundToMethod=" + mBoundToMethod);
3218 p.println(" mCurToken=" + mCurToken);
3219 p.println(" mCurIntent=" + mCurIntent);
3220 method = mCurMethod;
3221 p.println(" mCurMethod=" + mCurMethod);
3222 p.println(" mEnabledSession=" + mEnabledSession);
3223 p.println(" mShowRequested=" + mShowRequested
3224 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3225 + " mShowForced=" + mShowForced
3226 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003227 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003229
Jeff Brownb88102f2010-09-08 11:49:43 -07003230 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 pw.flush();
3233 try {
3234 client.client.asBinder().dump(fd, args);
3235 } catch (RemoteException e) {
3236 p.println("Input method client dead: " + e);
3237 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003238 } else {
3239 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003241
Jeff Brownb88102f2010-09-08 11:49:43 -07003242 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 pw.flush();
3245 try {
3246 method.asBinder().dump(fd, args);
3247 } catch (RemoteException e) {
3248 p.println("Input method service dead: " + e);
3249 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003250 } else {
3251 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
3253 }
3254}