blob: 6ddbf5a5a95f6550ae9e7d87d725442f6b5f0dc6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
satoke7c6998e2011-06-03 17:57:59 +090019import com.android.internal.os.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.HandlerCaller;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
satoke7c6998e2011-06-03 17:57:59 +090031import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090033import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35import android.app.ActivityManagerNative;
36import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090037import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090038import android.app.Notification;
39import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070040import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.ComponentName;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.DialogInterface.OnCancelListener;
46import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090047import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070049import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070054import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.res.Resources;
56import android.content.res.TypedArray;
57import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080058import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090060import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Handler;
62import android.os.IBinder;
63import android.os.IInterface;
64import android.os.Message;
65import android.os.Parcel;
66import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080067import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070071import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090072import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090074import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090076import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090077import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.PrintWriterPrinter;
79import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090080import android.util.Slog;
81import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090083import android.view.LayoutInflater;
84import android.view.View;
85import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090087import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.inputmethod.InputBinding;
89import android.view.inputmethod.InputMethod;
90import android.view.inputmethod.InputMethodInfo;
91import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090092import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090093import android.widget.ArrayAdapter;
94import android.widget.RadioButton;
95import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
satoke7c6998e2011-06-03 17:57:59 +090097import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +090099import java.io.FileInputStream;
100import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.IOException;
102import java.io.PrintWriter;
103import java.util.ArrayList;
Ken Wakasa761eb372011-03-04 19:06:18 +0900104import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900106import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.util.List;
satok913a8922010-08-26 21:53:41 +0900108import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110/**
111 * This class provides a system service that manages input methods.
112 */
113public class InputMethodManagerService extends IInputMethodManager.Stub
114 implements ServiceConnection, Handler.Callback {
115 static final boolean DEBUG = false;
116 static final String TAG = "InputManagerService";
117
118 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900119 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900120 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900121 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 static final int MSG_UNBIND_INPUT = 1000;
124 static final int MSG_BIND_INPUT = 1010;
125 static final int MSG_SHOW_SOFT_INPUT = 1020;
126 static final int MSG_HIDE_SOFT_INPUT = 1030;
127 static final int MSG_ATTACH_TOKEN = 1040;
128 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_START_INPUT = 2000;
131 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 static final int MSG_UNBIND_METHOD = 3000;
134 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
satokf9f01002011-05-19 21:31:50 +0900138 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
139
satokab751aa2010-09-14 19:17:36 +0900140 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900141 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900142 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
143 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900144 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satok4e4569d2010-11-19 18:45:53 +0900145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800147 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900149 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final IWindowManager mIWindowManager;
152 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900153 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 // All known input methods. mMethodMap also serves as the global
158 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900159 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
160 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900161 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
162 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800163
satok7cfc0ed2011-06-20 21:29:36 +0900164 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700165 private NotificationManager mNotificationManager;
166 private KeyguardManager mKeyguardManager;
167 private StatusBarManagerService mStatusBar;
168 private Notification mImeSwitcherNotification;
169 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900170 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900171 private boolean mNotificationShown;
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 class SessionState {
174 final ClientState client;
175 final IInputMethod method;
176 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
179 public String toString() {
180 return "SessionState{uid " + client.uid + " pid " + client.pid
181 + " method " + Integer.toHexString(
182 System.identityHashCode(method))
183 + " session " + Integer.toHexString(
184 System.identityHashCode(session))
185 + "}";
186 }
187
188 SessionState(ClientState _client, IInputMethod _method,
189 IInputMethodSession _session) {
190 client = _client;
191 method = _method;
192 session = _session;
193 }
194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 class ClientState {
197 final IInputMethodClient client;
198 final IInputContext inputContext;
199 final int uid;
200 final int pid;
201 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 boolean sessionRequested;
204 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "ClientState{" + Integer.toHexString(
209 System.identityHashCode(this)) + " uid " + uid
210 + " pid " + pid + "}";
211 }
212
213 ClientState(IInputMethodClient _client, IInputContext _inputContext,
214 int _uid, int _pid) {
215 client = _client;
216 inputContext = _inputContext;
217 uid = _uid;
218 pid = _pid;
219 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
220 }
221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 final HashMap<IBinder, ClientState> mClients
224 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700227 * Set once the system is ready to run third party code.
228 */
229 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * Id of the currently selected input method.
233 */
234 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * The current binding sequence number, incremented every time there is
238 * a new bind performed.
239 */
240 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
243 * The client that is currently bound to an input method.
244 */
245 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700248 * The last window token that gained focus.
249 */
250 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800251
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * The input context last provided by the current client.
254 */
255 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * The attributes last provided by the current client.
259 */
260 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 /**
263 * The input method ID of the input method service that we are currently
264 * connected to or in the process of connecting to.
265 */
266 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 /**
satokab751aa2010-09-14 19:17:36 +0900269 * The current subtype of the current input method.
270 */
271 private InputMethodSubtype mCurrentSubtype;
272
satok4e4569d2010-11-19 18:45:53 +0900273 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900274 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
275 mShortcutInputMethodsAndSubtypes =
276 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900277
278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 * Set to true if our ServiceConnection is currently actively bound to
280 * a service (whether or not we have gotten its IBinder back yet).
281 */
282 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Set if the client has asked for the input method to be shown.
286 */
287 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 /**
290 * Set if we were explicitly told to show the input method.
291 */
292 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 /**
295 * Set if we were forced to be shown.
296 */
297 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Set if we last told the input method to show itself.
301 */
302 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
305 * The Intent used to connect to the current input method.
306 */
307 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 /**
310 * The token we have made for the currently active input method, to
311 * identify it in the future.
312 */
313 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * If non-null, this is the input method service we are currently connected
317 * to.
318 */
319 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * Time that we last initiated a bind to the input method, to determine
323 * if we should try to disconnect and reconnect to it.
324 */
325 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Have we called mCurMethod.bindInput()?
329 */
330 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * Currently enabled session. Only touched by service thread, not
334 * protected by a lock.
335 */
336 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * True if the screen is on. The value is true initially.
340 */
341 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
Joe Onorato857fd9b2011-01-27 15:08:35 -0800343 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
344 int mImeWindowVis;
345
Ken Wakasa05dbb652011-08-22 15:22:43 +0900346 private AlertDialog.Builder mDialogBuilder;
347 private AlertDialog mSwitchingDialog;
348 private InputMethodInfo[] mIms;
349 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 class SettingsObserver extends ContentObserver {
352 SettingsObserver(Handler handler) {
353 super(handler);
354 ContentResolver resolver = mContext.getContentResolver();
355 resolver.registerContentObserver(Settings.Secure.getUriFor(
356 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900357 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900358 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
359 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900360 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 @Override public void onChange(boolean selfChange) {
364 synchronized (mMethodMap) {
365 updateFromSettingsLocked();
366 }
367 }
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
371 @Override
372 public void onReceive(Context context, Intent intent) {
373 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
374 mScreenOn = true;
375 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
376 mScreenOn = false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
378 hideInputMethodMenu();
379 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
383
384 // Inform the current client of the change in active status
385 try {
386 if (mCurClient != null && mCurClient.client != null) {
387 mCurClient.client.setActive(mScreenOn);
388 }
389 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800390 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 + mCurClient.pid + " uid " + mCurClient.uid);
392 }
393 }
394 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800395
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800396 class MyPackageMonitor extends PackageMonitor {
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800399 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800401 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
403 final int N = mMethodList.size();
404 if (curInputMethodId != null) {
405 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 InputMethodInfo imi = mMethodList.get(i);
407 if (imi.getId().equals(curInputMethodId)) {
408 for (String pkg : packages) {
409 if (imi.getPackageName().equals(pkg)) {
410 if (!doit) {
411 return true;
412 }
satok723a27e2010-11-11 14:58:11 +0900413 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 chooseNewDefaultIMELocked();
415 return true;
416 }
417 }
418 }
419 }
420 }
421 }
422 return false;
423 }
424
425 @Override
426 public void onSomePackagesChanged() {
427 synchronized (mMethodMap) {
428 InputMethodInfo curIm = null;
429 String curInputMethodId = Settings.Secure.getString(mContext
430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
434 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900435 final String imiId = imi.getId();
436 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 curIm = imi;
438 }
satoke7c6998e2011-06-03 17:57:59 +0900439
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900441 if (isPackageModified(imi.getPackageName())) {
442 mFileManager.deleteAllInputMethodSubtypes(imiId);
443 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800444 if (change == PACKAGE_TEMPORARY_CHANGE
445 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800446 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 + imi.getComponent());
448 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800452
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 buildInputMethodListLocked(mMethodList, mMethodMap);
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800458 int change = isPackageDisappearing(curIm.getPackageName());
459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 ServiceInfo si = null;
462 try {
463 si = mContext.getPackageManager().getServiceInfo(
464 curIm.getComponent(), 0);
465 } catch (PackageManager.NameNotFoundException ex) {
466 }
467 if (si == null) {
468 // Uh oh, current input method is no longer around!
469 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Joe Onorato857fd9b2011-01-27 15:08:35 -0800471 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700472 updateImeWindowStatusLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800474 changed = true;
475 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900477 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 }
479 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800480 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 }
satokab751aa2010-09-14 19:17:36 +0900482
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800483 if (curIm == null) {
484 // We currently don't have a default input method... is
485 // one now available?
486 changed = chooseNewDefaultIMELocked();
487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800488
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 if (changed) {
490 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492 }
493 }
494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 class MethodCallback extends IInputMethodCallback.Stub {
497 final IInputMethod mMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 MethodCallback(IInputMethod method) {
500 mMethod = method;
501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
satoke7c6998e2011-06-03 17:57:59 +0900503 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 public void finishedEvent(int seq, boolean handled) throws RemoteException {
505 }
506
satoke7c6998e2011-06-03 17:57:59 +0900507 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 public void sessionCreated(IInputMethodSession session) throws RemoteException {
509 onSessionCreated(mMethod, session);
510 }
511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800512
Dianne Hackborn661cd522011-08-22 00:26:20 -0700513 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800515 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 mHandler = new Handler(this);
517 mIWindowManager = IWindowManager.Stub.asInterface(
518 ServiceManager.getService(Context.WINDOW_SERVICE));
519 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900520 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public void executeMessage(Message msg) {
522 handleMessage(msg);
523 }
524 });
satok7cfc0ed2011-06-20 21:29:36 +0900525
satok7cfc0ed2011-06-20 21:29:36 +0900526 mImeSwitcherNotification = new Notification();
527 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
528 mImeSwitcherNotification.when = 0;
529 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
530 mImeSwitcherNotification.tickerText = null;
531 mImeSwitcherNotification.defaults = 0; // please be quiet
532 mImeSwitcherNotification.sound = null;
533 mImeSwitcherNotification.vibrate = null;
534 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900535 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900536
537 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900538
satoke7c6998e2011-06-03 17:57:59 +0900539 synchronized (mMethodMap) {
540 mFileManager = new InputMethodFileManager(mMethodMap);
541 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800542
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800543 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 IntentFilter screenOnOffFilt = new IntentFilter();
546 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
547 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700548 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800550
satok7cfc0ed2011-06-20 21:29:36 +0900551 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900552
satokd87c2592010-09-29 11:52:06 +0900553 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900554 mSettings = new InputMethodSettings(
555 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900557 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558
satokd87c2592010-09-29 11:52:06 +0900559 if (TextUtils.isEmpty(Settings.Secure.getString(
560 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900562 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
564 try {
satokd87c2592010-09-29 11:52:06 +0900565 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 imi.getPackageName(), 0).getResources();
567 if (res.getBoolean(imi.getIsDefaultResourceId())) {
568 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 }
571 } catch (PackageManager.NameNotFoundException ex) {
572 } catch (Resources.NotFoundException ex) {
573 }
574 }
575 }
satokd87c2592010-09-29 11:52:06 +0900576 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900577 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800578 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900581 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 mSettingsObserver = new SettingsObserver(mHandler);
586 updateFromSettingsLocked();
587 }
588
589 @Override
590 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
591 throws RemoteException {
592 try {
593 return super.onTransact(code, data, reply, flags);
594 } catch (RuntimeException e) {
595 // The input method manager only throws security exceptions, so let's
596 // log all others.
597 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800598 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600 throw e;
601 }
602 }
603
Dianne Hackborn661cd522011-08-22 00:26:20 -0700604 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700605 synchronized (mMethodMap) {
606 if (!mSystemReady) {
607 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700608 mKeyguardManager = (KeyguardManager)
609 mContext.getSystemService(Context.KEYGUARD_SERVICE);
610 mNotificationManager = (NotificationManager)
611 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
612 mStatusBar = statusBar;
613 statusBar.setIconVisibility("ime", false);
614 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900615 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
616 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700617 try {
618 startInputInnerLocked();
619 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800620 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700621 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700622 }
623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800625
Dianne Hackborn661cd522011-08-22 00:26:20 -0700626 void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900627 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700628 }
629
satoke7c6998e2011-06-03 17:57:59 +0900630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public List<InputMethodInfo> getInputMethodList() {
632 synchronized (mMethodMap) {
633 return new ArrayList<InputMethodInfo>(mMethodList);
634 }
635 }
636
satoke7c6998e2011-06-03 17:57:59 +0900637 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 public List<InputMethodInfo> getEnabledInputMethodList() {
639 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900640 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642 }
643
satokbb4aa062011-01-19 21:40:27 +0900644 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
645 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
646 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
647 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
648 for (InputMethodInfo imi: getEnabledInputMethodList()) {
649 enabledInputMethodAndSubtypes.put(
650 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
651 }
652 return enabledInputMethodAndSubtypes;
653 }
654
655 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
656 boolean allowsImplicitlySelectedSubtypes) {
657 if (imi == null && mCurMethodId != null) {
658 imi = mMethodMap.get(mCurMethodId);
659 }
satok7265d9b2011-02-14 15:47:30 +0900660 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900661 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900662 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900663 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900664 }
satok7265d9b2011-02-14 15:47:30 +0900665 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900666 }
667
satoke7c6998e2011-06-03 17:57:59 +0900668 @Override
satok16331c82010-12-20 23:48:46 +0900669 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
670 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900671 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900672 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900673 }
674 }
675
satoke7c6998e2011-06-03 17:57:59 +0900676 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public void addClient(IInputMethodClient client,
678 IInputContext inputContext, int uid, int pid) {
679 synchronized (mMethodMap) {
680 mClients.put(client.asBinder(), new ClientState(client,
681 inputContext, uid, pid));
682 }
683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
satoke7c6998e2011-06-03 17:57:59 +0900685 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 public void removeClient(IInputMethodClient client) {
687 synchronized (mMethodMap) {
688 mClients.remove(client.asBinder());
689 }
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 void executeOrSendMessage(IInterface target, Message msg) {
693 if (target.asBinder() instanceof Binder) {
694 mCaller.sendMessage(msg);
695 } else {
696 handleMessage(msg);
697 msg.recycle();
698 }
699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700701 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 + mCurClient.client.asBinder());
705 if (mBoundToMethod) {
706 mBoundToMethod = false;
707 if (mCurMethod != null) {
708 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
709 MSG_UNBIND_INPUT, mCurMethod));
710 }
711 }
712 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
713 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
714 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 // Call setActive(false) on the old client
717 try {
718 mCurClient.client.setActive(false);
719 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 + mCurClient.pid + " uid " + mCurClient.uid);
722 }
723 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800724
The Android Open Source Project10592532009-03-18 17:39:46 -0700725 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 private int getImeShowFlags() {
730 int flags = 0;
731 if (mShowForced) {
732 flags |= InputMethod.SHOW_FORCED
733 | InputMethod.SHOW_EXPLICIT;
734 } else if (mShowExplicitlyRequested) {
735 flags |= InputMethod.SHOW_EXPLICIT;
736 }
737 return flags;
738 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 private int getAppShowFlags() {
741 int flags = 0;
742 if (mShowForced) {
743 flags |= InputMethodManager.SHOW_FORCED;
744 } else if (!mShowExplicitlyRequested) {
745 flags |= InputMethodManager.SHOW_IMPLICIT;
746 }
747 return flags;
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
751 if (!mBoundToMethod) {
752 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
753 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
754 mBoundToMethod = true;
755 }
756 final SessionState session = mCurClient.curSession;
757 if (initial) {
758 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
759 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
760 } else {
761 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
762 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
763 }
764 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800765 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800766 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768 return needResult
769 ? new InputBindResult(session.session, mCurId, mCurSeq)
770 : null;
771 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 InputBindResult startInputLocked(IInputMethodClient client,
774 IInputContext inputContext, EditorInfo attribute,
775 boolean initial, boolean needResult) {
776 // If no method is currently selected, do nothing.
777 if (mCurMethodId == null) {
778 return mNoBinding;
779 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 ClientState cs = mClients.get(client.asBinder());
782 if (cs == null) {
783 throw new IllegalArgumentException("unknown client "
784 + client.asBinder());
785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 try {
788 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
789 // Check with the window manager to make sure this client actually
790 // has a window with focus. If not, reject. This is thread safe
791 // because if the focus changes some time before or after, the
792 // next client receiving focus that has any interest in input will
793 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
796 return null;
797 }
798 } catch (RemoteException e) {
799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if (mCurClient != cs) {
802 // If the client is changing, we need to switch over to the new
803 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700804 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800805 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 + cs.client.asBinder());
807
808 // If the screen is on, inform the new client it is active
809 if (mScreenOn) {
810 try {
811 cs.client.setActive(mScreenOn);
812 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800813 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 + cs.pid + " uid " + cs.uid);
815 }
816 }
817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // Bump up the sequence for this client and attach it.
820 mCurSeq++;
821 if (mCurSeq <= 0) mCurSeq = 1;
822 mCurClient = cs;
823 mCurInputContext = inputContext;
824 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // Check if the input method is changing.
827 if (mCurId != null && mCurId.equals(mCurMethodId)) {
828 if (cs.curSession != null) {
829 // Fast case: if we are already connected to the input method,
830 // then just return it.
831 return attachNewInputLocked(initial, needResult);
832 }
833 if (mHaveConnection) {
834 if (mCurMethod != null) {
835 if (!cs.sessionRequested) {
836 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
839 MSG_CREATE_SESSION, mCurMethod,
840 new MethodCallback(mCurMethod)));
841 }
842 // Return to client, and we will get back with it when
843 // we have had a session made for it.
844 return new InputBindResult(null, mCurId, mCurSeq);
845 } else if (SystemClock.uptimeMillis()
846 < (mLastBindTime+TIME_TO_RECONNECT)) {
847 // In this case we have connected to the service, but
848 // don't yet have its interface. If it hasn't been too
849 // long since we did the connection, we'll return to
850 // the client and wait to get the service interface so
851 // we can report back. If it has been too long, we want
852 // to fall through so we can try a disconnect/reconnect
853 // to see if we can get back in touch with the service.
854 return new InputBindResult(null, mCurId, mCurSeq);
855 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800856 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
857 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859 }
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700862 return startInputInnerLocked();
863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 InputBindResult startInputInnerLocked() {
866 if (mCurMethodId == null) {
867 return mNoBinding;
868 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800869
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700870 if (!mSystemReady) {
871 // If the system is not yet ready, we shouldn't be running third
872 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700873 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700874 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 InputMethodInfo info = mMethodMap.get(mCurMethodId);
877 if (info == null) {
878 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
879 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800880
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700881 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
884 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700885 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
886 com.android.internal.R.string.input_method_binding_label);
887 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
888 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE)) {
890 mLastBindTime = SystemClock.uptimeMillis();
891 mHaveConnection = true;
892 mCurId = info.getId();
893 mCurToken = new Binder();
894 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800895 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 mIWindowManager.addWindowToken(mCurToken,
897 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
898 } catch (RemoteException e) {
899 }
900 return new InputBindResult(null, mCurId, mCurSeq);
901 } else {
902 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800903 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 + mCurIntent);
905 }
906 return null;
907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
satoke7c6998e2011-06-03 17:57:59 +0900909 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public InputBindResult startInput(IInputMethodClient client,
911 IInputContext inputContext, EditorInfo attribute,
912 boolean initial, boolean needResult) {
913 synchronized (mMethodMap) {
914 final long ident = Binder.clearCallingIdentity();
915 try {
916 return startInputLocked(client, inputContext, attribute,
917 initial, needResult);
918 } finally {
919 Binder.restoreCallingIdentity(ident);
920 }
921 }
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public void finishInput(IInputMethodClient client) {
926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927
satoke7c6998e2011-06-03 17:57:59 +0900928 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public void onServiceConnected(ComponentName name, IBinder service) {
930 synchronized (mMethodMap) {
931 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
932 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700933 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700935 unbindCurrentMethodLocked(false);
936 return;
937 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700939 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
940 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800942 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700943 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700945 MSG_CREATE_SESSION, mCurMethod,
946 new MethodCallback(mCurMethod)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 }
948 }
949 }
950 }
951
952 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
953 synchronized (mMethodMap) {
954 if (mCurMethod != null && method != null
955 && mCurMethod.asBinder() == method.asBinder()) {
956 if (mCurClient != null) {
957 mCurClient.curSession = new SessionState(mCurClient,
958 method, session);
959 mCurClient.sessionRequested = false;
960 InputBindResult res = attachNewInputLocked(true, true);
961 if (res.method != null) {
962 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
963 MSG_BIND_METHOD, mCurClient.client, res));
964 }
965 }
966 }
967 }
968 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700970 void unbindCurrentMethodLocked(boolean reportToClient) {
971 if (mHaveConnection) {
972 mContext.unbindService(this);
973 mHaveConnection = false;
974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700976 if (mCurToken != null) {
977 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800978 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700979 mIWindowManager.removeWindowToken(mCurToken);
980 } catch (RemoteException e) {
981 }
982 mCurToken = null;
983 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800984
The Android Open Source Project10592532009-03-18 17:39:46 -0700985 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700986 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700988 if (reportToClient && mCurClient != null) {
989 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
990 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
991 }
992 }
993
Devin Taylor0c33ed22010-02-23 13:26:46 -0600994 private void finishSession(SessionState sessionState) {
995 if (sessionState != null && sessionState.session != null) {
996 try {
997 sessionState.session.finishSession();
998 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -0700999 Slog.w(TAG, "Session failed to close due to remote exception", e);
satokdbf29502011-08-25 15:28:23 +09001000 mImeWindowVis = 0;
1001 updateImeWindowStatusLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001002 }
1003 }
1004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001006 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (mCurMethod != null) {
1008 for (ClientState cs : mClients.values()) {
1009 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001010 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 cs.curSession = null;
1012 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001013
1014 finishSession(mEnabledSession);
1015 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 mCurMethod = null;
1017 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001018 if (mStatusBar != null) {
1019 mStatusBar.setIconVisibility("ime", false);
1020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001022
satoke7c6998e2011-06-03 17:57:59 +09001023 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 public void onServiceDisconnected(ComponentName name) {
1025 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 + " mCurIntent=" + mCurIntent);
1028 if (mCurMethod != null && mCurIntent != null
1029 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001030 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 // We consider this to be a new bind attempt, since the system
1032 // should now try to restart the service for us.
1033 mLastBindTime = SystemClock.uptimeMillis();
1034 mShowRequested = mInputShown;
1035 mInputShown = false;
1036 if (mCurClient != null) {
1037 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1038 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1039 }
1040 }
1041 }
1042 }
1043
satokf9f01002011-05-19 21:31:50 +09001044 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001046 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 long ident = Binder.clearCallingIdentity();
1048 try {
1049 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001050 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 return;
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 synchronized (mMethodMap) {
1055 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001057 if (mStatusBar != null) {
1058 mStatusBar.setIconVisibility("ime", false);
1059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001061 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001062 CharSequence contentDescription = null;
1063 try {
1064 PackageManager packageManager = mContext.getPackageManager();
1065 contentDescription = packageManager.getApplicationLabel(
1066 packageManager.getApplicationInfo(packageName, 0));
1067 } catch (NameNotFoundException nnfe) {
1068 /* ignore */
1069 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001070 if (mStatusBar != null) {
1071 mStatusBar.setIcon("ime", packageName, iconId, 0,
1072 contentDescription != null
1073 ? contentDescription.toString() : null);
1074 mStatusBar.setIconVisibility("ime", true);
1075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 }
1078 } finally {
1079 Binder.restoreCallingIdentity(ident);
1080 }
1081 }
1082
satok7cfc0ed2011-06-20 21:29:36 +09001083 private boolean needsToShowImeSwitchOngoingNotification() {
1084 if (!mShowOngoingImeSwitcherForPhones) return false;
1085 synchronized (mMethodMap) {
1086 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1087 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001088 if (N > 2) return true;
1089 if (N < 1) return false;
1090 int nonAuxCount = 0;
1091 int auxCount = 0;
1092 InputMethodSubtype nonAuxSubtype = null;
1093 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001094 for(int i = 0; i < N; ++i) {
1095 final InputMethodInfo imi = imis.get(i);
1096 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1097 imi, true);
1098 final int subtypeCount = subtypes.size();
1099 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001100 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001101 } else {
1102 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001103 final InputMethodSubtype subtype = subtypes.get(j);
1104 if (!subtype.isAuxiliary()) {
1105 ++nonAuxCount;
1106 nonAuxSubtype = subtype;
1107 } else {
1108 ++auxCount;
1109 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001110 }
1111 }
1112 }
satok7cfc0ed2011-06-20 21:29:36 +09001113 }
satokb6359412011-06-28 17:47:41 +09001114 if (nonAuxCount > 1 || auxCount > 1) {
1115 return true;
1116 } else if (nonAuxCount == 1 && auxCount == 1) {
1117 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001118 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1119 || auxSubtype.overridesImplicitlyEnabledSubtype()
1120 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001121 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1122 return false;
1123 }
1124 return true;
1125 }
1126 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001127 }
satok7cfc0ed2011-06-20 21:29:36 +09001128 }
1129
satokdbf29502011-08-25 15:28:23 +09001130 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001131 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001132 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001133 int uid = Binder.getCallingUid();
1134 long ident = Binder.clearCallingIdentity();
1135 try {
1136 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001137 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001138 return;
1139 }
1140
1141 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001142 mImeWindowVis = vis;
1143 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001144 if (mStatusBar != null) {
1145 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1146 }
satok7cfc0ed2011-06-20 21:29:36 +09001147 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001148 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1149 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001150 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001151 final CharSequence title = mRes.getText(
1152 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001153 final CharSequence imiLabel = imi.loadLabel(pm);
1154 final CharSequence summary = mCurrentSubtype != null
1155 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1156 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1157 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001158 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001159 : imiLabel;
1160
satok7cfc0ed2011-06-20 21:29:36 +09001161 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001162 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001163 if (mNotificationManager != null) {
1164 mNotificationManager.notify(
1165 com.android.internal.R.string.select_input_method,
1166 mImeSwitcherNotification);
1167 mNotificationShown = true;
1168 }
satok7cfc0ed2011-06-20 21:29:36 +09001169 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001170 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001171 mNotificationManager.cancel(
1172 com.android.internal.R.string.select_input_method);
1173 mNotificationShown = false;
1174 }
1175 }
satok06487a52010-10-29 11:37:18 +09001176 }
1177 } finally {
1178 Binder.restoreCallingIdentity(ident);
1179 }
1180 }
1181
satoke7c6998e2011-06-03 17:57:59 +09001182 @Override
satokf9f01002011-05-19 21:31:50 +09001183 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1184 synchronized (mMethodMap) {
1185 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1186 for (int i = 0; i < spans.length; ++i) {
1187 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001188 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001189 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001190 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001191 }
1192 }
1193 }
1194 }
1195
satoke7c6998e2011-06-03 17:57:59 +09001196 @Override
satokf9f01002011-05-19 21:31:50 +09001197 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1198 synchronized (mMethodMap) {
1199 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1200 // TODO: Do not send the intent if the process of the targetImi is already dead.
1201 if (targetImi != null) {
1202 final String[] suggestions = span.getSuggestions();
1203 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001204 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001205 final Intent intent = new Intent();
1206 // Ensures that only a class in the original IME package will receive the
1207 // notification.
satok42c5a162011-05-26 16:46:14 +09001208 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001209 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1210 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1211 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1212 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1213 mContext.sendBroadcast(intent);
1214 return true;
1215 }
1216 }
1217 return false;
1218 }
1219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001221 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1222 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1223 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1224 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001226 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001227 // There is no input method selected, try to choose new applicable input method.
1228 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1229 id = Settings.Secure.getString(mContext.getContentResolver(),
1230 Settings.Secure.DEFAULT_INPUT_METHOD);
1231 }
1232 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 try {
satokab751aa2010-09-14 19:17:36 +09001234 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001236 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001237 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001238 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
satokf3db1af2010-11-23 13:34:33 +09001240 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001241 } else {
1242 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001243 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001244 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
satokab751aa2010-09-14 19:17:36 +09001248 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 InputMethodInfo info = mMethodMap.get(id);
1250 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001251 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001255 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001256 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1257 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001258 }
1259 if (subtype != mCurrentSubtype) {
1260 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001261 if (subtype != null) {
1262 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1263 }
satokcd7cd292010-11-20 15:46:23 +09001264 if (mCurMethod != null) {
1265 try {
satoke40dea02011-01-30 01:14:02 +09001266 final Configuration conf = mRes.getConfiguration();
1267 final boolean haveHardKeyboard = conf.keyboard
1268 != Configuration.KEYBOARD_NOKEYS;
1269 final boolean hardKeyShown = haveHardKeyboard
Ken Wakasa8710e762011-01-30 11:02:09 +09001270 && conf.hardKeyboardHidden
1271 != Configuration.HARDKEYBOARDHIDDEN_YES;
satoke40dea02011-01-30 01:14:02 +09001272 mImeWindowVis = (mInputShown || hardKeyShown) ? (
1273 InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE)
1274 : 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001275 updateImeWindowStatusLocked();
satokcd7cd292010-11-20 15:46:23 +09001276 // If subtype is null, try to find the most applicable one from
1277 // getCurrentInputMethodSubtype.
1278 if (subtype == null) {
1279 subtype = getCurrentInputMethodSubtype();
1280 }
1281 mCurMethod.changeInputMethodSubtype(subtype);
1282 } catch (RemoteException e) {
1283 return;
satokab751aa2010-09-14 19:17:36 +09001284 }
1285 }
1286 }
1287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 return;
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 final long ident = Binder.clearCallingIdentity();
1292 try {
satokab751aa2010-09-14 19:17:36 +09001293 // Set a subtype to this input method.
1294 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001295 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1296 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1297 // because mCurMethodId is stored as a history in
1298 // setSelectedInputMethodAndSubtypeLocked().
1299 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300
1301 if (ActivityManagerNative.isSystemReady()) {
1302 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001303 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 intent.putExtra("input_method_id", id);
1305 mContext.sendBroadcast(intent);
1306 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001307 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 } finally {
1309 Binder.restoreCallingIdentity(ident);
1310 }
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
satok42c5a162011-05-26 16:46:14 +09001313 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001314 public boolean showSoftInput(IInputMethodClient client, int flags,
1315 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001316 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 long ident = Binder.clearCallingIdentity();
1318 try {
1319 synchronized (mMethodMap) {
1320 if (mCurClient == null || client == null
1321 || mCurClient.client.asBinder() != client.asBinder()) {
1322 try {
1323 // We need to check if this is the current client with
1324 // focus in the window manager, to allow this call to
1325 // be made before input is started in it.
1326 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001327 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001328 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001331 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Joe Onorato8a9b2202010-02-26 18:56:32 -08001335 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001336 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 } finally {
1339 Binder.restoreCallingIdentity(ident);
1340 }
1341 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001342
The Android Open Source Project4df24232009-03-05 14:34:35 -08001343 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 mShowRequested = true;
1345 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1346 mShowExplicitlyRequested = true;
1347 }
1348 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1349 mShowExplicitlyRequested = true;
1350 mShowForced = true;
1351 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001352
Dianne Hackborncc278702009-09-02 23:07:23 -07001353 if (!mSystemReady) {
1354 return false;
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Project4df24232009-03-05 14:34:35 -08001357 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001359 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1360 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1361 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 mInputShown = true;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001365 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 // The client has asked to have the input method shown, but
1367 // we have been sitting here too long with a connection to the
1368 // service and no interface received, so let's disconnect/connect
1369 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001370 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001372 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 mContext.unbindService(this);
1374 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE);
1375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
The Android Open Source Project4df24232009-03-05 14:34:35 -08001377 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001379
satok42c5a162011-05-26 16:46:14 +09001380 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001381 public boolean hideSoftInput(IInputMethodClient client, int flags,
1382 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001383 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 long ident = Binder.clearCallingIdentity();
1385 try {
1386 synchronized (mMethodMap) {
1387 if (mCurClient == null || client == null
1388 || mCurClient.client.asBinder() != client.asBinder()) {
1389 try {
1390 // We need to check if this is the current client with
1391 // focus in the window manager, to allow this call to
1392 // be made before input is started in it.
1393 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001394 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1395 + uid + ": " + client);
Joe Onorato857fd9b2011-01-27 15:08:35 -08001396 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001397 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001398 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400 } catch (RemoteException e) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001401 mImeWindowVis = 0;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001402 updateImeWindowStatusLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001403 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406
Joe Onorato8a9b2202010-02-26 18:56:32 -08001407 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001408 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410 } finally {
1411 Binder.restoreCallingIdentity(ident);
1412 }
1413 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001414
The Android Open Source Project4df24232009-03-05 14:34:35 -08001415 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1417 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001418 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001420 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001423 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001425 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001427 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001429 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1430 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1431 res = true;
1432 } else {
1433 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 mInputShown = false;
1436 mShowRequested = false;
1437 mShowExplicitlyRequested = false;
1438 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001439 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
satok42c5a162011-05-26 16:46:14 +09001442 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001443 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1445 boolean first, int windowFlags) {
1446 long ident = Binder.clearCallingIdentity();
1447 try {
1448 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 + " viewHasFocus=" + viewHasFocus
1451 + " isTextEditor=" + isTextEditor
1452 + " softInputMode=#" + Integer.toHexString(softInputMode)
1453 + " first=" + first + " flags=#"
1454 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 if (mCurClient == null || client == null
1457 || mCurClient.client.asBinder() != client.asBinder()) {
1458 try {
1459 // We need to check if this is the current client with
1460 // focus in the window manager, to allow this call to
1461 // be made before input is started in it.
1462 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001463 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 return;
1465 }
1466 } catch (RemoteException e) {
1467 }
1468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001470 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001471 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001472 return;
1473 }
1474 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001475
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001476 // Should we auto-show the IME even if the caller has not
1477 // specified what should be done with it?
1478 // We only do this automatically if the window can resize
1479 // to accommodate the IME (so what the user sees will give
1480 // them good context without input information being obscured
1481 // by the IME) or if running on a large screen where there
1482 // is more room for the target window + IME.
1483 final boolean doAutoShow =
1484 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1485 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1486 || mRes.getConfiguration().isLayoutSizeAtLeast(
1487 Configuration.SCREENLAYOUT_SIZE_LARGE);
1488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1490 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001491 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1493 // There is no focus view, and this window will
1494 // be behind any soft input window, so hide the
1495 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001496 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001497 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001499 } else if (isTextEditor && doAutoShow && (softInputMode &
1500 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 // There is a focus view, and we are navigating forward
1502 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001503 // We only do this automatically if the window an resize
1504 // to accomodate the IME (so what the user sees will give
1505 // them good context without input information being obscured
1506 // by the IME) or if running on a large screen where there
1507 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001508 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001509 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
1511 break;
1512 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1513 // Do nothing.
1514 break;
1515 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1516 if ((softInputMode &
1517 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001518 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001519 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 }
1521 break;
1522 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001523 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001524 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 break;
1526 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1527 if ((softInputMode &
1528 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001529 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001530 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
1532 break;
1533 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001534 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001535 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 break;
1537 }
1538 }
1539 } finally {
1540 Binder.restoreCallingIdentity(ident);
1541 }
1542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
satok42c5a162011-05-26 16:46:14 +09001544 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1546 synchronized (mMethodMap) {
1547 if (mCurClient == null || client == null
1548 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001549 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001550 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 }
1552
satok440aab52010-11-25 09:43:11 +09001553 // Always call subtype picker, because subtype picker is a superset of input method
1554 // picker.
satokab751aa2010-09-14 19:17:36 +09001555 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1556 }
1557 }
1558
satok42c5a162011-05-26 16:46:14 +09001559 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001561 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1562 }
1563
satok42c5a162011-05-26 16:46:14 +09001564 @Override
satok28203512010-11-24 11:06:49 +09001565 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1566 synchronized (mMethodMap) {
1567 if (subtype != null) {
1568 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1569 mMethodMap.get(id), subtype.hashCode()));
1570 } else {
1571 setInputMethod(token, id);
1572 }
1573 }
satokab751aa2010-09-14 19:17:36 +09001574 }
1575
satok42c5a162011-05-26 16:46:14 +09001576 @Override
satokb416a712010-11-25 20:42:14 +09001577 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001578 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001579 synchronized (mMethodMap) {
1580 if (mCurClient == null || client == null
1581 || mCurClient.client.asBinder() != client.asBinder()) {
1582 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1583 }
satok7fee71f2010-12-17 18:54:26 +09001584 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1585 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001586 }
1587 }
1588
satok4fc87d62011-05-20 16:13:43 +09001589 @Override
satok735cf382010-11-11 20:40:09 +09001590 public boolean switchToLastInputMethod(IBinder token) {
1591 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001592 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001593 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001594 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001595 lastImi = mMethodMap.get(lastIme.first);
1596 } else {
1597 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001598 }
satok4fc87d62011-05-20 16:13:43 +09001599 String targetLastImiId = null;
1600 int subtypeId = NOT_A_SUBTYPE_ID;
1601 if (lastIme != null && lastImi != null) {
1602 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1603 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1604 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1605 : mCurrentSubtype.hashCode();
1606 // If the last IME is the same as the current IME and the last subtype is not
1607 // defined, there is no need to switch to the last IME.
1608 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1609 targetLastImiId = lastIme.first;
1610 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1611 }
1612 }
1613
1614 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1615 // This is a safety net. If the currentSubtype can't be added to the history
1616 // and the framework couldn't find the last ime, we will make the last ime be
1617 // the most applicable enabled keyboard subtype of the system imes.
1618 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1619 if (enabled != null) {
1620 final int N = enabled.size();
1621 final String locale = mCurrentSubtype == null
1622 ? mRes.getConfiguration().locale.toString()
1623 : mCurrentSubtype.getLocale();
1624 for (int i = 0; i < N; ++i) {
1625 final InputMethodInfo imi = enabled.get(i);
1626 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1627 InputMethodSubtype keyboardSubtype =
1628 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1629 SUBTYPE_MODE_KEYBOARD, locale, true);
1630 if (keyboardSubtype != null) {
1631 targetLastImiId = imi.getId();
1632 subtypeId = getSubtypeIdFromHashCode(
1633 imi, keyboardSubtype.hashCode());
1634 if(keyboardSubtype.getLocale().equals(locale)) {
1635 break;
1636 }
1637 }
1638 }
1639 }
1640 }
1641 }
1642
1643 if (!TextUtils.isEmpty(targetLastImiId)) {
1644 if (DEBUG) {
1645 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1646 + ", from: " + mCurMethodId + ", " + subtypeId);
1647 }
1648 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1649 return true;
1650 } else {
1651 return false;
1652 }
satok735cf382010-11-11 20:40:09 +09001653 }
1654 }
1655
satoke7c6998e2011-06-03 17:57:59 +09001656 @Override
satok68f1b782011-04-11 14:26:04 +09001657 public InputMethodSubtype getLastInputMethodSubtype() {
1658 synchronized (mMethodMap) {
1659 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1660 // TODO: Handle the case of the last IME with no subtypes
1661 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1662 || TextUtils.isEmpty(lastIme.second)) return null;
1663 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1664 if (lastImi == null) return null;
1665 try {
1666 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001667 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1668 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1669 return null;
1670 }
1671 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001672 } catch (NumberFormatException e) {
1673 return null;
1674 }
1675 }
1676 }
1677
satoke7c6998e2011-06-03 17:57:59 +09001678 @Override
satokee5e77c2011-09-02 18:50:15 +09001679 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001680 // By this IPC call, only a process which shares the same uid with the IME can add
1681 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001682 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001683 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001684 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001685 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001686 final PackageManager pm = mContext.getPackageManager();
1687 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1688 if (packageInfos != null) {
1689 final int packageNum = packageInfos.length;
1690 for (int i = 0; i < packageNum; ++i) {
1691 if (packageInfos[i].equals(imi.getPackageName())) {
1692 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001693 final long ident = Binder.clearCallingIdentity();
1694 try {
1695 buildInputMethodListLocked(mMethodList, mMethodMap);
1696 } finally {
1697 Binder.restoreCallingIdentity(ident);
1698 }
satokee5e77c2011-09-02 18:50:15 +09001699 return;
satok91e88122011-07-18 11:11:42 +09001700 }
1701 }
1702 }
satoke7c6998e2011-06-03 17:57:59 +09001703 }
satokee5e77c2011-09-02 18:50:15 +09001704 return;
satoke7c6998e2011-06-03 17:57:59 +09001705 }
1706
satok28203512010-11-24 11:06:49 +09001707 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 synchronized (mMethodMap) {
1709 if (token == null) {
1710 if (mContext.checkCallingOrSelfPermission(
1711 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1712 != PackageManager.PERMISSION_GRANTED) {
1713 throw new SecurityException(
1714 "Using null token requires permission "
1715 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1716 }
1717 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001718 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1719 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 return;
1721 }
1722
satokc5933802011-08-31 21:26:04 +09001723 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 try {
satokab751aa2010-09-14 19:17:36 +09001725 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 } finally {
1727 Binder.restoreCallingIdentity(ident);
1728 }
1729 }
1730 }
1731
satok42c5a162011-05-26 16:46:14 +09001732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 public void hideMySoftInput(IBinder token, int flags) {
1734 synchronized (mMethodMap) {
1735 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001736 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1737 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 return;
1739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 long ident = Binder.clearCallingIdentity();
1741 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 hideCurrentInputLocked(flags, null);
1743 } finally {
1744 Binder.restoreCallingIdentity(ident);
1745 }
1746 }
1747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001748
satok42c5a162011-05-26 16:46:14 +09001749 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001750 public void showMySoftInput(IBinder token, int flags) {
1751 synchronized (mMethodMap) {
1752 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001753 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1754 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 return;
1756 }
1757 long ident = Binder.clearCallingIdentity();
1758 try {
1759 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 } finally {
1761 Binder.restoreCallingIdentity(ident);
1762 }
1763 }
1764 }
1765
1766 void setEnabledSessionInMainThread(SessionState session) {
1767 if (mEnabledSession != session) {
1768 if (mEnabledSession != null) {
1769 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001770 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 mEnabledSession.method.setSessionEnabled(
1772 mEnabledSession.session, false);
1773 } catch (RemoteException e) {
1774 }
1775 }
1776 mEnabledSession = session;
1777 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001778 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 session.method.setSessionEnabled(
1780 session.session, true);
1781 } catch (RemoteException e) {
1782 }
1783 }
1784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785
satok42c5a162011-05-26 16:46:14 +09001786 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 public boolean handleMessage(Message msg) {
1788 HandlerCaller.SomeArgs args;
1789 switch (msg.what) {
1790 case MSG_SHOW_IM_PICKER:
1791 showInputMethodMenu();
1792 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
satokab751aa2010-09-14 19:17:36 +09001794 case MSG_SHOW_IM_SUBTYPE_PICKER:
1795 showInputMethodSubtypeMenu();
1796 return true;
1797
satok47a44912010-10-06 16:03:58 +09001798 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001799 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001800 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001801 return true;
1802
1803 case MSG_SHOW_IM_CONFIG:
1804 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001805 return true;
1806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 case MSG_UNBIND_INPUT:
1810 try {
1811 ((IInputMethod)msg.obj).unbindInput();
1812 } catch (RemoteException e) {
1813 // There is nothing interesting about the method dying.
1814 }
1815 return true;
1816 case MSG_BIND_INPUT:
1817 args = (HandlerCaller.SomeArgs)msg.obj;
1818 try {
1819 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1820 } catch (RemoteException e) {
1821 }
1822 return true;
1823 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001824 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001826 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1827 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 } catch (RemoteException e) {
1829 }
1830 return true;
1831 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001832 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001834 ((IInputMethod)args.arg1).hideSoftInput(0,
1835 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 } catch (RemoteException e) {
1837 }
1838 return true;
1839 case MSG_ATTACH_TOKEN:
1840 args = (HandlerCaller.SomeArgs)msg.obj;
1841 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001842 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1844 } catch (RemoteException e) {
1845 }
1846 return true;
1847 case MSG_CREATE_SESSION:
1848 args = (HandlerCaller.SomeArgs)msg.obj;
1849 try {
1850 ((IInputMethod)args.arg1).createSession(
1851 (IInputMethodCallback)args.arg2);
1852 } catch (RemoteException e) {
1853 }
1854 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 case MSG_START_INPUT:
1858 args = (HandlerCaller.SomeArgs)msg.obj;
1859 try {
1860 SessionState session = (SessionState)args.arg1;
1861 setEnabledSessionInMainThread(session);
1862 session.method.startInput((IInputContext)args.arg2,
1863 (EditorInfo)args.arg3);
1864 } catch (RemoteException e) {
1865 }
1866 return true;
1867 case MSG_RESTART_INPUT:
1868 args = (HandlerCaller.SomeArgs)msg.obj;
1869 try {
1870 SessionState session = (SessionState)args.arg1;
1871 setEnabledSessionInMainThread(session);
1872 session.method.restartInput((IInputContext)args.arg2,
1873 (EditorInfo)args.arg3);
1874 } catch (RemoteException e) {
1875 }
1876 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 case MSG_UNBIND_METHOD:
1881 try {
1882 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1883 } catch (RemoteException e) {
1884 // There is nothing interesting about the last client dying.
1885 }
1886 return true;
1887 case MSG_BIND_METHOD:
1888 args = (HandlerCaller.SomeArgs)msg.obj;
1889 try {
1890 ((IInputMethodClient)args.arg1).onBindMethod(
1891 (InputBindResult)args.arg2);
1892 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001893 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 }
1895 return true;
1896 }
1897 return false;
1898 }
1899
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001900 private boolean isSystemIme(InputMethodInfo inputMethod) {
1901 return (inputMethod.getServiceInfo().applicationInfo.flags
1902 & ApplicationInfo.FLAG_SYSTEM) != 0;
1903 }
1904
Ken Wakasa586f0512011-01-20 22:31:01 +09001905 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1906 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1907 final int subtypeCount = imi.getSubtypeCount();
1908 for (int i = 0; i < subtypeCount; ++i) {
1909 subtypes.add(imi.getSubtypeAt(i));
1910 }
1911 return subtypes;
1912 }
1913
satoka86f5e42011-09-02 17:12:42 +09001914
1915 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1916 InputMethodInfo imi, String mode) {
1917 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1918 final int subtypeCount = imi.getSubtypeCount();
1919 for (int i = 0; i < subtypeCount; ++i) {
1920 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1921 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1922 subtypes.add(subtype);
1923 }
1924 }
1925 return subtypes;
1926 }
1927
satokdc9ddae2011-10-06 12:22:36 +09001928 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001929 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001930 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001931 // We'd prefer to fall back on a system IME, since that is safer.
1932 int i=enabled.size();
1933 while (i > 0) {
1934 i--;
satokdc9ddae2011-10-06 12:22:36 +09001935 final InputMethodInfo imi = enabled.get(i);
1936 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001937 break;
1938 }
1939 }
satokdc9ddae2011-10-06 12:22:36 +09001940 return enabled.get(i);
1941 }
1942 return null;
1943 }
1944
1945 private boolean chooseNewDefaultIMELocked() {
1946 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1947 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001948 if (DEBUG) {
1949 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1950 }
satok723a27e2010-11-11 14:58:11 +09001951 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001952 return true;
1953 }
1954
1955 return false;
1956 }
1957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1959 HashMap<String, InputMethodInfo> map) {
1960 list.clear();
1961 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001964 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07001965 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
1966 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
1967 Secure.DISABLED_SYSTEM_INPUT_METHODS);
1968 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969
1970 List<ResolveInfo> services = pm.queryIntentServices(
1971 new Intent(InputMethod.SERVICE_INTERFACE),
1972 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001973
satoke7c6998e2011-06-03 17:57:59 +09001974 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
1975 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 for (int i = 0; i < services.size(); ++i) {
1977 ResolveInfo ri = services.get(i);
1978 ServiceInfo si = ri.serviceInfo;
1979 ComponentName compName = new ComponentName(si.packageName, si.name);
1980 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
1981 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001982 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 + ": it does not require the permission "
1984 + android.Manifest.permission.BIND_INPUT_METHOD);
1985 continue;
1986 }
1987
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989
1990 try {
satoke7c6998e2011-06-03 17:57:59 +09001991 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07001993 final String id = p.getId();
1994 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995
Amith Yamasanie861ec12010-03-24 21:39:27 -07001996 // System IMEs are enabled by default, unless there's a hard keyboard
1997 // and the system IME was explicitly disabled
1998 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
1999 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002000 }
2001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002003 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002007 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002009 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 }
2011 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002012
2013 String defaultIme = Settings.Secure.getString(mContext
2014 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002015 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002016 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002017 updateFromSettingsLocked();
2018 }
2019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002023
satokab751aa2010-09-14 19:17:36 +09002024 private void showInputMethodMenu() {
2025 showInputMethodMenuInternal(false);
2026 }
2027
2028 private void showInputMethodSubtypeMenu() {
2029 showInputMethodMenuInternal(true);
2030 }
2031
satok217f5482010-12-15 05:19:19 +09002032 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002033 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002034 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002035 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2036 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002037 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002038 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002039 }
satok217f5482010-12-15 05:19:19 +09002040 mContext.startActivity(intent);
2041 }
2042
2043 private void showConfigureInputMethods() {
2044 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2045 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2046 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2047 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002048 mContext.startActivity(intent);
2049 }
2050
satokab751aa2010-09-14 19:17:36 +09002051 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002052 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002056 final boolean isScreenLocked = mKeyguardManager != null
2057 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 String lastInputMethodId = Settings.Secure.getString(context
2060 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002061 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002062 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002063
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002064 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002065 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2066 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002067 if (immis == null || immis.size() == 0) {
2068 return;
2069 }
2070
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002071 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072
Ken Wakasa761eb372011-03-04 19:06:18 +09002073 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2074 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2075 new Comparator<InputMethodInfo>() {
2076 @Override
2077 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2078 if (imi2 == null) return 0;
2079 if (imi1 == null) return 1;
2080 if (pm == null) {
2081 return imi1.getId().compareTo(imi2.getId());
2082 }
2083 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2084 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2085 return imiId1.toString().compareTo(imiId2.toString());
2086 }
2087 });
satok913a8922010-08-26 21:53:41 +09002088
Ken Wakasa761eb372011-03-04 19:06:18 +09002089 sortedImmis.putAll(immis);
2090
Ken Wakasa05dbb652011-08-22 15:22:43 +09002091 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002092
2093 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002094 if (imi == null) continue;
2095 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002096 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002097 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2098 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002099 }
satokbb4aa062011-01-19 21:40:27 +09002100 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002101 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002102 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002103 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002104 if (DEBUG) {
2105 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2106 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002107 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002108 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2109 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2110 // We show all enabled IMEs and subtypes when an IME is shown.
2111 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002112 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002113 final CharSequence subtypeLabel =
2114 subtype.overridesImplicitlyEnabledSubtype() ? null
2115 : subtype.getDisplayName(context, imi.getPackageName(),
2116 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002117 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2118
satok4a28bde2011-06-29 21:03:40 +09002119 // Removing this subtype from enabledSubtypeSet because we no longer
2120 // need to add an entry of this subtype to imList to avoid duplicated
2121 // entries.
2122 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002123 }
satokab751aa2010-09-14 19:17:36 +09002124 }
2125 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002126 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002127 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002128 }
satok913a8922010-08-26 21:53:41 +09002129
Ken Wakasa761eb372011-03-04 19:06:18 +09002130 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002131 mIms = new InputMethodInfo[N];
2132 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002133 int checkedItem = 0;
2134 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002135 final ImeSubtypeListItem item = imList.get(i);
2136 mIms[i] = item.mImi;
2137 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002138 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002139 int subtypeId = mSubtypeIds[i];
2140 if ((subtypeId == NOT_A_SUBTYPE_ID)
2141 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2142 || (subtypeId == lastInputMethodSubtypeId)) {
2143 checkedItem = i;
2144 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
satokab751aa2010-09-14 19:17:36 +09002147
Ken Wakasa05dbb652011-08-22 15:22:43 +09002148 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002149 com.android.internal.R.styleable.DialogPreference,
2150 com.android.internal.R.attr.alertDialogStyle, 0);
2151 mDialogBuilder = new AlertDialog.Builder(context)
2152 .setTitle(com.android.internal.R.string.select_input_method)
2153 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002154 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002155 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002157 }
2158 })
2159 .setIcon(a.getDrawable(
2160 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2161 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002162
Ken Wakasa05dbb652011-08-22 15:22:43 +09002163 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2164 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2165 checkedItem);
2166
2167 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002168 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002169 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002170 public void onClick(DialogInterface dialog, int which) {
2171 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002172 if (mIms == null || mIms.length <= which
2173 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002174 return;
2175 }
2176 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002177 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002178 hideInputMethodMenu();
2179 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002180 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002181 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002182 subtypeId = NOT_A_SUBTYPE_ID;
2183 }
2184 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002185 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002188 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189
satokbc81b692011-08-26 16:22:22 +09002190 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002191 mDialogBuilder.setPositiveButton(
2192 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002193 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002194 @Override
satok7f35c8c2010-10-07 21:13:11 +09002195 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002196 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002197 }
2198 });
2199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002201 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 mSwitchingDialog.getWindow().setType(
2203 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002204 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 mSwitchingDialog.show();
2206 }
2207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002208
Ken Wakasa05dbb652011-08-22 15:22:43 +09002209 private static class ImeSubtypeListItem {
2210 public final CharSequence mImeName;
2211 public final CharSequence mSubtypeName;
2212 public final InputMethodInfo mImi;
2213 public final int mSubtypeId;
2214 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2215 InputMethodInfo imi, int subtypeId) {
2216 mImeName = imeName;
2217 mSubtypeName = subtypeName;
2218 mImi = imi;
2219 mSubtypeId = subtypeId;
2220 }
2221 }
2222
2223 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2224 private final LayoutInflater mInflater;
2225 private final int mTextViewResourceId;
2226 private final List<ImeSubtypeListItem> mItemsList;
2227 private final int mCheckedItem;
2228 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2229 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2230 super(context, textViewResourceId, itemsList);
2231 mTextViewResourceId = textViewResourceId;
2232 mItemsList = itemsList;
2233 mCheckedItem = checkedItem;
2234 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2235 }
2236
2237 @Override
2238 public View getView(int position, View convertView, ViewGroup parent) {
2239 final View view = convertView != null ? convertView
2240 : mInflater.inflate(mTextViewResourceId, null);
2241 if (position < 0 || position >= mItemsList.size()) return view;
2242 final ImeSubtypeListItem item = mItemsList.get(position);
2243 final CharSequence imeName = item.mImeName;
2244 final CharSequence subtypeName = item.mSubtypeName;
2245 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2246 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2247 if (TextUtils.isEmpty(subtypeName)) {
2248 firstTextView.setText(imeName);
2249 secondTextView.setVisibility(View.GONE);
2250 } else {
2251 firstTextView.setText(subtypeName);
2252 secondTextView.setText(imeName);
2253 secondTextView.setVisibility(View.VISIBLE);
2254 }
2255 final RadioButton radioButton =
2256 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2257 radioButton.setChecked(position == mCheckedItem);
2258 return view;
2259 }
2260 }
2261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002263 synchronized (mMethodMap) {
2264 hideInputMethodMenuLocked();
2265 }
2266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
The Android Open Source Project10592532009-03-18 17:39:46 -07002268 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002269 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270
The Android Open Source Project10592532009-03-18 17:39:46 -07002271 if (mSwitchingDialog != null) {
2272 mSwitchingDialog.dismiss();
2273 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002275
The Android Open Source Project10592532009-03-18 17:39:46 -07002276 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002277 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002281
satok42c5a162011-05-26 16:46:14 +09002282 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 public boolean setInputMethodEnabled(String id, boolean enabled) {
2284 synchronized (mMethodMap) {
2285 if (mContext.checkCallingOrSelfPermission(
2286 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2287 != PackageManager.PERMISSION_GRANTED) {
2288 throw new SecurityException(
2289 "Requires permission "
2290 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2291 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 long ident = Binder.clearCallingIdentity();
2294 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002295 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 } finally {
2297 Binder.restoreCallingIdentity(ident);
2298 }
2299 }
2300 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002301
2302 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2303 // Make sure this is a valid input method.
2304 InputMethodInfo imm = mMethodMap.get(id);
2305 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002306 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002307 }
2308
satokd87c2592010-09-29 11:52:06 +09002309 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2310 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002311
satokd87c2592010-09-29 11:52:06 +09002312 if (enabled) {
2313 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2314 if (pair.first.equals(id)) {
2315 // We are enabling this input method, but it is already enabled.
2316 // Nothing to do. The previous state was enabled.
2317 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002318 }
2319 }
satokd87c2592010-09-29 11:52:06 +09002320 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2321 // Previous state was disabled.
2322 return false;
2323 } else {
2324 StringBuilder builder = new StringBuilder();
2325 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2326 builder, enabledInputMethodsList, id)) {
2327 // Disabled input method is currently selected, switch to another one.
2328 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2329 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002330 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2331 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2332 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002333 }
2334 // Previous state was enabled.
2335 return true;
2336 } else {
2337 // We are disabling the input method but it is already disabled.
2338 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002339 return false;
2340 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002341 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002342 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002343
satok57ffc002011-01-25 00:11:47 +09002344 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2345 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002346 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002347 }
2348
satok723a27e2010-11-11 14:58:11 +09002349 private void saveCurrentInputMethodAndSubtypeToHistory() {
2350 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2351 if (mCurrentSubtype != null) {
2352 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2353 }
satok57ffc002011-01-25 00:11:47 +09002354 if (canAddToLastInputMethod(mCurrentSubtype)) {
2355 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2356 }
satokab751aa2010-09-14 19:17:36 +09002357 }
2358
satok723a27e2010-11-11 14:58:11 +09002359 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2360 boolean setSubtypeOnly) {
2361 // Update the history of InputMethod and Subtype
2362 saveCurrentInputMethodAndSubtypeToHistory();
2363
2364 // Set Subtype here
2365 if (imi == null || subtypeId < 0) {
2366 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002367 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002368 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002369 if (subtypeId < imi.getSubtypeCount()) {
2370 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2371 mSettings.putSelectedSubtype(subtype.hashCode());
2372 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002373 } else {
2374 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2375 mCurrentSubtype = null;
2376 }
satokab751aa2010-09-14 19:17:36 +09002377 }
satok723a27e2010-11-11 14:58:11 +09002378
2379 if (!setSubtypeOnly) {
2380 // Set InputMethod here
2381 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2382 }
2383 }
2384
2385 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2386 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2387 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2388 // newDefaultIme is empty when there is no candidate for the selected IME.
2389 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2390 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2391 if (subtypeHashCode != null) {
2392 try {
2393 lastSubtypeId = getSubtypeIdFromHashCode(
2394 imi, Integer.valueOf(subtypeHashCode));
2395 } catch (NumberFormatException e) {
2396 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2397 }
2398 }
2399 }
2400 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002401 }
2402
2403 private int getSelectedInputMethodSubtypeId(String id) {
2404 InputMethodInfo imi = mMethodMap.get(id);
2405 if (imi == null) {
2406 return NOT_A_SUBTYPE_ID;
2407 }
satokab751aa2010-09-14 19:17:36 +09002408 int subtypeId;
2409 try {
2410 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2411 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2412 } catch (SettingNotFoundException e) {
2413 return NOT_A_SUBTYPE_ID;
2414 }
satok723a27e2010-11-11 14:58:11 +09002415 return getSubtypeIdFromHashCode(imi, subtypeId);
2416 }
2417
2418 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002419 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002420 final int subtypeCount = imi.getSubtypeCount();
2421 for (int i = 0; i < subtypeCount; ++i) {
2422 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002423 if (subtypeHashCode == ims.hashCode()) {
2424 return i;
2425 }
satokab751aa2010-09-14 19:17:36 +09002426 }
2427 }
2428 return NOT_A_SUBTYPE_ID;
2429 }
2430
satoka86f5e42011-09-02 17:12:42 +09002431 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2432 Resources res, InputMethodInfo imi) {
2433 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002434 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002435 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002436 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002437 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002438 final int N = subtypes.size();
2439 boolean containsKeyboardSubtype = false;
2440 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002441 // scan overriding implicitly enabled subtypes.
2442 InputMethodSubtype subtype = subtypes.get(i);
2443 if (subtype.overridesImplicitlyEnabledSubtype()) {
2444 final String mode = subtype.getMode();
2445 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2446 applicableModeAndSubtypesMap.put(mode, subtype);
2447 }
2448 }
2449 }
2450 if (applicableModeAndSubtypesMap.size() > 0) {
2451 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2452 }
2453 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002454 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002455 final String locale = subtype.getLocale();
2456 final String mode = subtype.getMode();
2457 // When system locale starts with subtype's locale, that subtype will be applicable
2458 // for system locale
2459 // For instance, it's clearly applicable for cases like system locale = en_US and
2460 // subtype = en, but it is not necessarily considered applicable for cases like system
2461 // locale = en and subtype = en_US.
2462 // We just call systemLocale.startsWith(locale) in this function because there is no
2463 // need to find applicable subtypes aggressively unlike
2464 // findLastResortApplicableSubtypeLocked.
2465 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002466 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002467 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002468 if (applicableSubtype != null) {
2469 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2470 if (!systemLocale.equals(locale)) continue;
2471 }
satok3da92232011-01-11 22:46:30 +09002472 applicableModeAndSubtypesMap.put(mode, subtype);
satok16331c82010-12-20 23:48:46 +09002473 if (!containsKeyboardSubtype
2474 && SUBTYPE_MODE_KEYBOARD.equalsIgnoreCase(subtype.getMode())) {
2475 containsKeyboardSubtype = true;
2476 }
2477 }
2478 }
satok4a28bde2011-06-29 21:03:40 +09002479 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
satok3da92232011-01-11 22:46:30 +09002480 applicableModeAndSubtypesMap.values());
satok16331c82010-12-20 23:48:46 +09002481 if (!containsKeyboardSubtype) {
2482 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002483 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002484 if (lastResortKeyboardSubtype != null) {
2485 applicableSubtypes.add(lastResortKeyboardSubtype);
2486 }
2487 }
2488 return applicableSubtypes;
2489 }
2490
satok4e4569d2010-11-19 18:45:53 +09002491 /**
2492 * If there are no selected subtypes, tries finding the most applicable one according to the
2493 * given locale.
2494 * @param subtypes this function will search the most applicable subtype in subtypes
2495 * @param mode subtypes will be filtered by mode
2496 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002497 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2498 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002499 * @return the most applicable subtypeId
2500 */
satokdf31ae62011-01-15 06:19:44 +09002501 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2502 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002503 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002504 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002505 return null;
satok8fbb1e82010-11-02 23:15:58 +09002506 }
satok4e4569d2010-11-19 18:45:53 +09002507 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002508 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002509 }
satok8fbb1e82010-11-02 23:15:58 +09002510 final String language = locale.substring(0, 2);
2511 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002512 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002513 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002514 final int N = subtypes.size();
2515 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002516 InputMethodSubtype subtype = subtypes.get(i);
2517 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002518 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2519 // and all subtypes with all modes can be candidates.
2520 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002521 if (firstMatchedModeSubtype == null) {
2522 firstMatchedModeSubtype = subtype;
2523 }
satok9ef02832010-11-04 21:17:48 +09002524 if (locale.equals(subtypeLocale)) {
2525 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002526 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002527 break;
2528 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2529 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002530 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002531 partialMatchFound = true;
2532 }
satok8fbb1e82010-11-02 23:15:58 +09002533 }
2534 }
2535
satok7599a7f2010-12-22 13:45:23 +09002536 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2537 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002538 }
2539
satok8fbb1e82010-11-02 23:15:58 +09002540 // The first subtype applicable to the system locale will be defined as the most applicable
2541 // subtype.
2542 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002543 if (applicableSubtype != null) {
2544 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2545 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2546 }
satok8fbb1e82010-11-02 23:15:58 +09002547 }
satokcd7cd292010-11-20 15:46:23 +09002548 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002549 }
2550
satok4e4569d2010-11-19 18:45:53 +09002551 // If there are no selected shortcuts, tries finding the most applicable ones.
2552 private Pair<InputMethodInfo, InputMethodSubtype>
2553 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2554 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2555 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002556 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002557 boolean foundInSystemIME = false;
2558
2559 // Search applicable subtype for each InputMethodInfo
2560 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002561 final String imiId = imi.getId();
2562 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2563 continue;
2564 }
satokcd7cd292010-11-20 15:46:23 +09002565 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002566 final List<InputMethodSubtype> enabledSubtypes =
2567 getEnabledInputMethodSubtypeList(imi, true);
2568 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002569 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002570 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002571 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002572 }
satokdf31ae62011-01-15 06:19:44 +09002573 // 2. Search by the system locale from enabledSubtypes.
2574 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002575 if (subtype == null) {
2576 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002577 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002578 }
satoka86f5e42011-09-02 17:12:42 +09002579 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2580 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2581 final ArrayList<InputMethodSubtype> subtypesForSearch =
2582 overridingImplicitlyEnabledSubtypes.isEmpty()
2583 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002584 // 4. Search by the current subtype's locale from all subtypes.
2585 if (subtype == null && mCurrentSubtype != null) {
2586 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002587 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002588 }
2589 // 5. Search by the system locale from all subtypes.
2590 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002591 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002592 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002593 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002594 }
satokcd7cd292010-11-20 15:46:23 +09002595 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002596 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002597 // The current input method is the most applicable IME.
2598 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002599 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002600 break;
satok7599a7f2010-12-22 13:45:23 +09002601 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002602 // The system input method is 2nd applicable IME.
2603 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002604 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002605 if ((imi.getServiceInfo().applicationInfo.flags
2606 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2607 foundInSystemIME = true;
2608 }
satok4e4569d2010-11-19 18:45:53 +09002609 }
2610 }
2611 }
2612 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002613 if (mostApplicableIMI != null) {
2614 Slog.w(TAG, "Most applicable shortcut input method was:"
2615 + mostApplicableIMI.getId());
2616 if (mostApplicableSubtype != null) {
2617 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2618 + "," + mostApplicableSubtype.getMode() + ","
2619 + mostApplicableSubtype.getLocale());
2620 }
2621 }
satok4e4569d2010-11-19 18:45:53 +09002622 }
satokcd7cd292010-11-20 15:46:23 +09002623 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002624 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002625 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002626 } else {
2627 return null;
2628 }
2629 }
2630
satokab751aa2010-09-14 19:17:36 +09002631 /**
2632 * @return Return the current subtype of this input method.
2633 */
satok42c5a162011-05-26 16:46:14 +09002634 @Override
satokab751aa2010-09-14 19:17:36 +09002635 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002636 boolean subtypeIsSelected = false;
2637 try {
2638 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2639 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2640 } catch (SettingNotFoundException e) {
2641 }
satok3ef8b292010-11-23 06:06:29 +09002642 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002643 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002644 String lastInputMethodId = Settings.Secure.getString(
2645 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002646 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2647 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002648 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2649 if (imi != null) {
2650 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002651 // the most applicable subtype from explicitly or implicitly enabled
2652 // subtypes.
2653 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2654 getEnabledInputMethodSubtypeList(imi, true);
2655 // If there is only one explicitly or implicitly enabled subtype,
2656 // just returns it.
2657 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2658 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2659 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2660 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2661 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2662 SUBTYPE_MODE_KEYBOARD, null, true);
2663 if (mCurrentSubtype == null) {
2664 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2665 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2666 true);
2667 }
2668 }
satok4e4569d2010-11-19 18:45:53 +09002669 }
satokcd7cd292010-11-20 15:46:23 +09002670 } else {
satok3ef8b292010-11-23 06:06:29 +09002671 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002672 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002673 }
satok8fbb1e82010-11-02 23:15:58 +09002674 }
satok3ef8b292010-11-23 06:06:29 +09002675 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002676 }
satokab751aa2010-09-14 19:17:36 +09002677 }
2678
satokf3db1af2010-11-23 13:34:33 +09002679 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2680 InputMethodSubtype subtype) {
2681 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2682 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2683 } else {
2684 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2685 subtypes.add(subtype);
2686 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2687 }
2688 }
2689
satok4e4569d2010-11-19 18:45:53 +09002690 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002691 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002692 @Override
satok4e4569d2010-11-19 18:45:53 +09002693 public List getShortcutInputMethodsAndSubtypes() {
2694 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002695 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002696 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002697 // If there are no selected shortcut subtypes, the framework will try to find
2698 // the most applicable subtype from all subtypes whose mode is
2699 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002700 Pair<InputMethodInfo, InputMethodSubtype> info =
2701 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2702 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002703 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002704 ret.add(info.first);
2705 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002706 }
satok3da92232011-01-11 22:46:30 +09002707 return ret;
satokf3db1af2010-11-23 13:34:33 +09002708 }
satokf3db1af2010-11-23 13:34:33 +09002709 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2710 ret.add(imi);
2711 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2712 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002713 }
2714 }
satokf3db1af2010-11-23 13:34:33 +09002715 return ret;
satok4e4569d2010-11-19 18:45:53 +09002716 }
2717 }
2718
satok42c5a162011-05-26 16:46:14 +09002719 @Override
satokb66d2872010-11-10 01:04:04 +09002720 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2721 synchronized (mMethodMap) {
2722 if (subtype != null && mCurMethodId != null) {
2723 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2724 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2725 if (subtypeId != NOT_A_SUBTYPE_ID) {
2726 setInputMethodLocked(mCurMethodId, subtypeId);
2727 return true;
2728 }
2729 }
2730 return false;
2731 }
2732 }
2733
satokd87c2592010-09-29 11:52:06 +09002734 /**
2735 * Utility class for putting and getting settings for InputMethod
2736 * TODO: Move all putters and getters of settings to this class.
2737 */
2738 private static class InputMethodSettings {
2739 // The string for enabled input method is saved as follows:
2740 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2741 private static final char INPUT_METHOD_SEPARATER = ':';
2742 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002743 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002744 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2745
satok723a27e2010-11-11 14:58:11 +09002746 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002747 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2748
satokdf31ae62011-01-15 06:19:44 +09002749 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002750 private final ContentResolver mResolver;
2751 private final HashMap<String, InputMethodInfo> mMethodMap;
2752 private final ArrayList<InputMethodInfo> mMethodList;
2753
2754 private String mEnabledInputMethodsStrCache;
2755
2756 private static void buildEnabledInputMethodsSettingString(
2757 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2758 String id = pair.first;
2759 ArrayList<String> subtypes = pair.second;
2760 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002761 // Inputmethod and subtypes are saved in the settings as follows:
2762 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2763 for (String subtypeId: subtypes) {
2764 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002765 }
2766 }
2767
2768 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002769 Resources res, ContentResolver resolver,
2770 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2771 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002772 mResolver = resolver;
2773 mMethodMap = methodMap;
2774 mMethodList = methodList;
2775 }
2776
2777 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2778 return createEnabledInputMethodListLocked(
2779 getEnabledInputMethodsAndSubtypeListLocked());
2780 }
2781
satok7f35c8c2010-10-07 21:13:11 +09002782 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002783 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2784 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002785 getEnabledInputMethodsAndSubtypeListLocked());
2786 }
2787
satok67ddf9c2010-11-17 09:45:54 +09002788 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2789 InputMethodInfo imi) {
2790 List<Pair<String, ArrayList<String>>> imsList =
2791 getEnabledInputMethodsAndSubtypeListLocked();
2792 ArrayList<InputMethodSubtype> enabledSubtypes =
2793 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002794 if (imi != null) {
2795 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2796 InputMethodInfo info = mMethodMap.get(imsPair.first);
2797 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002798 final int subtypeCount = info.getSubtypeCount();
2799 for (int i = 0; i < subtypeCount; ++i) {
2800 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002801 for (String s: imsPair.second) {
2802 if (String.valueOf(ims.hashCode()).equals(s)) {
2803 enabledSubtypes.add(ims);
2804 }
satok67ddf9c2010-11-17 09:45:54 +09002805 }
2806 }
satok884ef9a2010-11-18 10:39:46 +09002807 break;
satok67ddf9c2010-11-17 09:45:54 +09002808 }
satok67ddf9c2010-11-17 09:45:54 +09002809 }
2810 }
2811 return enabledSubtypes;
2812 }
2813
satokd87c2592010-09-29 11:52:06 +09002814 // At the initial boot, the settings for input methods are not set,
2815 // so we need to enable IME in that case.
2816 public void enableAllIMEsIfThereIsNoEnabledIME() {
2817 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2818 StringBuilder sb = new StringBuilder();
2819 final int N = mMethodList.size();
2820 for (int i = 0; i < N; i++) {
2821 InputMethodInfo imi = mMethodList.get(i);
2822 Slog.i(TAG, "Adding: " + imi.getId());
2823 if (i > 0) sb.append(':');
2824 sb.append(imi.getId());
2825 }
2826 putEnabledInputMethodsStr(sb.toString());
2827 }
2828 }
2829
satokbb4aa062011-01-19 21:40:27 +09002830 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002831 ArrayList<Pair<String, ArrayList<String>>> imsList
2832 = new ArrayList<Pair<String, ArrayList<String>>>();
2833 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2834 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2835 return imsList;
2836 }
satok723a27e2010-11-11 14:58:11 +09002837 mInputMethodSplitter.setString(enabledInputMethodsStr);
2838 while (mInputMethodSplitter.hasNext()) {
2839 String nextImsStr = mInputMethodSplitter.next();
2840 mSubtypeSplitter.setString(nextImsStr);
2841 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002842 ArrayList<String> subtypeHashes = new ArrayList<String>();
2843 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002844 String imeId = mSubtypeSplitter.next();
2845 while (mSubtypeSplitter.hasNext()) {
2846 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002847 }
2848 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2849 }
2850 }
2851 return imsList;
2852 }
2853
2854 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2855 if (reloadInputMethodStr) {
2856 getEnabledInputMethodsStr();
2857 }
2858 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2859 // Add in the newly enabled input method.
2860 putEnabledInputMethodsStr(id);
2861 } else {
2862 putEnabledInputMethodsStr(
2863 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2864 }
2865 }
2866
2867 /**
2868 * Build and put a string of EnabledInputMethods with removing specified Id.
2869 * @return the specified id was removed or not.
2870 */
2871 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2872 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2873 boolean isRemoved = false;
2874 boolean needsAppendSeparator = false;
2875 for (Pair<String, ArrayList<String>> ims: imsList) {
2876 String curId = ims.first;
2877 if (curId.equals(id)) {
2878 // We are disabling this input method, and it is
2879 // currently enabled. Skip it to remove from the
2880 // new list.
2881 isRemoved = true;
2882 } else {
2883 if (needsAppendSeparator) {
2884 builder.append(INPUT_METHOD_SEPARATER);
2885 } else {
2886 needsAppendSeparator = true;
2887 }
2888 buildEnabledInputMethodsSettingString(builder, ims);
2889 }
2890 }
2891 if (isRemoved) {
2892 // Update the setting with the new list of input methods.
2893 putEnabledInputMethodsStr(builder.toString());
2894 }
2895 return isRemoved;
2896 }
2897
2898 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2899 List<Pair<String, ArrayList<String>>> imsList) {
2900 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2901 for (Pair<String, ArrayList<String>> ims: imsList) {
2902 InputMethodInfo info = mMethodMap.get(ims.first);
2903 if (info != null) {
2904 res.add(info);
2905 }
2906 }
2907 return res;
2908 }
2909
satok7f35c8c2010-10-07 21:13:11 +09002910 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002911 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002912 List<Pair<String, ArrayList<String>>> imsList) {
2913 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2914 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2915 for (Pair<String, ArrayList<String>> ims : imsList) {
2916 InputMethodInfo info = mMethodMap.get(ims.first);
2917 if (info != null) {
2918 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2919 }
2920 }
2921 return res;
2922 }
2923
satokd87c2592010-09-29 11:52:06 +09002924 private void putEnabledInputMethodsStr(String str) {
2925 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2926 mEnabledInputMethodsStrCache = str;
2927 }
2928
2929 private String getEnabledInputMethodsStr() {
2930 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2931 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002932 if (DEBUG) {
2933 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2934 }
satokd87c2592010-09-29 11:52:06 +09002935 return mEnabledInputMethodsStrCache;
2936 }
satok723a27e2010-11-11 14:58:11 +09002937
2938 private void saveSubtypeHistory(
2939 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2940 StringBuilder builder = new StringBuilder();
2941 boolean isImeAdded = false;
2942 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2943 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2944 newSubtypeId);
2945 isImeAdded = true;
2946 }
2947 for (Pair<String, String> ime: savedImes) {
2948 String imeId = ime.first;
2949 String subtypeId = ime.second;
2950 if (TextUtils.isEmpty(subtypeId)) {
2951 subtypeId = NOT_A_SUBTYPE_ID_STR;
2952 }
2953 if (isImeAdded) {
2954 builder.append(INPUT_METHOD_SEPARATER);
2955 } else {
2956 isImeAdded = true;
2957 }
2958 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2959 subtypeId);
2960 }
2961 // Remove the last INPUT_METHOD_SEPARATER
2962 putSubtypeHistoryStr(builder.toString());
2963 }
2964
2965 public void addSubtypeToHistory(String imeId, String subtypeId) {
2966 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
2967 for (Pair<String, String> ime: subtypeHistory) {
2968 if (ime.first.equals(imeId)) {
2969 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09002970 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09002971 + ime.second);
2972 }
2973 // We should break here
2974 subtypeHistory.remove(ime);
2975 break;
2976 }
2977 }
satokbb4aa062011-01-19 21:40:27 +09002978 if (DEBUG) {
2979 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
2980 }
satok723a27e2010-11-11 14:58:11 +09002981 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
2982 }
2983
2984 private void putSubtypeHistoryStr(String str) {
2985 if (DEBUG) {
2986 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
2987 }
2988 Settings.Secure.putString(
2989 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
2990 }
2991
2992 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
2993 // Gets the first one from the history
2994 return getLastSubtypeForInputMethodLockedInternal(null);
2995 }
2996
2997 public String getLastSubtypeForInputMethodLocked(String imeId) {
2998 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
2999 if (ime != null) {
3000 return ime.second;
3001 } else {
3002 return null;
3003 }
3004 }
3005
3006 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3007 List<Pair<String, ArrayList<String>>> enabledImes =
3008 getEnabledInputMethodsAndSubtypeListLocked();
3009 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003010 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003011 final String imeInTheHistory = imeAndSubtype.first;
3012 // If imeId is empty, returns the first IME and subtype in the history
3013 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3014 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003015 final String subtypeHashCode =
3016 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3017 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003018 if (!TextUtils.isEmpty(subtypeHashCode)) {
3019 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003020 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003021 }
3022 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3023 }
3024 }
3025 }
3026 if (DEBUG) {
3027 Slog.d(TAG, "No enabled IME found in the history");
3028 }
3029 return null;
3030 }
3031
satokdf31ae62011-01-15 06:19:44 +09003032 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003033 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3034 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3035 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003036 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3037 if (explicitlyEnabledSubtypes.size() == 0) {
3038 // If there are no explicitly enabled subtypes, applicable subtypes are
3039 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003040 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003041 // If IME is enabled and no subtypes are enabled, applicable subtypes
3042 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003043 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003044 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003045 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003046 if (implicitlySelectedSubtypes != null) {
3047 final int N = implicitlySelectedSubtypes.size();
3048 for (int i = 0; i < N; ++i) {
3049 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3050 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3051 return subtypeHashCode;
3052 }
3053 }
3054 }
3055 }
3056 } else {
satokf6cafb62011-01-17 16:29:02 +09003057 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003058 if (s.equals(subtypeHashCode)) {
3059 // If both imeId and subtypeId are enabled, return subtypeId.
3060 return s;
3061 }
satok723a27e2010-11-11 14:58:11 +09003062 }
3063 }
3064 // If imeId was enabled but subtypeId was disabled.
3065 return NOT_A_SUBTYPE_ID_STR;
3066 }
3067 }
3068 // If both imeId and subtypeId are disabled, return null
3069 return null;
3070 }
3071
3072 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3073 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3074 final String subtypeHistoryStr = getSubtypeHistoryStr();
3075 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3076 return imsList;
3077 }
3078 mInputMethodSplitter.setString(subtypeHistoryStr);
3079 while (mInputMethodSplitter.hasNext()) {
3080 String nextImsStr = mInputMethodSplitter.next();
3081 mSubtypeSplitter.setString(nextImsStr);
3082 if (mSubtypeSplitter.hasNext()) {
3083 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3084 // The first element is ime id.
3085 String imeId = mSubtypeSplitter.next();
3086 while (mSubtypeSplitter.hasNext()) {
3087 subtypeId = mSubtypeSplitter.next();
3088 break;
3089 }
3090 imsList.add(new Pair<String, String>(imeId, subtypeId));
3091 }
3092 }
3093 return imsList;
3094 }
3095
3096 private String getSubtypeHistoryStr() {
3097 if (DEBUG) {
3098 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3099 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3100 }
3101 return Settings.Secure.getString(
3102 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3103 }
3104
3105 public void putSelectedInputMethod(String imeId) {
3106 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3107 }
3108
3109 public void putSelectedSubtype(int subtypeId) {
3110 Settings.Secure.putInt(
3111 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3112 }
satokd87c2592010-09-29 11:52:06 +09003113 }
3114
satoke7c6998e2011-06-03 17:57:59 +09003115 private static class InputMethodFileManager {
3116 private static final String SYSTEM_PATH = "system";
3117 private static final String INPUT_METHOD_PATH = "inputmethod";
3118 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3119 private static final String NODE_SUBTYPES = "subtypes";
3120 private static final String NODE_SUBTYPE = "subtype";
3121 private static final String NODE_IMI = "imi";
3122 private static final String ATTR_ID = "id";
3123 private static final String ATTR_LABEL = "label";
3124 private static final String ATTR_ICON = "icon";
3125 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3126 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3127 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3128 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3129 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3130 private final HashMap<String, InputMethodInfo> mMethodMap;
3131 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3132 new HashMap<String, List<InputMethodSubtype>>();
3133 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3134 if (methodMap == null) {
3135 throw new NullPointerException("methodMap is null");
3136 }
3137 mMethodMap = methodMap;
3138 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3139 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3140 if (!inputMethodDir.mkdirs()) {
3141 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3142 }
3143 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3144 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3145 if (!subtypeFile.exists()) {
3146 // If "subtypes.xml" doesn't exist, create a blank file.
3147 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3148 methodMap);
3149 } else {
3150 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3151 }
3152 }
3153
3154 private void deleteAllInputMethodSubtypes(String imiId) {
3155 synchronized (mMethodMap) {
3156 mSubtypesMap.remove(imiId);
3157 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3158 mMethodMap);
3159 }
3160 }
3161
3162 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003163 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003164 synchronized (mMethodMap) {
3165 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3166 final int N = additionalSubtypes.length;
3167 for (int i = 0; i < N; ++i) {
3168 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003169 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003170 subtypes.add(subtype);
3171 }
3172 }
satok4a28bde2011-06-29 21:03:40 +09003173 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003174 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3175 mMethodMap);
3176 }
3177 }
3178
3179 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3180 synchronized (mMethodMap) {
3181 return mSubtypesMap;
3182 }
3183 }
3184
3185 private static void writeAdditionalInputMethodSubtypes(
3186 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3187 HashMap<String, InputMethodInfo> methodMap) {
3188 // Safety net for the case that this function is called before methodMap is set.
3189 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3190 FileOutputStream fos = null;
3191 try {
3192 fos = subtypesFile.startWrite();
3193 final XmlSerializer out = new FastXmlSerializer();
3194 out.setOutput(fos, "utf-8");
3195 out.startDocument(null, true);
3196 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3197 out.startTag(null, NODE_SUBTYPES);
3198 for (String imiId : allSubtypes.keySet()) {
3199 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3200 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3201 continue;
3202 }
3203 out.startTag(null, NODE_IMI);
3204 out.attribute(null, ATTR_ID, imiId);
3205 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3206 final int N = subtypesList.size();
3207 for (int i = 0; i < N; ++i) {
3208 final InputMethodSubtype subtype = subtypesList.get(i);
3209 out.startTag(null, NODE_SUBTYPE);
3210 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3211 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3212 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3213 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3214 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3215 out.attribute(null, ATTR_IS_AUXILIARY,
3216 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3217 out.endTag(null, NODE_SUBTYPE);
3218 }
3219 out.endTag(null, NODE_IMI);
3220 }
3221 out.endTag(null, NODE_SUBTYPES);
3222 out.endDocument();
3223 subtypesFile.finishWrite(fos);
3224 } catch (java.io.IOException e) {
3225 Slog.w(TAG, "Error writing subtypes", e);
3226 if (fos != null) {
3227 subtypesFile.failWrite(fos);
3228 }
3229 }
3230 }
3231
3232 private static void readAdditionalInputMethodSubtypes(
3233 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3234 if (allSubtypes == null || subtypesFile == null) return;
3235 allSubtypes.clear();
3236 FileInputStream fis = null;
3237 try {
3238 fis = subtypesFile.openRead();
3239 final XmlPullParser parser = Xml.newPullParser();
3240 parser.setInput(fis, null);
3241 int type = parser.getEventType();
3242 // Skip parsing until START_TAG
3243 while ((type = parser.next()) != XmlPullParser.START_TAG
3244 && type != XmlPullParser.END_DOCUMENT) {}
3245 String firstNodeName = parser.getName();
3246 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3247 throw new XmlPullParserException("Xml doesn't start with subtypes");
3248 }
3249 final int depth =parser.getDepth();
3250 String currentImiId = null;
3251 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3252 while (((type = parser.next()) != XmlPullParser.END_TAG
3253 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3254 if (type != XmlPullParser.START_TAG)
3255 continue;
3256 final String nodeName = parser.getName();
3257 if (NODE_IMI.equals(nodeName)) {
3258 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3259 if (TextUtils.isEmpty(currentImiId)) {
3260 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3261 continue;
3262 }
3263 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3264 allSubtypes.put(currentImiId, tempSubtypesArray);
3265 } else if (NODE_SUBTYPE.equals(nodeName)) {
3266 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3267 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3268 continue;
3269 }
3270 final int icon = Integer.valueOf(
3271 parser.getAttributeValue(null, ATTR_ICON));
3272 final int label = Integer.valueOf(
3273 parser.getAttributeValue(null, ATTR_LABEL));
3274 final String imeSubtypeLocale =
3275 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3276 final String imeSubtypeMode =
3277 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3278 final String imeSubtypeExtraValue =
3279 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003280 final boolean isAuxiliary = "1".equals(String.valueOf(
3281 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003282 final InputMethodSubtype subtype =
3283 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3284 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3285 tempSubtypesArray.add(subtype);
3286 }
3287 }
3288 } catch (XmlPullParserException e) {
3289 Slog.w(TAG, "Error reading subtypes: " + e);
3290 return;
3291 } catch (java.io.IOException e) {
3292 Slog.w(TAG, "Error reading subtypes: " + e);
3293 return;
3294 } catch (NumberFormatException e) {
3295 Slog.w(TAG, "Error reading subtypes: " + e);
3296 return;
3297 } finally {
3298 if (fis != null) {
3299 try {
3300 fis.close();
3301 } catch (java.io.IOException e1) {
3302 Slog.w(TAG, "Failed to close.");
3303 }
3304 }
3305 }
3306 }
3307 }
3308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 @Override
3312 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3313 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3314 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3317 + Binder.getCallingPid()
3318 + ", uid=" + Binder.getCallingUid());
3319 return;
3320 }
3321
3322 IInputMethod method;
3323 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 synchronized (mMethodMap) {
3328 p.println("Current Input Method Manager state:");
3329 int N = mMethodList.size();
3330 p.println(" Input Methods:");
3331 for (int i=0; i<N; i++) {
3332 InputMethodInfo info = mMethodList.get(i);
3333 p.println(" InputMethod #" + i + ":");
3334 info.dump(p, " ");
3335 }
3336 p.println(" Clients:");
3337 for (ClientState ci : mClients.values()) {
3338 p.println(" Client " + ci + ":");
3339 p.println(" client=" + ci.client);
3340 p.println(" inputContext=" + ci.inputContext);
3341 p.println(" sessionRequested=" + ci.sessionRequested);
3342 p.println(" curSession=" + ci.curSession);
3343 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003344 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003346 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3347 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3349 + " mBoundToMethod=" + mBoundToMethod);
3350 p.println(" mCurToken=" + mCurToken);
3351 p.println(" mCurIntent=" + mCurIntent);
3352 method = mCurMethod;
3353 p.println(" mCurMethod=" + mCurMethod);
3354 p.println(" mEnabledSession=" + mEnabledSession);
3355 p.println(" mShowRequested=" + mShowRequested
3356 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3357 + " mShowForced=" + mShowForced
3358 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003359 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003361
Jeff Brownb88102f2010-09-08 11:49:43 -07003362 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 pw.flush();
3365 try {
3366 client.client.asBinder().dump(fd, args);
3367 } catch (RemoteException e) {
3368 p.println("Input method client dead: " + e);
3369 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003370 } else {
3371 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003373
Jeff Brownb88102f2010-09-08 11:49:43 -07003374 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 pw.flush();
3377 try {
3378 method.asBinder().dump(fd, args);
3379 } catch (RemoteException e) {
3380 p.println("Input method service dead: " + e);
3381 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003382 } else {
3383 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
3385 }
3386}