blob: aba1bc627890f3147abf0faf573eff12b21aaa6a [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";
satokc3690562012-01-10 20:14:43 +0900145 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
146 "EnabledWhenDefaultIsNotAsciiCapable";
147 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok4e4569d2010-11-19 18:45:53 +0900148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800150 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900152 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 final IWindowManager mIWindowManager;
155 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900156 private final InputMethodFileManager mFileManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 // All known input methods. mMethodMap also serves as the global
161 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900162 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
163 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900164 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
165 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800166
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700167 // Used to bring IME service up to visible adjustment while it is being shown.
168 final ServiceConnection mVisibleConnection = new ServiceConnection() {
169 @Override public void onServiceConnected(ComponentName name, IBinder service) {
170 }
171
172 @Override public void onServiceDisconnected(ComponentName name) {
173 }
174 };
175 boolean mVisibleBound = false;
176
satok7cfc0ed2011-06-20 21:29:36 +0900177 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700178 private NotificationManager mNotificationManager;
179 private KeyguardManager mKeyguardManager;
180 private StatusBarManagerService mStatusBar;
181 private Notification mImeSwitcherNotification;
182 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900183 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900184 private boolean mNotificationShown;
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 class SessionState {
187 final ClientState client;
188 final IInputMethod method;
189 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 @Override
192 public String toString() {
193 return "SessionState{uid " + client.uid + " pid " + client.pid
194 + " method " + Integer.toHexString(
195 System.identityHashCode(method))
196 + " session " + Integer.toHexString(
197 System.identityHashCode(session))
198 + "}";
199 }
200
201 SessionState(ClientState _client, IInputMethod _method,
202 IInputMethodSession _session) {
203 client = _client;
204 method = _method;
205 session = _session;
206 }
207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 class ClientState {
210 final IInputMethodClient client;
211 final IInputContext inputContext;
212 final int uid;
213 final int pid;
214 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 boolean sessionRequested;
217 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 @Override
220 public String toString() {
221 return "ClientState{" + Integer.toHexString(
222 System.identityHashCode(this)) + " uid " + uid
223 + " pid " + pid + "}";
224 }
225
226 ClientState(IInputMethodClient _client, IInputContext _inputContext,
227 int _uid, int _pid) {
228 client = _client;
229 inputContext = _inputContext;
230 uid = _uid;
231 pid = _pid;
232 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
233 }
234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 final HashMap<IBinder, ClientState> mClients
237 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700240 * Set once the system is ready to run third party code.
241 */
242 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800243
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 * Id of the currently selected input method.
246 */
247 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 /**
250 * The current binding sequence number, incremented every time there is
251 * a new bind performed.
252 */
253 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 /**
256 * The client that is currently bound to an input method.
257 */
258 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700261 * The last window token that gained focus.
262 */
263 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * The input context last provided by the current client.
267 */
268 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The attributes last provided by the current client.
272 */
273 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
276 * The input method ID of the input method service that we are currently
277 * connected to or in the process of connecting to.
278 */
279 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
satokab751aa2010-09-14 19:17:36 +0900282 * The current subtype of the current input method.
283 */
284 private InputMethodSubtype mCurrentSubtype;
285
satok4e4569d2010-11-19 18:45:53 +0900286 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900287 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
288 mShortcutInputMethodsAndSubtypes =
289 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900290
291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 * Set to true if our ServiceConnection is currently actively bound to
293 * a service (whether or not we have gotten its IBinder back yet).
294 */
295 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * Set if the client has asked for the input method to be shown.
299 */
300 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
303 * Set if we were explicitly told to show the input method.
304 */
305 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * Set if we were forced to be shown.
309 */
310 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if we last told the input method to show itself.
314 */
315 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * The Intent used to connect to the current input method.
319 */
320 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * The token we have made for the currently active input method, to
324 * identify it in the future.
325 */
326 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * If non-null, this is the input method service we are currently connected
330 * to.
331 */
332 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Time that we last initiated a bind to the input method, to determine
336 * if we should try to disconnect and reconnect to it.
337 */
338 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
341 * Have we called mCurMethod.bindInput()?
342 */
343 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
346 * Currently enabled session. Only touched by service thread, not
347 * protected by a lock.
348 */
349 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 /**
352 * True if the screen is on. The value is true initially.
353 */
354 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
Joe Onorato857fd9b2011-01-27 15:08:35 -0800356 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
357 int mImeWindowVis;
358
Ken Wakasa05dbb652011-08-22 15:22:43 +0900359 private AlertDialog.Builder mDialogBuilder;
360 private AlertDialog mSwitchingDialog;
361 private InputMethodInfo[] mIms;
362 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 class SettingsObserver extends ContentObserver {
365 SettingsObserver(Handler handler) {
366 super(handler);
367 ContentResolver resolver = mContext.getContentResolver();
368 resolver.registerContentObserver(Settings.Secure.getUriFor(
369 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900370 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900371 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
372 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900373 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 @Override public void onChange(boolean selfChange) {
377 synchronized (mMethodMap) {
378 updateFromSettingsLocked();
379 }
380 }
381 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
384 @Override
385 public void onReceive(Context context, Intent intent) {
386 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
387 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900388 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
390 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900391 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700392 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
393 hideInputMethodMenu();
394 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800396 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398
399 // Inform the current client of the change in active status
400 try {
401 if (mCurClient != null && mCurClient.client != null) {
402 mCurClient.client.setActive(mScreenOn);
403 }
404 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800405 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 + mCurClient.pid + " uid " + mCurClient.uid);
407 }
408 }
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800411 class MyPackageMonitor extends PackageMonitor {
412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800414 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800416 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
418 final int N = mMethodList.size();
419 if (curInputMethodId != null) {
420 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 InputMethodInfo imi = mMethodList.get(i);
422 if (imi.getId().equals(curInputMethodId)) {
423 for (String pkg : packages) {
424 if (imi.getPackageName().equals(pkg)) {
425 if (!doit) {
426 return true;
427 }
satok723a27e2010-11-11 14:58:11 +0900428 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800429 chooseNewDefaultIMELocked();
430 return true;
431 }
432 }
433 }
434 }
435 }
436 }
437 return false;
438 }
439
440 @Override
441 public void onSomePackagesChanged() {
442 synchronized (mMethodMap) {
443 InputMethodInfo curIm = null;
444 String curInputMethodId = Settings.Secure.getString(mContext
445 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
446 final int N = mMethodList.size();
447 if (curInputMethodId != null) {
448 for (int i=0; i<N; i++) {
449 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900450 final String imiId = imi.getId();
451 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 curIm = imi;
453 }
satoke7c6998e2011-06-03 17:57:59 +0900454
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800455 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900456 if (isPackageModified(imi.getPackageName())) {
457 mFileManager.deleteAllInputMethodSubtypes(imiId);
458 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800459 if (change == PACKAGE_TEMPORARY_CHANGE
460 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800461 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800462 + imi.getComponent());
463 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465 }
466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800467
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800468 buildInputMethodListLocked(mMethodList, mMethodMap);
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800471
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800472 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 int change = isPackageDisappearing(curIm.getPackageName());
474 if (change == PACKAGE_TEMPORARY_CHANGE
475 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800476 ServiceInfo si = null;
477 try {
478 si = mContext.getPackageManager().getServiceInfo(
479 curIm.getComponent(), 0);
480 } catch (PackageManager.NameNotFoundException ex) {
481 }
482 if (si == null) {
483 // Uh oh, current input method is no longer around!
484 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800485 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900486 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800487 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800488 changed = true;
489 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800490 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900491 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800492 }
493 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800494 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800495 }
satokab751aa2010-09-14 19:17:36 +0900496
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 if (curIm == null) {
498 // We currently don't have a default input method... is
499 // one now available?
500 changed = chooseNewDefaultIMELocked();
501 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800503 if (changed) {
504 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506 }
507 }
508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Jean Chalarde0d32a62011-10-20 20:36:07 +0900510 private static class MethodCallback extends IInputMethodCallback.Stub {
511 private final IInputMethod mMethod;
512 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Jean Chalarde0d32a62011-10-20 20:36:07 +0900514 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900516 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800518
satoke7c6998e2011-06-03 17:57:59 +0900519 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public void finishedEvent(int seq, boolean handled) throws RemoteException {
521 }
522
satoke7c6998e2011-06-03 17:57:59 +0900523 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900525 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800528
Dianne Hackborn661cd522011-08-22 00:26:20 -0700529 public InputMethodManagerService(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800531 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mHandler = new Handler(this);
533 mIWindowManager = IWindowManager.Stub.asInterface(
534 ServiceManager.getService(Context.WINDOW_SERVICE));
535 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public void executeMessage(Message msg) {
538 handleMessage(msg);
539 }
540 });
satok7cfc0ed2011-06-20 21:29:36 +0900541
satok7cfc0ed2011-06-20 21:29:36 +0900542 mImeSwitcherNotification = new Notification();
543 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
544 mImeSwitcherNotification.when = 0;
545 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
546 mImeSwitcherNotification.tickerText = null;
547 mImeSwitcherNotification.defaults = 0; // please be quiet
548 mImeSwitcherNotification.sound = null;
549 mImeSwitcherNotification.vibrate = null;
550 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900551 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900552
553 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900554
satoke7c6998e2011-06-03 17:57:59 +0900555 synchronized (mMethodMap) {
556 mFileManager = new InputMethodFileManager(mMethodMap);
557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800559 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 IntentFilter screenOnOffFilt = new IntentFilter();
562 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
563 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700564 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800566
satok7cfc0ed2011-06-20 21:29:36 +0900567 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900568
satokd87c2592010-09-29 11:52:06 +0900569 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900570 mSettings = new InputMethodSettings(
571 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900573 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
satokd87c2592010-09-29 11:52:06 +0900575 if (TextUtils.isEmpty(Settings.Secure.getString(
576 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900578 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
580 try {
satokd87c2592010-09-29 11:52:06 +0900581 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 imi.getPackageName(), 0).getResources();
583 if (res.getBoolean(imi.getIsDefaultResourceId())) {
584 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800585 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587 } catch (PackageManager.NameNotFoundException ex) {
588 } catch (Resources.NotFoundException ex) {
589 }
590 }
591 }
satokd87c2592010-09-29 11:52:06 +0900592 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900593 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800594 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900597 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
599 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 mSettingsObserver = new SettingsObserver(mHandler);
602 updateFromSettingsLocked();
603 }
604
605 @Override
606 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
607 throws RemoteException {
608 try {
609 return super.onTransact(code, data, reply, flags);
610 } catch (RuntimeException e) {
611 // The input method manager only throws security exceptions, so let's
612 // log all others.
613 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800614 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616 throw e;
617 }
618 }
619
Dianne Hackborn661cd522011-08-22 00:26:20 -0700620 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700621 synchronized (mMethodMap) {
622 if (!mSystemReady) {
623 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700624 mKeyguardManager = (KeyguardManager)
625 mContext.getSystemService(Context.KEYGUARD_SERVICE);
626 mNotificationManager = (NotificationManager)
627 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
628 mStatusBar = statusBar;
629 statusBar.setIconVisibility("ime", false);
630 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900631 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
632 com.android.internal.R.bool.show_ongoing_ime_switcher);
Dianne Hackborncc278702009-09-02 23:07:23 -0700633 try {
634 startInputInnerLocked();
635 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800636 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700637 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700638 }
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800641
satok15452a42011-10-28 17:58:28 +0900642 private void setImeWindowVisibilityStatusHiddenLocked() {
643 mImeWindowVis = 0;
644 updateImeWindowStatusLocked();
645 }
646
satok3afd6c02011-11-18 08:38:19 +0900647 private void refreshImeWindowVisibilityLocked() {
648 final Configuration conf = mRes.getConfiguration();
649 final boolean haveHardKeyboard = conf.keyboard
650 != Configuration.KEYBOARD_NOKEYS;
651 final boolean hardKeyShown = haveHardKeyboard
652 && conf.hardKeyboardHidden
653 != Configuration.HARDKEYBOARDHIDDEN_YES;
654 final boolean isScreenLocked = mKeyguardManager != null
655 && mKeyguardManager.isKeyguardLocked()
656 && mKeyguardManager.isKeyguardSecure();
657 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
658 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
659 updateImeWindowStatusLocked();
660 }
661
satok15452a42011-10-28 17:58:28 +0900662 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900663 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700664 }
665
satoke7c6998e2011-06-03 17:57:59 +0900666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 public List<InputMethodInfo> getInputMethodList() {
668 synchronized (mMethodMap) {
669 return new ArrayList<InputMethodInfo>(mMethodList);
670 }
671 }
672
satoke7c6998e2011-06-03 17:57:59 +0900673 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 public List<InputMethodInfo> getEnabledInputMethodList() {
675 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900676 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678 }
679
satokbb4aa062011-01-19 21:40:27 +0900680 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
681 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
682 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
683 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
684 for (InputMethodInfo imi: getEnabledInputMethodList()) {
685 enabledInputMethodAndSubtypes.put(
686 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
687 }
688 return enabledInputMethodAndSubtypes;
689 }
690
691 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
692 boolean allowsImplicitlySelectedSubtypes) {
693 if (imi == null && mCurMethodId != null) {
694 imi = mMethodMap.get(mCurMethodId);
695 }
satok7265d9b2011-02-14 15:47:30 +0900696 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900697 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900698 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900699 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900700 }
satok7265d9b2011-02-14 15:47:30 +0900701 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900702 }
703
satoke7c6998e2011-06-03 17:57:59 +0900704 @Override
satok16331c82010-12-20 23:48:46 +0900705 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
706 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900707 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900708 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900709 }
710 }
711
satoke7c6998e2011-06-03 17:57:59 +0900712 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 public void addClient(IInputMethodClient client,
714 IInputContext inputContext, int uid, int pid) {
715 synchronized (mMethodMap) {
716 mClients.put(client.asBinder(), new ClientState(client,
717 inputContext, uid, pid));
718 }
719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
satoke7c6998e2011-06-03 17:57:59 +0900721 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 public void removeClient(IInputMethodClient client) {
723 synchronized (mMethodMap) {
724 mClients.remove(client.asBinder());
725 }
726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 void executeOrSendMessage(IInterface target, Message msg) {
729 if (target.asBinder() instanceof Binder) {
730 mCaller.sendMessage(msg);
731 } else {
732 handleMessage(msg);
733 msg.recycle();
734 }
735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700737 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800739 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 + mCurClient.client.asBinder());
741 if (mBoundToMethod) {
742 mBoundToMethod = false;
743 if (mCurMethod != null) {
744 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
745 MSG_UNBIND_INPUT, mCurMethod));
746 }
747 }
748 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
749 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
750 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 // Call setActive(false) on the old client
753 try {
754 mCurClient.client.setActive(false);
755 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800756 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 + mCurClient.pid + " uid " + mCurClient.uid);
758 }
759 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
The Android Open Source Project10592532009-03-18 17:39:46 -0700761 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 private int getImeShowFlags() {
766 int flags = 0;
767 if (mShowForced) {
768 flags |= InputMethod.SHOW_FORCED
769 | InputMethod.SHOW_EXPLICIT;
770 } else if (mShowExplicitlyRequested) {
771 flags |= InputMethod.SHOW_EXPLICIT;
772 }
773 return flags;
774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 private int getAppShowFlags() {
777 int flags = 0;
778 if (mShowForced) {
779 flags |= InputMethodManager.SHOW_FORCED;
780 } else if (!mShowExplicitlyRequested) {
781 flags |= InputMethodManager.SHOW_IMPLICIT;
782 }
783 return flags;
784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 InputBindResult attachNewInputLocked(boolean initial, boolean needResult) {
787 if (!mBoundToMethod) {
788 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
789 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
790 mBoundToMethod = true;
791 }
792 final SessionState session = mCurClient.curSession;
793 if (initial) {
794 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
795 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
796 } else {
797 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
798 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
799 }
800 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800801 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800802 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804 return needResult
805 ? new InputBindResult(session.session, mCurId, mCurSeq)
806 : null;
807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 InputBindResult startInputLocked(IInputMethodClient client,
810 IInputContext inputContext, EditorInfo attribute,
811 boolean initial, boolean needResult) {
812 // If no method is currently selected, do nothing.
813 if (mCurMethodId == null) {
814 return mNoBinding;
815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 ClientState cs = mClients.get(client.asBinder());
818 if (cs == null) {
819 throw new IllegalArgumentException("unknown client "
820 + client.asBinder());
821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 try {
824 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
825 // Check with the window manager to make sure this client actually
826 // has a window with focus. If not, reject. This is thread safe
827 // because if the focus changes some time before or after, the
828 // next client receiving focus that has any interest in input will
829 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
832 return null;
833 }
834 } catch (RemoteException e) {
835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 if (mCurClient != cs) {
838 // If the client is changing, we need to switch over to the new
839 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700840 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 + cs.client.asBinder());
843
844 // If the screen is on, inform the new client it is active
845 if (mScreenOn) {
846 try {
847 cs.client.setActive(mScreenOn);
848 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800849 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 + cs.pid + " uid " + cs.uid);
851 }
852 }
853 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 // Bump up the sequence for this client and attach it.
856 mCurSeq++;
857 if (mCurSeq <= 0) mCurSeq = 1;
858 mCurClient = cs;
859 mCurInputContext = inputContext;
860 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 // Check if the input method is changing.
863 if (mCurId != null && mCurId.equals(mCurMethodId)) {
864 if (cs.curSession != null) {
865 // Fast case: if we are already connected to the input method,
866 // then just return it.
867 return attachNewInputLocked(initial, needResult);
868 }
869 if (mHaveConnection) {
870 if (mCurMethod != null) {
871 if (!cs.sessionRequested) {
872 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800873 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
875 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900876 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 }
878 // Return to client, and we will get back with it when
879 // we have had a session made for it.
880 return new InputBindResult(null, mCurId, mCurSeq);
881 } else if (SystemClock.uptimeMillis()
882 < (mLastBindTime+TIME_TO_RECONNECT)) {
883 // In this case we have connected to the service, but
884 // don't yet have its interface. If it hasn't been too
885 // long since we did the connection, we'll return to
886 // the client and wait to get the service interface so
887 // we can report back. If it has been too long, we want
888 // to fall through so we can try a disconnect/reconnect
889 // to see if we can get back in touch with the service.
890 return new InputBindResult(null, mCurId, mCurSeq);
891 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800892 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
893 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895 }
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700898 return startInputInnerLocked();
899 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800900
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700901 InputBindResult startInputInnerLocked() {
902 if (mCurMethodId == null) {
903 return mNoBinding;
904 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800905
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700906 if (!mSystemReady) {
907 // If the system is not yet ready, we shouldn't be running third
908 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700909 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700910 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 InputMethodInfo info = mMethodMap.get(mCurMethodId);
913 if (info == null) {
914 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
915 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800916
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700917 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
920 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700921 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
922 com.android.internal.R.string.input_method_binding_label);
923 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
924 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700925 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
926 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 mLastBindTime = SystemClock.uptimeMillis();
928 mHaveConnection = true;
929 mCurId = info.getId();
930 mCurToken = new Binder();
931 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800932 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 mIWindowManager.addWindowToken(mCurToken,
934 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
935 } catch (RemoteException e) {
936 }
937 return new InputBindResult(null, mCurId, mCurSeq);
938 } else {
939 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800940 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 + mCurIntent);
942 }
943 return null;
944 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945
satoke7c6998e2011-06-03 17:57:59 +0900946 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 public InputBindResult startInput(IInputMethodClient client,
948 IInputContext inputContext, EditorInfo attribute,
949 boolean initial, boolean needResult) {
950 synchronized (mMethodMap) {
951 final long ident = Binder.clearCallingIdentity();
952 try {
953 return startInputLocked(client, inputContext, attribute,
954 initial, needResult);
955 } finally {
956 Binder.restoreCallingIdentity(ident);
957 }
958 }
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
satoke7c6998e2011-06-03 17:57:59 +0900961 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 public void finishInput(IInputMethodClient client) {
963 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800964
satoke7c6998e2011-06-03 17:57:59 +0900965 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 public void onServiceConnected(ComponentName name, IBinder service) {
967 synchronized (mMethodMap) {
968 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
969 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -0700970 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800971 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -0700972 unbindCurrentMethodLocked(false);
973 return;
974 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800975 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -0700976 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
977 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800979 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -0700980 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -0700982 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900983 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 }
986 }
987 }
988
989 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
990 synchronized (mMethodMap) {
991 if (mCurMethod != null && method != null
992 && mCurMethod.asBinder() == method.asBinder()) {
993 if (mCurClient != null) {
994 mCurClient.curSession = new SessionState(mCurClient,
995 method, session);
996 mCurClient.sessionRequested = false;
997 InputBindResult res = attachNewInputLocked(true, true);
998 if (res.method != null) {
999 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1000 MSG_BIND_METHOD, mCurClient.client, res));
1001 }
1002 }
1003 }
1004 }
1005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001006
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001007 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001008 if (mVisibleBound) {
1009 mContext.unbindService(mVisibleConnection);
1010 mVisibleBound = false;
1011 }
1012
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001013 if (mHaveConnection) {
1014 mContext.unbindService(this);
1015 mHaveConnection = false;
1016 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001017
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001018 if (mCurToken != null) {
1019 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001020 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001021 mIWindowManager.removeWindowToken(mCurToken);
1022 } catch (RemoteException e) {
1023 }
1024 mCurToken = null;
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
The Android Open Source Project10592532009-03-18 17:39:46 -07001027 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001028 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001029
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001030 if (reportToClient && mCurClient != null) {
1031 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1032 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1033 }
1034 }
1035
Devin Taylor0c33ed22010-02-23 13:26:46 -06001036 private void finishSession(SessionState sessionState) {
1037 if (sessionState != null && sessionState.session != null) {
1038 try {
1039 sessionState.session.finishSession();
1040 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001041 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001042 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001043 }
1044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001047 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 if (mCurMethod != null) {
1049 for (ClientState cs : mClients.values()) {
1050 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001051 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 cs.curSession = null;
1053 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001054
1055 finishSession(mEnabledSession);
1056 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mCurMethod = null;
1058 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001059 if (mStatusBar != null) {
1060 mStatusBar.setIconVisibility("ime", false);
1061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001063
satoke7c6998e2011-06-03 17:57:59 +09001064 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 public void onServiceDisconnected(ComponentName name) {
1066 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001067 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 + " mCurIntent=" + mCurIntent);
1069 if (mCurMethod != null && mCurIntent != null
1070 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001071 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 // We consider this to be a new bind attempt, since the system
1073 // should now try to restart the service for us.
1074 mLastBindTime = SystemClock.uptimeMillis();
1075 mShowRequested = mInputShown;
1076 mInputShown = false;
1077 if (mCurClient != null) {
1078 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1079 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1080 }
1081 }
1082 }
1083 }
1084
satokf9f01002011-05-19 21:31:50 +09001085 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001087 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 long ident = Binder.clearCallingIdentity();
1089 try {
1090 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001091 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 return;
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 synchronized (mMethodMap) {
1096 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001098 if (mStatusBar != null) {
1099 mStatusBar.setIconVisibility("ime", false);
1100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001102 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001103 CharSequence contentDescription = null;
1104 try {
1105 PackageManager packageManager = mContext.getPackageManager();
1106 contentDescription = packageManager.getApplicationLabel(
1107 packageManager.getApplicationInfo(packageName, 0));
1108 } catch (NameNotFoundException nnfe) {
1109 /* ignore */
1110 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001111 if (mStatusBar != null) {
1112 mStatusBar.setIcon("ime", packageName, iconId, 0,
1113 contentDescription != null
1114 ? contentDescription.toString() : null);
1115 mStatusBar.setIconVisibility("ime", true);
1116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 }
1119 } finally {
1120 Binder.restoreCallingIdentity(ident);
1121 }
1122 }
1123
satok7cfc0ed2011-06-20 21:29:36 +09001124 private boolean needsToShowImeSwitchOngoingNotification() {
1125 if (!mShowOngoingImeSwitcherForPhones) return false;
1126 synchronized (mMethodMap) {
1127 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1128 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001129 if (N > 2) return true;
1130 if (N < 1) return false;
1131 int nonAuxCount = 0;
1132 int auxCount = 0;
1133 InputMethodSubtype nonAuxSubtype = null;
1134 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001135 for(int i = 0; i < N; ++i) {
1136 final InputMethodInfo imi = imis.get(i);
1137 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1138 imi, true);
1139 final int subtypeCount = subtypes.size();
1140 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001141 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001142 } else {
1143 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001144 final InputMethodSubtype subtype = subtypes.get(j);
1145 if (!subtype.isAuxiliary()) {
1146 ++nonAuxCount;
1147 nonAuxSubtype = subtype;
1148 } else {
1149 ++auxCount;
1150 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001151 }
1152 }
1153 }
satok7cfc0ed2011-06-20 21:29:36 +09001154 }
satokb6359412011-06-28 17:47:41 +09001155 if (nonAuxCount > 1 || auxCount > 1) {
1156 return true;
1157 } else if (nonAuxCount == 1 && auxCount == 1) {
1158 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001159 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1160 || auxSubtype.overridesImplicitlyEnabledSubtype()
1161 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001162 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1163 return false;
1164 }
1165 return true;
1166 }
1167 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001168 }
satok7cfc0ed2011-06-20 21:29:36 +09001169 }
1170
satokdbf29502011-08-25 15:28:23 +09001171 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001172 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001173 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001174 int uid = Binder.getCallingUid();
1175 long ident = Binder.clearCallingIdentity();
1176 try {
1177 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001178 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001179 return;
1180 }
1181
1182 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001183 mImeWindowVis = vis;
1184 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001185 if (mStatusBar != null) {
1186 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1187 }
satok7cfc0ed2011-06-20 21:29:36 +09001188 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001189 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1190 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001191 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001192 final CharSequence title = mRes.getText(
1193 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001194 final CharSequence imiLabel = imi.loadLabel(pm);
1195 final CharSequence summary = mCurrentSubtype != null
1196 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1197 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1198 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001199 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001200 : imiLabel;
1201
satok7cfc0ed2011-06-20 21:29:36 +09001202 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001203 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001204 if (mNotificationManager != null) {
1205 mNotificationManager.notify(
1206 com.android.internal.R.string.select_input_method,
1207 mImeSwitcherNotification);
1208 mNotificationShown = true;
1209 }
satok7cfc0ed2011-06-20 21:29:36 +09001210 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001211 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001212 mNotificationManager.cancel(
1213 com.android.internal.R.string.select_input_method);
1214 mNotificationShown = false;
1215 }
1216 }
satok06487a52010-10-29 11:37:18 +09001217 }
1218 } finally {
1219 Binder.restoreCallingIdentity(ident);
1220 }
1221 }
1222
satoke7c6998e2011-06-03 17:57:59 +09001223 @Override
satokf9f01002011-05-19 21:31:50 +09001224 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1225 synchronized (mMethodMap) {
1226 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1227 for (int i = 0; i < spans.length; ++i) {
1228 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001229 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001230 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001231 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001232 }
1233 }
1234 }
1235 }
1236
satoke7c6998e2011-06-03 17:57:59 +09001237 @Override
satokf9f01002011-05-19 21:31:50 +09001238 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1239 synchronized (mMethodMap) {
1240 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1241 // TODO: Do not send the intent if the process of the targetImi is already dead.
1242 if (targetImi != null) {
1243 final String[] suggestions = span.getSuggestions();
1244 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001245 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001246 final Intent intent = new Intent();
1247 // Ensures that only a class in the original IME package will receive the
1248 // notification.
satok42c5a162011-05-26 16:46:14 +09001249 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001250 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1251 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1252 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1253 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1254 mContext.sendBroadcast(intent);
1255 return true;
1256 }
1257 }
1258 return false;
1259 }
1260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001262 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1263 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1264 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1265 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001267 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001268 // There is no input method selected, try to choose new applicable input method.
1269 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1270 id = Settings.Secure.getString(mContext.getContentResolver(),
1271 Settings.Secure.DEFAULT_INPUT_METHOD);
1272 }
1273 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 try {
satokab751aa2010-09-14 19:17:36 +09001275 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001278 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001279 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
satokf3db1af2010-11-23 13:34:33 +09001281 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001282 } else {
1283 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001284 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001285 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001288
satokab751aa2010-09-14 19:17:36 +09001289 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 InputMethodInfo info = mMethodMap.get(id);
1291 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001292 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001296 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001297 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1298 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001299 }
1300 if (subtype != mCurrentSubtype) {
1301 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001302 if (subtype != null) {
1303 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1304 }
satokcd7cd292010-11-20 15:46:23 +09001305 if (mCurMethod != null) {
1306 try {
satok3afd6c02011-11-18 08:38:19 +09001307 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001308 // If subtype is null, try to find the most applicable one from
1309 // getCurrentInputMethodSubtype.
1310 if (subtype == null) {
1311 subtype = getCurrentInputMethodSubtype();
1312 }
1313 mCurMethod.changeInputMethodSubtype(subtype);
1314 } catch (RemoteException e) {
1315 return;
satokab751aa2010-09-14 19:17:36 +09001316 }
1317 }
1318 }
1319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 return;
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 final long ident = Binder.clearCallingIdentity();
1324 try {
satokab751aa2010-09-14 19:17:36 +09001325 // Set a subtype to this input method.
1326 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001327 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1328 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1329 // because mCurMethodId is stored as a history in
1330 // setSelectedInputMethodAndSubtypeLocked().
1331 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332
1333 if (ActivityManagerNative.isSystemReady()) {
1334 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001335 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 intent.putExtra("input_method_id", id);
1337 mContext.sendBroadcast(intent);
1338 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001339 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 } finally {
1341 Binder.restoreCallingIdentity(ident);
1342 }
1343 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001344
satok42c5a162011-05-26 16:46:14 +09001345 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001346 public boolean showSoftInput(IInputMethodClient client, int flags,
1347 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001348 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 long ident = Binder.clearCallingIdentity();
1350 try {
1351 synchronized (mMethodMap) {
1352 if (mCurClient == null || client == null
1353 || mCurClient.client.asBinder() != client.asBinder()) {
1354 try {
1355 // We need to check if this is the current client with
1356 // focus in the window manager, to allow this call to
1357 // be made before input is started in it.
1358 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001359 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001360 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
1362 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001363 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
1365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
Joe Onorato8a9b2202010-02-26 18:56:32 -08001367 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001368 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370 } finally {
1371 Binder.restoreCallingIdentity(ident);
1372 }
1373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Project4df24232009-03-05 14:34:35 -08001375 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 mShowRequested = true;
1377 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1378 mShowExplicitlyRequested = true;
1379 }
1380 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1381 mShowExplicitlyRequested = true;
1382 mShowForced = true;
1383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001384
Dianne Hackborncc278702009-09-02 23:07:23 -07001385 if (!mSystemReady) {
1386 return false;
1387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
The Android Open Source Project4df24232009-03-05 14:34:35 -08001389 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001391 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1392 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1393 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001395 if (mHaveConnection && !mVisibleBound) {
1396 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1397 mVisibleBound = true;
1398 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001399 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001401 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 // The client has asked to have the input method shown, but
1403 // we have been sitting here too long with a connection to the
1404 // service and no interface received, so let's disconnect/connect
1405 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001406 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001408 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001410 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1411 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001413
The Android Open Source Project4df24232009-03-05 14:34:35 -08001414 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
satok42c5a162011-05-26 16:46:14 +09001417 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001418 public boolean hideSoftInput(IInputMethodClient client, int flags,
1419 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001420 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 long ident = Binder.clearCallingIdentity();
1422 try {
1423 synchronized (mMethodMap) {
1424 if (mCurClient == null || client == null
1425 || mCurClient.client.asBinder() != client.asBinder()) {
1426 try {
1427 // We need to check if this is the current client with
1428 // focus in the window manager, to allow this call to
1429 // be made before input is started in it.
1430 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001431 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1432 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001433 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001434 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001437 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001438 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
Joe Onorato8a9b2202010-02-26 18:56:32 -08001442 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001443 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 }
1445 } finally {
1446 Binder.restoreCallingIdentity(ident);
1447 }
1448 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001449
The Android Open Source Project4df24232009-03-05 14:34:35 -08001450 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1452 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001453 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001455 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001458 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001460 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001462 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001464 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1465 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1466 res = true;
1467 } else {
1468 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001470 if (mHaveConnection && mVisibleBound) {
1471 mContext.unbindService(mVisibleConnection);
1472 mVisibleBound = false;
1473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 mInputShown = false;
1475 mShowRequested = false;
1476 mShowExplicitlyRequested = false;
1477 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001478 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001480
satok42c5a162011-05-26 16:46:14 +09001481 @Override
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001482 public void windowGainedFocus(IInputMethodClient client, IBinder windowToken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 boolean viewHasFocus, boolean isTextEditor, int softInputMode,
1484 boolean first, int windowFlags) {
1485 long ident = Binder.clearCallingIdentity();
1486 try {
1487 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001488 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 + " viewHasFocus=" + viewHasFocus
1490 + " isTextEditor=" + isTextEditor
1491 + " softInputMode=#" + Integer.toHexString(softInputMode)
1492 + " first=" + first + " flags=#"
1493 + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 if (mCurClient == null || client == null
1496 || mCurClient.client.asBinder() != client.asBinder()) {
1497 try {
1498 // We need to check if this is the current client with
1499 // focus in the window manager, to allow this call to
1500 // be made before input is started in it.
1501 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001502 Slog.w(TAG, "Client not active, ignoring focus gain of: " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 return;
1504 }
1505 } catch (RemoteException e) {
1506 }
1507 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001508
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001509 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001510 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001511 return;
1512 }
1513 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001514
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001515 // Should we auto-show the IME even if the caller has not
1516 // specified what should be done with it?
1517 // We only do this automatically if the window can resize
1518 // to accommodate the IME (so what the user sees will give
1519 // them good context without input information being obscured
1520 // by the IME) or if running on a large screen where there
1521 // is more room for the target window + IME.
1522 final boolean doAutoShow =
1523 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1524 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1525 || mRes.getConfiguration().isLayoutSizeAtLeast(
1526 Configuration.SCREENLAYOUT_SIZE_LARGE);
1527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1529 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001530 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1532 // There is no focus view, and this window will
1533 // be behind any soft input window, so hide the
1534 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001535 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001536 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001538 } else if (isTextEditor && doAutoShow && (softInputMode &
1539 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 // There is a focus view, and we are navigating forward
1541 // into the window, so show the input window for the user.
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001542 // We only do this automatically if the window an resize
1543 // to accomodate the IME (so what the user sees will give
1544 // them good context without input information being obscured
1545 // by the IME) or if running on a large screen where there
1546 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001547 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001548 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 break;
1551 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1552 // Do nothing.
1553 break;
1554 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1555 if ((softInputMode &
1556 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001557 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001558 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
1560 break;
1561 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001562 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001563 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 break;
1565 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1566 if ((softInputMode &
1567 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001568 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001569 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 break;
1572 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001573 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001574 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 break;
1576 }
1577 }
1578 } finally {
1579 Binder.restoreCallingIdentity(ident);
1580 }
1581 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001582
satok42c5a162011-05-26 16:46:14 +09001583 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1585 synchronized (mMethodMap) {
1586 if (mCurClient == null || client == null
1587 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001588 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001589 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
satok440aab52010-11-25 09:43:11 +09001592 // Always call subtype picker, because subtype picker is a superset of input method
1593 // picker.
satokab751aa2010-09-14 19:17:36 +09001594 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1595 }
1596 }
1597
satok42c5a162011-05-26 16:46:14 +09001598 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001600 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1601 }
1602
satok42c5a162011-05-26 16:46:14 +09001603 @Override
satok28203512010-11-24 11:06:49 +09001604 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1605 synchronized (mMethodMap) {
1606 if (subtype != null) {
1607 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1608 mMethodMap.get(id), subtype.hashCode()));
1609 } else {
1610 setInputMethod(token, id);
1611 }
1612 }
satokab751aa2010-09-14 19:17:36 +09001613 }
1614
satok42c5a162011-05-26 16:46:14 +09001615 @Override
satokb416a712010-11-25 20:42:14 +09001616 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001617 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001618 synchronized (mMethodMap) {
1619 if (mCurClient == null || client == null
1620 || mCurClient.client.asBinder() != client.asBinder()) {
1621 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1622 }
satok7fee71f2010-12-17 18:54:26 +09001623 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1624 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001625 }
1626 }
1627
satok4fc87d62011-05-20 16:13:43 +09001628 @Override
satok735cf382010-11-11 20:40:09 +09001629 public boolean switchToLastInputMethod(IBinder token) {
1630 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001631 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001632 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001633 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001634 lastImi = mMethodMap.get(lastIme.first);
1635 } else {
1636 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001637 }
satok4fc87d62011-05-20 16:13:43 +09001638 String targetLastImiId = null;
1639 int subtypeId = NOT_A_SUBTYPE_ID;
1640 if (lastIme != null && lastImi != null) {
1641 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1642 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1643 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1644 : mCurrentSubtype.hashCode();
1645 // If the last IME is the same as the current IME and the last subtype is not
1646 // defined, there is no need to switch to the last IME.
1647 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1648 targetLastImiId = lastIme.first;
1649 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1650 }
1651 }
1652
1653 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1654 // This is a safety net. If the currentSubtype can't be added to the history
1655 // and the framework couldn't find the last ime, we will make the last ime be
1656 // the most applicable enabled keyboard subtype of the system imes.
1657 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1658 if (enabled != null) {
1659 final int N = enabled.size();
1660 final String locale = mCurrentSubtype == null
1661 ? mRes.getConfiguration().locale.toString()
1662 : mCurrentSubtype.getLocale();
1663 for (int i = 0; i < N; ++i) {
1664 final InputMethodInfo imi = enabled.get(i);
1665 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1666 InputMethodSubtype keyboardSubtype =
1667 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1668 SUBTYPE_MODE_KEYBOARD, locale, true);
1669 if (keyboardSubtype != null) {
1670 targetLastImiId = imi.getId();
1671 subtypeId = getSubtypeIdFromHashCode(
1672 imi, keyboardSubtype.hashCode());
1673 if(keyboardSubtype.getLocale().equals(locale)) {
1674 break;
1675 }
1676 }
1677 }
1678 }
1679 }
1680 }
1681
1682 if (!TextUtils.isEmpty(targetLastImiId)) {
1683 if (DEBUG) {
1684 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1685 + ", from: " + mCurMethodId + ", " + subtypeId);
1686 }
1687 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1688 return true;
1689 } else {
1690 return false;
1691 }
satok735cf382010-11-11 20:40:09 +09001692 }
1693 }
1694
satoke7c6998e2011-06-03 17:57:59 +09001695 @Override
satok68f1b782011-04-11 14:26:04 +09001696 public InputMethodSubtype getLastInputMethodSubtype() {
1697 synchronized (mMethodMap) {
1698 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1699 // TODO: Handle the case of the last IME with no subtypes
1700 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1701 || TextUtils.isEmpty(lastIme.second)) return null;
1702 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1703 if (lastImi == null) return null;
1704 try {
1705 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001706 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1707 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1708 return null;
1709 }
1710 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001711 } catch (NumberFormatException e) {
1712 return null;
1713 }
1714 }
1715 }
1716
satoke7c6998e2011-06-03 17:57:59 +09001717 @Override
satokee5e77c2011-09-02 18:50:15 +09001718 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001719 // By this IPC call, only a process which shares the same uid with the IME can add
1720 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001721 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001722 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001723 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001724 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001725 final PackageManager pm = mContext.getPackageManager();
1726 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1727 if (packageInfos != null) {
1728 final int packageNum = packageInfos.length;
1729 for (int i = 0; i < packageNum; ++i) {
1730 if (packageInfos[i].equals(imi.getPackageName())) {
1731 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001732 final long ident = Binder.clearCallingIdentity();
1733 try {
1734 buildInputMethodListLocked(mMethodList, mMethodMap);
1735 } finally {
1736 Binder.restoreCallingIdentity(ident);
1737 }
satokee5e77c2011-09-02 18:50:15 +09001738 return;
satok91e88122011-07-18 11:11:42 +09001739 }
1740 }
1741 }
satoke7c6998e2011-06-03 17:57:59 +09001742 }
satokee5e77c2011-09-02 18:50:15 +09001743 return;
satoke7c6998e2011-06-03 17:57:59 +09001744 }
1745
satok28203512010-11-24 11:06:49 +09001746 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 synchronized (mMethodMap) {
1748 if (token == null) {
1749 if (mContext.checkCallingOrSelfPermission(
1750 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1751 != PackageManager.PERMISSION_GRANTED) {
1752 throw new SecurityException(
1753 "Using null token requires permission "
1754 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1755 }
1756 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001757 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1758 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 return;
1760 }
1761
satokc5933802011-08-31 21:26:04 +09001762 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 try {
satokab751aa2010-09-14 19:17:36 +09001764 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 } finally {
1766 Binder.restoreCallingIdentity(ident);
1767 }
1768 }
1769 }
1770
satok42c5a162011-05-26 16:46:14 +09001771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 public void hideMySoftInput(IBinder token, int flags) {
1773 synchronized (mMethodMap) {
1774 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001775 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1776 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 return;
1778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 long ident = Binder.clearCallingIdentity();
1780 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001781 hideCurrentInputLocked(flags, null);
1782 } finally {
1783 Binder.restoreCallingIdentity(ident);
1784 }
1785 }
1786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
satok42c5a162011-05-26 16:46:14 +09001788 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 public void showMySoftInput(IBinder token, int flags) {
1790 synchronized (mMethodMap) {
1791 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001792 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1793 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 return;
1795 }
1796 long ident = Binder.clearCallingIdentity();
1797 try {
1798 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 } finally {
1800 Binder.restoreCallingIdentity(ident);
1801 }
1802 }
1803 }
1804
1805 void setEnabledSessionInMainThread(SessionState session) {
1806 if (mEnabledSession != session) {
1807 if (mEnabledSession != null) {
1808 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001809 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 mEnabledSession.method.setSessionEnabled(
1811 mEnabledSession.session, false);
1812 } catch (RemoteException e) {
1813 }
1814 }
1815 mEnabledSession = session;
1816 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001817 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 session.method.setSessionEnabled(
1819 session.session, true);
1820 } catch (RemoteException e) {
1821 }
1822 }
1823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
satok42c5a162011-05-26 16:46:14 +09001825 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 public boolean handleMessage(Message msg) {
1827 HandlerCaller.SomeArgs args;
1828 switch (msg.what) {
1829 case MSG_SHOW_IM_PICKER:
1830 showInputMethodMenu();
1831 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001832
satokab751aa2010-09-14 19:17:36 +09001833 case MSG_SHOW_IM_SUBTYPE_PICKER:
1834 showInputMethodSubtypeMenu();
1835 return true;
1836
satok47a44912010-10-06 16:03:58 +09001837 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001838 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001839 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001840 return true;
1841
1842 case MSG_SHOW_IM_CONFIG:
1843 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001844 return true;
1845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 case MSG_UNBIND_INPUT:
1849 try {
1850 ((IInputMethod)msg.obj).unbindInput();
1851 } catch (RemoteException e) {
1852 // There is nothing interesting about the method dying.
1853 }
1854 return true;
1855 case MSG_BIND_INPUT:
1856 args = (HandlerCaller.SomeArgs)msg.obj;
1857 try {
1858 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1859 } catch (RemoteException e) {
1860 }
1861 return true;
1862 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001863 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001865 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1866 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 } catch (RemoteException e) {
1868 }
1869 return true;
1870 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001871 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001873 ((IInputMethod)args.arg1).hideSoftInput(0,
1874 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } catch (RemoteException e) {
1876 }
1877 return true;
1878 case MSG_ATTACH_TOKEN:
1879 args = (HandlerCaller.SomeArgs)msg.obj;
1880 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001881 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1883 } catch (RemoteException e) {
1884 }
1885 return true;
1886 case MSG_CREATE_SESSION:
1887 args = (HandlerCaller.SomeArgs)msg.obj;
1888 try {
1889 ((IInputMethod)args.arg1).createSession(
1890 (IInputMethodCallback)args.arg2);
1891 } catch (RemoteException e) {
1892 }
1893 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 case MSG_START_INPUT:
1897 args = (HandlerCaller.SomeArgs)msg.obj;
1898 try {
1899 SessionState session = (SessionState)args.arg1;
1900 setEnabledSessionInMainThread(session);
1901 session.method.startInput((IInputContext)args.arg2,
1902 (EditorInfo)args.arg3);
1903 } catch (RemoteException e) {
1904 }
1905 return true;
1906 case MSG_RESTART_INPUT:
1907 args = (HandlerCaller.SomeArgs)msg.obj;
1908 try {
1909 SessionState session = (SessionState)args.arg1;
1910 setEnabledSessionInMainThread(session);
1911 session.method.restartInput((IInputContext)args.arg2,
1912 (EditorInfo)args.arg3);
1913 } catch (RemoteException e) {
1914 }
1915 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 case MSG_UNBIND_METHOD:
1920 try {
1921 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
1922 } catch (RemoteException e) {
1923 // There is nothing interesting about the last client dying.
1924 }
1925 return true;
1926 case MSG_BIND_METHOD:
1927 args = (HandlerCaller.SomeArgs)msg.obj;
1928 try {
1929 ((IInputMethodClient)args.arg1).onBindMethod(
1930 (InputBindResult)args.arg2);
1931 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001932 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 return true;
1935 }
1936 return false;
1937 }
1938
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001939 private boolean isSystemIme(InputMethodInfo inputMethod) {
1940 return (inputMethod.getServiceInfo().applicationInfo.flags
1941 & ApplicationInfo.FLAG_SYSTEM) != 0;
1942 }
1943
Ken Wakasa586f0512011-01-20 22:31:01 +09001944 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
1945 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1946 final int subtypeCount = imi.getSubtypeCount();
1947 for (int i = 0; i < subtypeCount; ++i) {
1948 subtypes.add(imi.getSubtypeAt(i));
1949 }
1950 return subtypes;
1951 }
1952
satoka86f5e42011-09-02 17:12:42 +09001953
1954 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
1955 InputMethodInfo imi, String mode) {
1956 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
1957 final int subtypeCount = imi.getSubtypeCount();
1958 for (int i = 0; i < subtypeCount; ++i) {
1959 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
1960 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
1961 subtypes.add(subtype);
1962 }
1963 }
1964 return subtypes;
1965 }
1966
satokdc9ddae2011-10-06 12:22:36 +09001967 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09001968 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001969 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001970 // We'd prefer to fall back on a system IME, since that is safer.
1971 int i=enabled.size();
1972 while (i > 0) {
1973 i--;
satokdc9ddae2011-10-06 12:22:36 +09001974 final InputMethodInfo imi = enabled.get(i);
1975 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07001976 break;
1977 }
1978 }
satokdc9ddae2011-10-06 12:22:36 +09001979 return enabled.get(i);
1980 }
1981 return null;
1982 }
1983
1984 private boolean chooseNewDefaultIMELocked() {
1985 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
1986 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09001987 if (DEBUG) {
1988 Slog.d(TAG, "New default IME was selected: " + imi.getId());
1989 }
satok723a27e2010-11-11 14:58:11 +09001990 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07001991 return true;
1992 }
1993
1994 return false;
1995 }
1996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
1998 HashMap<String, InputMethodInfo> map) {
1999 list.clear();
2000 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002003 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002004 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2005 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2006 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2007 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
2009 List<ResolveInfo> services = pm.queryIntentServices(
2010 new Intent(InputMethod.SERVICE_INTERFACE),
2011 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002012
satoke7c6998e2011-06-03 17:57:59 +09002013 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2014 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 for (int i = 0; i < services.size(); ++i) {
2016 ResolveInfo ri = services.get(i);
2017 ServiceInfo si = ri.serviceInfo;
2018 ComponentName compName = new ComponentName(si.packageName, si.name);
2019 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2020 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002021 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 + ": it does not require the permission "
2023 + android.Manifest.permission.BIND_INPUT_METHOD);
2024 continue;
2025 }
2026
Joe Onorato8a9b2202010-02-26 18:56:32 -08002027 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028
2029 try {
satoke7c6998e2011-06-03 17:57:59 +09002030 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002032 final String id = p.getId();
2033 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
Amith Yamasanie861ec12010-03-24 21:39:27 -07002035 // System IMEs are enabled by default, unless there's a hard keyboard
2036 // and the system IME was explicitly disabled
2037 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2038 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002039 }
2040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002042 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002046 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002048 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
2050 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002051
2052 String defaultIme = Settings.Secure.getString(mContext
2053 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002054 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002055 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002056 updateFromSettingsLocked();
2057 }
2058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002062
satokab751aa2010-09-14 19:17:36 +09002063 private void showInputMethodMenu() {
2064 showInputMethodMenuInternal(false);
2065 }
2066
2067 private void showInputMethodSubtypeMenu() {
2068 showInputMethodMenuInternal(true);
2069 }
2070
satok217f5482010-12-15 05:19:19 +09002071 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002072 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002073 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002074 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2075 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002076 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002077 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002078 }
satok217f5482010-12-15 05:19:19 +09002079 mContext.startActivity(intent);
2080 }
2081
2082 private void showConfigureInputMethods() {
2083 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2084 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2085 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2086 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002087 mContext.startActivity(intent);
2088 }
2089
satokab751aa2010-09-14 19:17:36 +09002090 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002091 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 final PackageManager pm = context.getPackageManager();
satokbc81b692011-08-26 16:22:22 +09002095 final boolean isScreenLocked = mKeyguardManager != null
2096 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 String lastInputMethodId = Settings.Secure.getString(context
2099 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002100 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002101 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002102
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002103 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002104 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2105 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002106 if (immis == null || immis.size() == 0) {
2107 return;
2108 }
2109
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002110 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111
Ken Wakasa761eb372011-03-04 19:06:18 +09002112 final TreeMap<InputMethodInfo, List<InputMethodSubtype>> sortedImmis =
2113 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2114 new Comparator<InputMethodInfo>() {
2115 @Override
2116 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2117 if (imi2 == null) return 0;
2118 if (imi1 == null) return 1;
2119 if (pm == null) {
2120 return imi1.getId().compareTo(imi2.getId());
2121 }
2122 CharSequence imiId1 = imi1.loadLabel(pm) + "/" + imi1.getId();
2123 CharSequence imiId2 = imi2.loadLabel(pm) + "/" + imi2.getId();
2124 return imiId1.toString().compareTo(imiId2.toString());
2125 }
2126 });
satok913a8922010-08-26 21:53:41 +09002127
Ken Wakasa761eb372011-03-04 19:06:18 +09002128 sortedImmis.putAll(immis);
2129
Ken Wakasa05dbb652011-08-22 15:22:43 +09002130 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
Ken Wakasa761eb372011-03-04 19:06:18 +09002131
2132 for (InputMethodInfo imi : sortedImmis.keySet()) {
satokbb4aa062011-01-19 21:40:27 +09002133 if (imi == null) continue;
2134 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
satok7f35c8c2010-10-07 21:13:11 +09002135 HashSet<String> enabledSubtypeSet = new HashSet<String>();
satokbb4aa062011-01-19 21:40:27 +09002136 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2137 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
satok7f35c8c2010-10-07 21:13:11 +09002138 }
satokbb4aa062011-01-19 21:40:27 +09002139 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002140 final CharSequence imeLabel = imi.loadLabel(pm);
satok7f35c8c2010-10-07 21:13:11 +09002141 if (showSubtypes && enabledSubtypeSet.size() > 0) {
satokbb4aa062011-01-19 21:40:27 +09002142 final int subtypeCount = imi.getSubtypeCount();
satok4a28bde2011-06-29 21:03:40 +09002143 if (DEBUG) {
2144 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2145 }
Ken Wakasa586f0512011-01-20 22:31:01 +09002146 for (int j = 0; j < subtypeCount; ++j) {
satok4a28bde2011-06-29 21:03:40 +09002147 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2148 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2149 // We show all enabled IMEs and subtypes when an IME is shown.
2150 if (enabledSubtypeSet.contains(subtypeHashCode)
satokbc81b692011-08-26 16:22:22 +09002151 && ((mInputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
satok38aac042011-09-06 14:41:33 +09002152 final CharSequence subtypeLabel =
2153 subtype.overridesImplicitlyEnabledSubtype() ? null
2154 : subtype.getDisplayName(context, imi.getPackageName(),
2155 imi.getServiceInfo().applicationInfo);
Ken Wakasa05dbb652011-08-22 15:22:43 +09002156 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2157
satok4a28bde2011-06-29 21:03:40 +09002158 // Removing this subtype from enabledSubtypeSet because we no longer
2159 // need to add an entry of this subtype to imList to avoid duplicated
2160 // entries.
2161 enabledSubtypeSet.remove(subtypeHashCode);
satokab751aa2010-09-14 19:17:36 +09002162 }
satokab751aa2010-09-14 19:17:36 +09002163 }
2164 } else {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002165 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
satokab751aa2010-09-14 19:17:36 +09002166 }
Dianne Hackborn97106ab2010-03-03 00:08:31 -08002167 }
satok913a8922010-08-26 21:53:41 +09002168
satokc3690562012-01-10 20:14:43 +09002169 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2170 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2171 if (currentSubtype != null) {
2172 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2173 lastInputMethodSubtypeId =
2174 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2175 }
2176 }
2177
Ken Wakasa761eb372011-03-04 19:06:18 +09002178 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002179 mIms = new InputMethodInfo[N];
2180 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002181 int checkedItem = 0;
2182 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002183 final ImeSubtypeListItem item = imList.get(i);
2184 mIms[i] = item.mImi;
2185 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002186 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002187 int subtypeId = mSubtypeIds[i];
2188 if ((subtypeId == NOT_A_SUBTYPE_ID)
2189 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2190 || (subtypeId == lastInputMethodSubtypeId)) {
2191 checkedItem = i;
2192 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
satokab751aa2010-09-14 19:17:36 +09002195
Ken Wakasa05dbb652011-08-22 15:22:43 +09002196 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002197 com.android.internal.R.styleable.DialogPreference,
2198 com.android.internal.R.attr.alertDialogStyle, 0);
2199 mDialogBuilder = new AlertDialog.Builder(context)
2200 .setTitle(com.android.internal.R.string.select_input_method)
2201 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002202 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002203 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002205 }
2206 })
2207 .setIcon(a.getDrawable(
2208 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2209 a.recycle();
satokd87c2592010-09-29 11:52:06 +09002210
Ken Wakasa05dbb652011-08-22 15:22:43 +09002211 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2212 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2213 checkedItem);
2214
2215 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002216 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002217 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002218 public void onClick(DialogInterface dialog, int which) {
2219 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002220 if (mIms == null || mIms.length <= which
2221 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002222 return;
2223 }
2224 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002225 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002226 hideInputMethodMenu();
2227 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002228 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002229 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002230 subtypeId = NOT_A_SUBTYPE_ID;
2231 }
2232 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002233 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002236 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237
satokbc81b692011-08-26 16:22:22 +09002238 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002239 mDialogBuilder.setPositiveButton(
2240 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002241 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002242 @Override
satok7f35c8c2010-10-07 21:13:11 +09002243 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002244 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002245 }
2246 });
2247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002249 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 mSwitchingDialog.getWindow().setType(
2251 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002252 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 mSwitchingDialog.show();
2254 }
2255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002256
Ken Wakasa05dbb652011-08-22 15:22:43 +09002257 private static class ImeSubtypeListItem {
2258 public final CharSequence mImeName;
2259 public final CharSequence mSubtypeName;
2260 public final InputMethodInfo mImi;
2261 public final int mSubtypeId;
2262 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2263 InputMethodInfo imi, int subtypeId) {
2264 mImeName = imeName;
2265 mSubtypeName = subtypeName;
2266 mImi = imi;
2267 mSubtypeId = subtypeId;
2268 }
2269 }
2270
2271 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2272 private final LayoutInflater mInflater;
2273 private final int mTextViewResourceId;
2274 private final List<ImeSubtypeListItem> mItemsList;
2275 private final int mCheckedItem;
2276 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2277 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2278 super(context, textViewResourceId, itemsList);
2279 mTextViewResourceId = textViewResourceId;
2280 mItemsList = itemsList;
2281 mCheckedItem = checkedItem;
2282 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2283 }
2284
2285 @Override
2286 public View getView(int position, View convertView, ViewGroup parent) {
2287 final View view = convertView != null ? convertView
2288 : mInflater.inflate(mTextViewResourceId, null);
2289 if (position < 0 || position >= mItemsList.size()) return view;
2290 final ImeSubtypeListItem item = mItemsList.get(position);
2291 final CharSequence imeName = item.mImeName;
2292 final CharSequence subtypeName = item.mSubtypeName;
2293 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2294 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2295 if (TextUtils.isEmpty(subtypeName)) {
2296 firstTextView.setText(imeName);
2297 secondTextView.setVisibility(View.GONE);
2298 } else {
2299 firstTextView.setText(subtypeName);
2300 secondTextView.setText(imeName);
2301 secondTextView.setVisibility(View.VISIBLE);
2302 }
2303 final RadioButton radioButton =
2304 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2305 radioButton.setChecked(position == mCheckedItem);
2306 return view;
2307 }
2308 }
2309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002311 synchronized (mMethodMap) {
2312 hideInputMethodMenuLocked();
2313 }
2314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002315
The Android Open Source Project10592532009-03-18 17:39:46 -07002316 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002317 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318
The Android Open Source Project10592532009-03-18 17:39:46 -07002319 if (mSwitchingDialog != null) {
2320 mSwitchingDialog.dismiss();
2321 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002323
The Android Open Source Project10592532009-03-18 17:39:46 -07002324 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002325 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002329
satok42c5a162011-05-26 16:46:14 +09002330 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 public boolean setInputMethodEnabled(String id, boolean enabled) {
2332 synchronized (mMethodMap) {
2333 if (mContext.checkCallingOrSelfPermission(
2334 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2335 != PackageManager.PERMISSION_GRANTED) {
2336 throw new SecurityException(
2337 "Requires permission "
2338 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2339 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 long ident = Binder.clearCallingIdentity();
2342 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002343 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 } finally {
2345 Binder.restoreCallingIdentity(ident);
2346 }
2347 }
2348 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002349
2350 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2351 // Make sure this is a valid input method.
2352 InputMethodInfo imm = mMethodMap.get(id);
2353 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002354 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002355 }
2356
satokd87c2592010-09-29 11:52:06 +09002357 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2358 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002359
satokd87c2592010-09-29 11:52:06 +09002360 if (enabled) {
2361 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2362 if (pair.first.equals(id)) {
2363 // We are enabling this input method, but it is already enabled.
2364 // Nothing to do. The previous state was enabled.
2365 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002366 }
2367 }
satokd87c2592010-09-29 11:52:06 +09002368 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2369 // Previous state was disabled.
2370 return false;
2371 } else {
2372 StringBuilder builder = new StringBuilder();
2373 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2374 builder, enabledInputMethodsList, id)) {
2375 // Disabled input method is currently selected, switch to another one.
2376 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2377 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002378 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2379 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2380 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002381 }
2382 // Previous state was enabled.
2383 return true;
2384 } else {
2385 // We are disabling the input method but it is already disabled.
2386 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002387 return false;
2388 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002389 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002390 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002391
satok57ffc002011-01-25 00:11:47 +09002392 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2393 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002394 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002395 }
2396
satok723a27e2010-11-11 14:58:11 +09002397 private void saveCurrentInputMethodAndSubtypeToHistory() {
2398 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2399 if (mCurrentSubtype != null) {
2400 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2401 }
satok57ffc002011-01-25 00:11:47 +09002402 if (canAddToLastInputMethod(mCurrentSubtype)) {
2403 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2404 }
satokab751aa2010-09-14 19:17:36 +09002405 }
2406
satok723a27e2010-11-11 14:58:11 +09002407 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2408 boolean setSubtypeOnly) {
2409 // Update the history of InputMethod and Subtype
2410 saveCurrentInputMethodAndSubtypeToHistory();
2411
2412 // Set Subtype here
2413 if (imi == null || subtypeId < 0) {
2414 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002415 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002416 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002417 if (subtypeId < imi.getSubtypeCount()) {
2418 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2419 mSettings.putSelectedSubtype(subtype.hashCode());
2420 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002421 } else {
2422 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2423 mCurrentSubtype = null;
2424 }
satokab751aa2010-09-14 19:17:36 +09002425 }
satok723a27e2010-11-11 14:58:11 +09002426
2427 if (!setSubtypeOnly) {
2428 // Set InputMethod here
2429 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2430 }
2431 }
2432
2433 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2434 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2435 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2436 // newDefaultIme is empty when there is no candidate for the selected IME.
2437 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2438 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2439 if (subtypeHashCode != null) {
2440 try {
2441 lastSubtypeId = getSubtypeIdFromHashCode(
2442 imi, Integer.valueOf(subtypeHashCode));
2443 } catch (NumberFormatException e) {
2444 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2445 }
2446 }
2447 }
2448 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002449 }
2450
2451 private int getSelectedInputMethodSubtypeId(String id) {
2452 InputMethodInfo imi = mMethodMap.get(id);
2453 if (imi == null) {
2454 return NOT_A_SUBTYPE_ID;
2455 }
satokab751aa2010-09-14 19:17:36 +09002456 int subtypeId;
2457 try {
2458 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2459 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2460 } catch (SettingNotFoundException e) {
2461 return NOT_A_SUBTYPE_ID;
2462 }
satok723a27e2010-11-11 14:58:11 +09002463 return getSubtypeIdFromHashCode(imi, subtypeId);
2464 }
2465
2466 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002467 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002468 final int subtypeCount = imi.getSubtypeCount();
2469 for (int i = 0; i < subtypeCount; ++i) {
2470 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002471 if (subtypeHashCode == ims.hashCode()) {
2472 return i;
2473 }
satokab751aa2010-09-14 19:17:36 +09002474 }
2475 }
2476 return NOT_A_SUBTYPE_ID;
2477 }
2478
satoka86f5e42011-09-02 17:12:42 +09002479 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2480 Resources res, InputMethodInfo imi) {
2481 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002482 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002483 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002484 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002485 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002486 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002487 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002488 // scan overriding implicitly enabled subtypes.
2489 InputMethodSubtype subtype = subtypes.get(i);
2490 if (subtype.overridesImplicitlyEnabledSubtype()) {
2491 final String mode = subtype.getMode();
2492 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2493 applicableModeAndSubtypesMap.put(mode, subtype);
2494 }
2495 }
2496 }
2497 if (applicableModeAndSubtypesMap.size() > 0) {
2498 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2499 }
2500 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002501 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002502 final String locale = subtype.getLocale();
2503 final String mode = subtype.getMode();
2504 // When system locale starts with subtype's locale, that subtype will be applicable
2505 // for system locale
2506 // For instance, it's clearly applicable for cases like system locale = en_US and
2507 // subtype = en, but it is not necessarily considered applicable for cases like system
2508 // locale = en and subtype = en_US.
2509 // We just call systemLocale.startsWith(locale) in this function because there is no
2510 // need to find applicable subtypes aggressively unlike
2511 // findLastResortApplicableSubtypeLocked.
2512 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002513 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002514 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002515 if (applicableSubtype != null) {
2516 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2517 if (!systemLocale.equals(locale)) continue;
2518 }
satok3da92232011-01-11 22:46:30 +09002519 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002520 }
2521 }
2522 final InputMethodSubtype keyboardSubtype
2523 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2524 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2525 applicableModeAndSubtypesMap.values());
2526 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2527 for (int i = 0; i < N; ++i) {
2528 final InputMethodSubtype subtype = subtypes.get(i);
2529 final String mode = subtype.getMode();
2530 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2531 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2532 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002533 }
2534 }
2535 }
satokc3690562012-01-10 20:14:43 +09002536 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002537 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002538 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002539 if (lastResortKeyboardSubtype != null) {
2540 applicableSubtypes.add(lastResortKeyboardSubtype);
2541 }
2542 }
2543 return applicableSubtypes;
2544 }
2545
satok4e4569d2010-11-19 18:45:53 +09002546 /**
2547 * If there are no selected subtypes, tries finding the most applicable one according to the
2548 * given locale.
2549 * @param subtypes this function will search the most applicable subtype in subtypes
2550 * @param mode subtypes will be filtered by mode
2551 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002552 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2553 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002554 * @return the most applicable subtypeId
2555 */
satokdf31ae62011-01-15 06:19:44 +09002556 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2557 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002558 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002559 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002560 return null;
satok8fbb1e82010-11-02 23:15:58 +09002561 }
satok4e4569d2010-11-19 18:45:53 +09002562 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002563 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002564 }
satok8fbb1e82010-11-02 23:15:58 +09002565 final String language = locale.substring(0, 2);
2566 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002567 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002568 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002569 final int N = subtypes.size();
2570 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002571 InputMethodSubtype subtype = subtypes.get(i);
2572 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002573 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2574 // and all subtypes with all modes can be candidates.
2575 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002576 if (firstMatchedModeSubtype == null) {
2577 firstMatchedModeSubtype = subtype;
2578 }
satok9ef02832010-11-04 21:17:48 +09002579 if (locale.equals(subtypeLocale)) {
2580 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002581 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002582 break;
2583 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2584 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002585 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002586 partialMatchFound = true;
2587 }
satok8fbb1e82010-11-02 23:15:58 +09002588 }
2589 }
2590
satok7599a7f2010-12-22 13:45:23 +09002591 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2592 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002593 }
2594
satok8fbb1e82010-11-02 23:15:58 +09002595 // The first subtype applicable to the system locale will be defined as the most applicable
2596 // subtype.
2597 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002598 if (applicableSubtype != null) {
2599 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2600 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2601 }
satok8fbb1e82010-11-02 23:15:58 +09002602 }
satokcd7cd292010-11-20 15:46:23 +09002603 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002604 }
2605
satok4e4569d2010-11-19 18:45:53 +09002606 // If there are no selected shortcuts, tries finding the most applicable ones.
2607 private Pair<InputMethodInfo, InputMethodSubtype>
2608 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2609 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2610 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002611 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002612 boolean foundInSystemIME = false;
2613
2614 // Search applicable subtype for each InputMethodInfo
2615 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002616 final String imiId = imi.getId();
2617 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2618 continue;
2619 }
satokcd7cd292010-11-20 15:46:23 +09002620 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002621 final List<InputMethodSubtype> enabledSubtypes =
2622 getEnabledInputMethodSubtypeList(imi, true);
2623 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002624 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002625 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002626 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002627 }
satokdf31ae62011-01-15 06:19:44 +09002628 // 2. Search by the system locale from enabledSubtypes.
2629 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002630 if (subtype == null) {
2631 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002632 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002633 }
satoka86f5e42011-09-02 17:12:42 +09002634 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2635 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2636 final ArrayList<InputMethodSubtype> subtypesForSearch =
2637 overridingImplicitlyEnabledSubtypes.isEmpty()
2638 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002639 // 4. Search by the current subtype's locale from all subtypes.
2640 if (subtype == null && mCurrentSubtype != null) {
2641 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002642 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002643 }
2644 // 5. Search by the system locale from all subtypes.
2645 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002646 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002647 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002648 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002649 }
satokcd7cd292010-11-20 15:46:23 +09002650 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002651 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002652 // The current input method is the most applicable IME.
2653 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002654 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002655 break;
satok7599a7f2010-12-22 13:45:23 +09002656 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002657 // The system input method is 2nd applicable IME.
2658 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002659 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002660 if ((imi.getServiceInfo().applicationInfo.flags
2661 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2662 foundInSystemIME = true;
2663 }
satok4e4569d2010-11-19 18:45:53 +09002664 }
2665 }
2666 }
2667 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002668 if (mostApplicableIMI != null) {
2669 Slog.w(TAG, "Most applicable shortcut input method was:"
2670 + mostApplicableIMI.getId());
2671 if (mostApplicableSubtype != null) {
2672 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2673 + "," + mostApplicableSubtype.getMode() + ","
2674 + mostApplicableSubtype.getLocale());
2675 }
2676 }
satok4e4569d2010-11-19 18:45:53 +09002677 }
satokcd7cd292010-11-20 15:46:23 +09002678 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002679 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002680 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002681 } else {
2682 return null;
2683 }
2684 }
2685
satokab751aa2010-09-14 19:17:36 +09002686 /**
2687 * @return Return the current subtype of this input method.
2688 */
satok42c5a162011-05-26 16:46:14 +09002689 @Override
satokab751aa2010-09-14 19:17:36 +09002690 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002691 boolean subtypeIsSelected = false;
2692 try {
2693 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2694 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2695 } catch (SettingNotFoundException e) {
2696 }
satok3ef8b292010-11-23 06:06:29 +09002697 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002698 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002699 String lastInputMethodId = Settings.Secure.getString(
2700 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002701 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2702 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002703 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2704 if (imi != null) {
2705 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002706 // the most applicable subtype from explicitly or implicitly enabled
2707 // subtypes.
2708 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2709 getEnabledInputMethodSubtypeList(imi, true);
2710 // If there is only one explicitly or implicitly enabled subtype,
2711 // just returns it.
2712 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2713 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2714 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2715 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2716 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2717 SUBTYPE_MODE_KEYBOARD, null, true);
2718 if (mCurrentSubtype == null) {
2719 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2720 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2721 true);
2722 }
2723 }
satok4e4569d2010-11-19 18:45:53 +09002724 }
satokcd7cd292010-11-20 15:46:23 +09002725 } else {
satok3ef8b292010-11-23 06:06:29 +09002726 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002727 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002728 }
satok8fbb1e82010-11-02 23:15:58 +09002729 }
satok3ef8b292010-11-23 06:06:29 +09002730 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002731 }
satokab751aa2010-09-14 19:17:36 +09002732 }
2733
satokf3db1af2010-11-23 13:34:33 +09002734 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2735 InputMethodSubtype subtype) {
2736 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2737 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2738 } else {
2739 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2740 subtypes.add(subtype);
2741 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2742 }
2743 }
2744
satok4e4569d2010-11-19 18:45:53 +09002745 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002746 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002747 @Override
satok4e4569d2010-11-19 18:45:53 +09002748 public List getShortcutInputMethodsAndSubtypes() {
2749 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002750 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002751 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002752 // If there are no selected shortcut subtypes, the framework will try to find
2753 // the most applicable subtype from all subtypes whose mode is
2754 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002755 Pair<InputMethodInfo, InputMethodSubtype> info =
2756 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2757 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002758 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002759 ret.add(info.first);
2760 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002761 }
satok3da92232011-01-11 22:46:30 +09002762 return ret;
satokf3db1af2010-11-23 13:34:33 +09002763 }
satokf3db1af2010-11-23 13:34:33 +09002764 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2765 ret.add(imi);
2766 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2767 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002768 }
2769 }
satokf3db1af2010-11-23 13:34:33 +09002770 return ret;
satok4e4569d2010-11-19 18:45:53 +09002771 }
2772 }
2773
satok42c5a162011-05-26 16:46:14 +09002774 @Override
satokb66d2872010-11-10 01:04:04 +09002775 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2776 synchronized (mMethodMap) {
2777 if (subtype != null && mCurMethodId != null) {
2778 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2779 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2780 if (subtypeId != NOT_A_SUBTYPE_ID) {
2781 setInputMethodLocked(mCurMethodId, subtypeId);
2782 return true;
2783 }
2784 }
2785 return false;
2786 }
2787 }
2788
satokd87c2592010-09-29 11:52:06 +09002789 /**
2790 * Utility class for putting and getting settings for InputMethod
2791 * TODO: Move all putters and getters of settings to this class.
2792 */
2793 private static class InputMethodSettings {
2794 // The string for enabled input method is saved as follows:
2795 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2796 private static final char INPUT_METHOD_SEPARATER = ':';
2797 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002798 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002799 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2800
satok723a27e2010-11-11 14:58:11 +09002801 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002802 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2803
satokdf31ae62011-01-15 06:19:44 +09002804 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002805 private final ContentResolver mResolver;
2806 private final HashMap<String, InputMethodInfo> mMethodMap;
2807 private final ArrayList<InputMethodInfo> mMethodList;
2808
2809 private String mEnabledInputMethodsStrCache;
2810
2811 private static void buildEnabledInputMethodsSettingString(
2812 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
2813 String id = pair.first;
2814 ArrayList<String> subtypes = pair.second;
2815 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09002816 // Inputmethod and subtypes are saved in the settings as follows:
2817 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
2818 for (String subtypeId: subtypes) {
2819 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09002820 }
2821 }
2822
2823 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09002824 Resources res, ContentResolver resolver,
2825 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
2826 mRes = res;
satokd87c2592010-09-29 11:52:06 +09002827 mResolver = resolver;
2828 mMethodMap = methodMap;
2829 mMethodList = methodList;
2830 }
2831
2832 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
2833 return createEnabledInputMethodListLocked(
2834 getEnabledInputMethodsAndSubtypeListLocked());
2835 }
2836
satok7f35c8c2010-10-07 21:13:11 +09002837 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002838 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
2839 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002840 getEnabledInputMethodsAndSubtypeListLocked());
2841 }
2842
satok67ddf9c2010-11-17 09:45:54 +09002843 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
2844 InputMethodInfo imi) {
2845 List<Pair<String, ArrayList<String>>> imsList =
2846 getEnabledInputMethodsAndSubtypeListLocked();
2847 ArrayList<InputMethodSubtype> enabledSubtypes =
2848 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09002849 if (imi != null) {
2850 for (Pair<String, ArrayList<String>> imsPair : imsList) {
2851 InputMethodInfo info = mMethodMap.get(imsPair.first);
2852 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002853 final int subtypeCount = info.getSubtypeCount();
2854 for (int i = 0; i < subtypeCount; ++i) {
2855 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09002856 for (String s: imsPair.second) {
2857 if (String.valueOf(ims.hashCode()).equals(s)) {
2858 enabledSubtypes.add(ims);
2859 }
satok67ddf9c2010-11-17 09:45:54 +09002860 }
2861 }
satok884ef9a2010-11-18 10:39:46 +09002862 break;
satok67ddf9c2010-11-17 09:45:54 +09002863 }
satok67ddf9c2010-11-17 09:45:54 +09002864 }
2865 }
2866 return enabledSubtypes;
2867 }
2868
satokd87c2592010-09-29 11:52:06 +09002869 // At the initial boot, the settings for input methods are not set,
2870 // so we need to enable IME in that case.
2871 public void enableAllIMEsIfThereIsNoEnabledIME() {
2872 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
2873 StringBuilder sb = new StringBuilder();
2874 final int N = mMethodList.size();
2875 for (int i = 0; i < N; i++) {
2876 InputMethodInfo imi = mMethodList.get(i);
2877 Slog.i(TAG, "Adding: " + imi.getId());
2878 if (i > 0) sb.append(':');
2879 sb.append(imi.getId());
2880 }
2881 putEnabledInputMethodsStr(sb.toString());
2882 }
2883 }
2884
satokbb4aa062011-01-19 21:40:27 +09002885 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09002886 ArrayList<Pair<String, ArrayList<String>>> imsList
2887 = new ArrayList<Pair<String, ArrayList<String>>>();
2888 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
2889 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
2890 return imsList;
2891 }
satok723a27e2010-11-11 14:58:11 +09002892 mInputMethodSplitter.setString(enabledInputMethodsStr);
2893 while (mInputMethodSplitter.hasNext()) {
2894 String nextImsStr = mInputMethodSplitter.next();
2895 mSubtypeSplitter.setString(nextImsStr);
2896 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09002897 ArrayList<String> subtypeHashes = new ArrayList<String>();
2898 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09002899 String imeId = mSubtypeSplitter.next();
2900 while (mSubtypeSplitter.hasNext()) {
2901 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09002902 }
2903 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
2904 }
2905 }
2906 return imsList;
2907 }
2908
2909 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
2910 if (reloadInputMethodStr) {
2911 getEnabledInputMethodsStr();
2912 }
2913 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
2914 // Add in the newly enabled input method.
2915 putEnabledInputMethodsStr(id);
2916 } else {
2917 putEnabledInputMethodsStr(
2918 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
2919 }
2920 }
2921
2922 /**
2923 * Build and put a string of EnabledInputMethods with removing specified Id.
2924 * @return the specified id was removed or not.
2925 */
2926 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2927 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
2928 boolean isRemoved = false;
2929 boolean needsAppendSeparator = false;
2930 for (Pair<String, ArrayList<String>> ims: imsList) {
2931 String curId = ims.first;
2932 if (curId.equals(id)) {
2933 // We are disabling this input method, and it is
2934 // currently enabled. Skip it to remove from the
2935 // new list.
2936 isRemoved = true;
2937 } else {
2938 if (needsAppendSeparator) {
2939 builder.append(INPUT_METHOD_SEPARATER);
2940 } else {
2941 needsAppendSeparator = true;
2942 }
2943 buildEnabledInputMethodsSettingString(builder, ims);
2944 }
2945 }
2946 if (isRemoved) {
2947 // Update the setting with the new list of input methods.
2948 putEnabledInputMethodsStr(builder.toString());
2949 }
2950 return isRemoved;
2951 }
2952
2953 private List<InputMethodInfo> createEnabledInputMethodListLocked(
2954 List<Pair<String, ArrayList<String>>> imsList) {
2955 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
2956 for (Pair<String, ArrayList<String>> ims: imsList) {
2957 InputMethodInfo info = mMethodMap.get(ims.first);
2958 if (info != null) {
2959 res.add(info);
2960 }
2961 }
2962 return res;
2963 }
2964
satok7f35c8c2010-10-07 21:13:11 +09002965 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09002966 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09002967 List<Pair<String, ArrayList<String>>> imsList) {
2968 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
2969 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
2970 for (Pair<String, ArrayList<String>> ims : imsList) {
2971 InputMethodInfo info = mMethodMap.get(ims.first);
2972 if (info != null) {
2973 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
2974 }
2975 }
2976 return res;
2977 }
2978
satokd87c2592010-09-29 11:52:06 +09002979 private void putEnabledInputMethodsStr(String str) {
2980 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
2981 mEnabledInputMethodsStrCache = str;
2982 }
2983
2984 private String getEnabledInputMethodsStr() {
2985 mEnabledInputMethodsStrCache = Settings.Secure.getString(
2986 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09002987 if (DEBUG) {
2988 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
2989 }
satokd87c2592010-09-29 11:52:06 +09002990 return mEnabledInputMethodsStrCache;
2991 }
satok723a27e2010-11-11 14:58:11 +09002992
2993 private void saveSubtypeHistory(
2994 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
2995 StringBuilder builder = new StringBuilder();
2996 boolean isImeAdded = false;
2997 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
2998 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
2999 newSubtypeId);
3000 isImeAdded = true;
3001 }
3002 for (Pair<String, String> ime: savedImes) {
3003 String imeId = ime.first;
3004 String subtypeId = ime.second;
3005 if (TextUtils.isEmpty(subtypeId)) {
3006 subtypeId = NOT_A_SUBTYPE_ID_STR;
3007 }
3008 if (isImeAdded) {
3009 builder.append(INPUT_METHOD_SEPARATER);
3010 } else {
3011 isImeAdded = true;
3012 }
3013 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3014 subtypeId);
3015 }
3016 // Remove the last INPUT_METHOD_SEPARATER
3017 putSubtypeHistoryStr(builder.toString());
3018 }
3019
3020 public void addSubtypeToHistory(String imeId, String subtypeId) {
3021 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3022 for (Pair<String, String> ime: subtypeHistory) {
3023 if (ime.first.equals(imeId)) {
3024 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003025 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003026 + ime.second);
3027 }
3028 // We should break here
3029 subtypeHistory.remove(ime);
3030 break;
3031 }
3032 }
satokbb4aa062011-01-19 21:40:27 +09003033 if (DEBUG) {
3034 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3035 }
satok723a27e2010-11-11 14:58:11 +09003036 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3037 }
3038
3039 private void putSubtypeHistoryStr(String str) {
3040 if (DEBUG) {
3041 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3042 }
3043 Settings.Secure.putString(
3044 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3045 }
3046
3047 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3048 // Gets the first one from the history
3049 return getLastSubtypeForInputMethodLockedInternal(null);
3050 }
3051
3052 public String getLastSubtypeForInputMethodLocked(String imeId) {
3053 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3054 if (ime != null) {
3055 return ime.second;
3056 } else {
3057 return null;
3058 }
3059 }
3060
3061 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3062 List<Pair<String, ArrayList<String>>> enabledImes =
3063 getEnabledInputMethodsAndSubtypeListLocked();
3064 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003065 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003066 final String imeInTheHistory = imeAndSubtype.first;
3067 // If imeId is empty, returns the first IME and subtype in the history
3068 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3069 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003070 final String subtypeHashCode =
3071 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3072 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003073 if (!TextUtils.isEmpty(subtypeHashCode)) {
3074 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003075 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003076 }
3077 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3078 }
3079 }
3080 }
3081 if (DEBUG) {
3082 Slog.d(TAG, "No enabled IME found in the history");
3083 }
3084 return null;
3085 }
3086
satokdf31ae62011-01-15 06:19:44 +09003087 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003088 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3089 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3090 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003091 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3092 if (explicitlyEnabledSubtypes.size() == 0) {
3093 // If there are no explicitly enabled subtypes, applicable subtypes are
3094 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003095 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003096 // If IME is enabled and no subtypes are enabled, applicable subtypes
3097 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003098 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003099 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003100 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003101 if (implicitlySelectedSubtypes != null) {
3102 final int N = implicitlySelectedSubtypes.size();
3103 for (int i = 0; i < N; ++i) {
3104 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3105 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3106 return subtypeHashCode;
3107 }
3108 }
3109 }
3110 }
3111 } else {
satokf6cafb62011-01-17 16:29:02 +09003112 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003113 if (s.equals(subtypeHashCode)) {
3114 // If both imeId and subtypeId are enabled, return subtypeId.
3115 return s;
3116 }
satok723a27e2010-11-11 14:58:11 +09003117 }
3118 }
3119 // If imeId was enabled but subtypeId was disabled.
3120 return NOT_A_SUBTYPE_ID_STR;
3121 }
3122 }
3123 // If both imeId and subtypeId are disabled, return null
3124 return null;
3125 }
3126
3127 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3128 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3129 final String subtypeHistoryStr = getSubtypeHistoryStr();
3130 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3131 return imsList;
3132 }
3133 mInputMethodSplitter.setString(subtypeHistoryStr);
3134 while (mInputMethodSplitter.hasNext()) {
3135 String nextImsStr = mInputMethodSplitter.next();
3136 mSubtypeSplitter.setString(nextImsStr);
3137 if (mSubtypeSplitter.hasNext()) {
3138 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3139 // The first element is ime id.
3140 String imeId = mSubtypeSplitter.next();
3141 while (mSubtypeSplitter.hasNext()) {
3142 subtypeId = mSubtypeSplitter.next();
3143 break;
3144 }
3145 imsList.add(new Pair<String, String>(imeId, subtypeId));
3146 }
3147 }
3148 return imsList;
3149 }
3150
3151 private String getSubtypeHistoryStr() {
3152 if (DEBUG) {
3153 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3154 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3155 }
3156 return Settings.Secure.getString(
3157 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3158 }
3159
3160 public void putSelectedInputMethod(String imeId) {
3161 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3162 }
3163
3164 public void putSelectedSubtype(int subtypeId) {
3165 Settings.Secure.putInt(
3166 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3167 }
satokd87c2592010-09-29 11:52:06 +09003168 }
3169
satoke7c6998e2011-06-03 17:57:59 +09003170 private static class InputMethodFileManager {
3171 private static final String SYSTEM_PATH = "system";
3172 private static final String INPUT_METHOD_PATH = "inputmethod";
3173 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3174 private static final String NODE_SUBTYPES = "subtypes";
3175 private static final String NODE_SUBTYPE = "subtype";
3176 private static final String NODE_IMI = "imi";
3177 private static final String ATTR_ID = "id";
3178 private static final String ATTR_LABEL = "label";
3179 private static final String ATTR_ICON = "icon";
3180 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3181 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3182 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3183 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3184 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3185 private final HashMap<String, InputMethodInfo> mMethodMap;
3186 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3187 new HashMap<String, List<InputMethodSubtype>>();
3188 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3189 if (methodMap == null) {
3190 throw new NullPointerException("methodMap is null");
3191 }
3192 mMethodMap = methodMap;
3193 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3194 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3195 if (!inputMethodDir.mkdirs()) {
3196 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3197 }
3198 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3199 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3200 if (!subtypeFile.exists()) {
3201 // If "subtypes.xml" doesn't exist, create a blank file.
3202 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3203 methodMap);
3204 } else {
3205 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3206 }
3207 }
3208
3209 private void deleteAllInputMethodSubtypes(String imiId) {
3210 synchronized (mMethodMap) {
3211 mSubtypesMap.remove(imiId);
3212 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3213 mMethodMap);
3214 }
3215 }
3216
3217 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003218 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003219 synchronized (mMethodMap) {
3220 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3221 final int N = additionalSubtypes.length;
3222 for (int i = 0; i < N; ++i) {
3223 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003224 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003225 subtypes.add(subtype);
3226 }
3227 }
satok4a28bde2011-06-29 21:03:40 +09003228 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003229 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3230 mMethodMap);
3231 }
3232 }
3233
3234 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3235 synchronized (mMethodMap) {
3236 return mSubtypesMap;
3237 }
3238 }
3239
3240 private static void writeAdditionalInputMethodSubtypes(
3241 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3242 HashMap<String, InputMethodInfo> methodMap) {
3243 // Safety net for the case that this function is called before methodMap is set.
3244 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3245 FileOutputStream fos = null;
3246 try {
3247 fos = subtypesFile.startWrite();
3248 final XmlSerializer out = new FastXmlSerializer();
3249 out.setOutput(fos, "utf-8");
3250 out.startDocument(null, true);
3251 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3252 out.startTag(null, NODE_SUBTYPES);
3253 for (String imiId : allSubtypes.keySet()) {
3254 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3255 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3256 continue;
3257 }
3258 out.startTag(null, NODE_IMI);
3259 out.attribute(null, ATTR_ID, imiId);
3260 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3261 final int N = subtypesList.size();
3262 for (int i = 0; i < N; ++i) {
3263 final InputMethodSubtype subtype = subtypesList.get(i);
3264 out.startTag(null, NODE_SUBTYPE);
3265 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3266 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3267 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3268 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3269 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3270 out.attribute(null, ATTR_IS_AUXILIARY,
3271 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3272 out.endTag(null, NODE_SUBTYPE);
3273 }
3274 out.endTag(null, NODE_IMI);
3275 }
3276 out.endTag(null, NODE_SUBTYPES);
3277 out.endDocument();
3278 subtypesFile.finishWrite(fos);
3279 } catch (java.io.IOException e) {
3280 Slog.w(TAG, "Error writing subtypes", e);
3281 if (fos != null) {
3282 subtypesFile.failWrite(fos);
3283 }
3284 }
3285 }
3286
3287 private static void readAdditionalInputMethodSubtypes(
3288 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3289 if (allSubtypes == null || subtypesFile == null) return;
3290 allSubtypes.clear();
3291 FileInputStream fis = null;
3292 try {
3293 fis = subtypesFile.openRead();
3294 final XmlPullParser parser = Xml.newPullParser();
3295 parser.setInput(fis, null);
3296 int type = parser.getEventType();
3297 // Skip parsing until START_TAG
3298 while ((type = parser.next()) != XmlPullParser.START_TAG
3299 && type != XmlPullParser.END_DOCUMENT) {}
3300 String firstNodeName = parser.getName();
3301 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3302 throw new XmlPullParserException("Xml doesn't start with subtypes");
3303 }
3304 final int depth =parser.getDepth();
3305 String currentImiId = null;
3306 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3307 while (((type = parser.next()) != XmlPullParser.END_TAG
3308 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3309 if (type != XmlPullParser.START_TAG)
3310 continue;
3311 final String nodeName = parser.getName();
3312 if (NODE_IMI.equals(nodeName)) {
3313 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3314 if (TextUtils.isEmpty(currentImiId)) {
3315 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3316 continue;
3317 }
3318 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3319 allSubtypes.put(currentImiId, tempSubtypesArray);
3320 } else if (NODE_SUBTYPE.equals(nodeName)) {
3321 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3322 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3323 continue;
3324 }
3325 final int icon = Integer.valueOf(
3326 parser.getAttributeValue(null, ATTR_ICON));
3327 final int label = Integer.valueOf(
3328 parser.getAttributeValue(null, ATTR_LABEL));
3329 final String imeSubtypeLocale =
3330 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3331 final String imeSubtypeMode =
3332 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3333 final String imeSubtypeExtraValue =
3334 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003335 final boolean isAuxiliary = "1".equals(String.valueOf(
3336 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003337 final InputMethodSubtype subtype =
3338 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3339 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3340 tempSubtypesArray.add(subtype);
3341 }
3342 }
3343 } catch (XmlPullParserException e) {
3344 Slog.w(TAG, "Error reading subtypes: " + e);
3345 return;
3346 } catch (java.io.IOException e) {
3347 Slog.w(TAG, "Error reading subtypes: " + e);
3348 return;
3349 } catch (NumberFormatException e) {
3350 Slog.w(TAG, "Error reading subtypes: " + e);
3351 return;
3352 } finally {
3353 if (fis != null) {
3354 try {
3355 fis.close();
3356 } catch (java.io.IOException e1) {
3357 Slog.w(TAG, "Failed to close.");
3358 }
3359 }
3360 }
3361 }
3362 }
3363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 @Override
3367 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3368 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3369 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3372 + Binder.getCallingPid()
3373 + ", uid=" + Binder.getCallingUid());
3374 return;
3375 }
3376
3377 IInputMethod method;
3378 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 synchronized (mMethodMap) {
3383 p.println("Current Input Method Manager state:");
3384 int N = mMethodList.size();
3385 p.println(" Input Methods:");
3386 for (int i=0; i<N; i++) {
3387 InputMethodInfo info = mMethodList.get(i);
3388 p.println(" InputMethod #" + i + ":");
3389 info.dump(p, " ");
3390 }
3391 p.println(" Clients:");
3392 for (ClientState ci : mClients.values()) {
3393 p.println(" Client " + ci + ":");
3394 p.println(" client=" + ci.client);
3395 p.println(" inputContext=" + ci.inputContext);
3396 p.println(" sessionRequested=" + ci.sessionRequested);
3397 p.println(" curSession=" + ci.curSession);
3398 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003399 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003401 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3402 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3404 + " mBoundToMethod=" + mBoundToMethod);
3405 p.println(" mCurToken=" + mCurToken);
3406 p.println(" mCurIntent=" + mCurIntent);
3407 method = mCurMethod;
3408 p.println(" mCurMethod=" + mCurMethod);
3409 p.println(" mEnabledSession=" + mEnabledSession);
3410 p.println(" mShowRequested=" + mShowRequested
3411 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3412 + " mShowForced=" + mShowForced
3413 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003414 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003416
Jeff Brownb88102f2010-09-08 11:49:43 -07003417 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 pw.flush();
3420 try {
3421 client.client.asBinder().dump(fd, args);
3422 } catch (RemoteException e) {
3423 p.println("Input method client dead: " + e);
3424 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003425 } else {
3426 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003428
Jeff Brownb88102f2010-09-08 11:49:43 -07003429 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 pw.flush();
3432 try {
3433 method.asBinder().dump(fd, args);
3434 } catch (RemoteException e) {
3435 p.println("Input method service dead: " + e);
3436 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003437 } else {
3438 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 }
3440 }
3441}