blob: ca7241c3dfcc3ed71b0dfce6d1b7f98b9ef24dc7 [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;
satok01038492012-04-09 21:08:27 +090030import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
37import android.app.AlertDialog;
satokf90a33e2011-07-19 11:55:52 +090038import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090039import android.app.Notification;
40import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070041import android.app.PendingIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.ComponentName;
43import android.content.ContentResolver;
44import android.content.Context;
45import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.DialogInterface.OnCancelListener;
47import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090048import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070050import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070052import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070055import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.res.Resources;
57import android.content.res.TypedArray;
58import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080059import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090061import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.Handler;
63import android.os.IBinder;
64import android.os.IInterface;
65import android.os.Message;
66import android.os.Parcel;
67import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080068import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.ServiceManager;
70import android.os.SystemClock;
71import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070072import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090073import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090075import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090077import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090078import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.util.PrintWriterPrinter;
80import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090081import android.util.Slog;
82import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090084import android.view.LayoutInflater;
85import android.view.View;
86import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090088import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.view.inputmethod.InputBinding;
90import android.view.inputmethod.InputMethod;
91import android.view.inputmethod.InputMethodInfo;
92import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090093import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090094import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +090095import android.widget.CompoundButton;
96import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +090097import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +090098import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +090099import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
satoke7c6998e2011-06-03 17:57:59 +0900101import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900103import java.io.FileInputStream;
104import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.io.IOException;
106import java.io.PrintWriter;
107import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900108import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900109import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900111import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.util.List;
satok913a8922010-08-26 21:53:41 +0900113import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115/**
116 * This class provides a system service that manages input methods.
117 */
118public class InputMethodManagerService extends IInputMethodManager.Stub
119 implements ServiceConnection, Handler.Callback {
120 static final boolean DEBUG = false;
121 static final String TAG = "InputManagerService";
122
123 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900124 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900125 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900126 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 static final int MSG_UNBIND_INPUT = 1000;
129 static final int MSG_BIND_INPUT = 1010;
130 static final int MSG_SHOW_SOFT_INPUT = 1020;
131 static final int MSG_HIDE_SOFT_INPUT = 1030;
132 static final int MSG_ATTACH_TOKEN = 1040;
133 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 static final int MSG_START_INPUT = 2000;
136 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final int MSG_UNBIND_METHOD = 3000;
139 static final int MSG_BIND_METHOD = 3010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800140
satok01038492012-04-09 21:08:27 +0900141 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800144
satokf9f01002011-05-19 21:31:50 +0900145 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
146
satokab751aa2010-09-14 19:17:36 +0900147 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900148 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900149 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
150 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900151 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900152 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
153 "EnabledWhenDefaultIsNotAsciiCapable";
154 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok4e4569d2010-11-19 18:45:53 +0900155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800157 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900159 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 final IWindowManager mIWindowManager;
162 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900163 private final InputMethodFileManager mFileManager;
satok688bd472012-02-09 20:09:17 +0900164 private final InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900165 private final HardKeyboardListener mHardKeyboardListener;
166 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 // All known input methods. mMethodMap also serves as the global
171 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900172 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
173 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900174 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
175 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800176
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700177 // Used to bring IME service up to visible adjustment while it is being shown.
178 final ServiceConnection mVisibleConnection = new ServiceConnection() {
179 @Override public void onServiceConnected(ComponentName name, IBinder service) {
180 }
181
182 @Override public void onServiceDisconnected(ComponentName name) {
183 }
184 };
185 boolean mVisibleBound = false;
186
satok7cfc0ed2011-06-20 21:29:36 +0900187 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700188 private NotificationManager mNotificationManager;
189 private KeyguardManager mKeyguardManager;
190 private StatusBarManagerService mStatusBar;
191 private Notification mImeSwitcherNotification;
192 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900193 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900194 private boolean mNotificationShown;
195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 class SessionState {
197 final ClientState client;
198 final IInputMethod method;
199 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 @Override
202 public String toString() {
203 return "SessionState{uid " + client.uid + " pid " + client.pid
204 + " method " + Integer.toHexString(
205 System.identityHashCode(method))
206 + " session " + Integer.toHexString(
207 System.identityHashCode(session))
208 + "}";
209 }
210
211 SessionState(ClientState _client, IInputMethod _method,
212 IInputMethodSession _session) {
213 client = _client;
214 method = _method;
215 session = _session;
216 }
217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 class ClientState {
220 final IInputMethodClient client;
221 final IInputContext inputContext;
222 final int uid;
223 final int pid;
224 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 boolean sessionRequested;
227 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public String toString() {
231 return "ClientState{" + Integer.toHexString(
232 System.identityHashCode(this)) + " uid " + uid
233 + " pid " + pid + "}";
234 }
235
236 ClientState(IInputMethodClient _client, IInputContext _inputContext,
237 int _uid, int _pid) {
238 client = _client;
239 inputContext = _inputContext;
240 uid = _uid;
241 pid = _pid;
242 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
243 }
244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 final HashMap<IBinder, ClientState> mClients
247 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700250 * Set once the system is ready to run third party code.
251 */
252 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * Id of the currently selected input method.
256 */
257 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * The current binding sequence number, incremented every time there is
261 * a new bind performed.
262 */
263 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * The client that is currently bound to an input method.
267 */
268 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700271 * The last window token that gained focus.
272 */
273 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 * The input context last provided by the current client.
277 */
278 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
281 * The attributes last provided by the current client.
282 */
283 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The input method ID of the input method service that we are currently
287 * connected to or in the process of connecting to.
288 */
289 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
satokab751aa2010-09-14 19:17:36 +0900292 * The current subtype of the current input method.
293 */
294 private InputMethodSubtype mCurrentSubtype;
295
satok4e4569d2010-11-19 18:45:53 +0900296 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900297 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
298 mShortcutInputMethodsAndSubtypes =
299 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900300
301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 * Set to true if our ServiceConnection is currently actively bound to
303 * a service (whether or not we have gotten its IBinder back yet).
304 */
305 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * Set if the client has asked for the input method to be shown.
309 */
310 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if we were explicitly told to show the input method.
314 */
315 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if we were forced to be shown.
319 */
320 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we last told the input method to show itself.
324 */
325 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * The Intent used to connect to the current input method.
329 */
330 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The token we have made for the currently active input method, to
334 * identify it in the future.
335 */
336 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * If non-null, this is the input method service we are currently connected
340 * to.
341 */
342 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * Time that we last initiated a bind to the input method, to determine
346 * if we should try to disconnect and reconnect to it.
347 */
348 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * Have we called mCurMethod.bindInput()?
352 */
353 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Currently enabled session. Only touched by service thread, not
357 * protected by a lock.
358 */
359 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * True if the screen is on. The value is true initially.
363 */
364 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
Joe Onorato857fd9b2011-01-27 15:08:35 -0800366 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
367 int mImeWindowVis;
368
Ken Wakasa05dbb652011-08-22 15:22:43 +0900369 private AlertDialog.Builder mDialogBuilder;
370 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900371 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900372 private InputMethodInfo[] mIms;
373 private int[] mSubtypeIds;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 class SettingsObserver extends ContentObserver {
376 SettingsObserver(Handler handler) {
377 super(handler);
378 ContentResolver resolver = mContext.getContentResolver();
379 resolver.registerContentObserver(Settings.Secure.getUriFor(
380 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900381 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900382 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
383 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900384 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 @Override public void onChange(boolean selfChange) {
388 synchronized (mMethodMap) {
389 updateFromSettingsLocked();
390 }
391 }
392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
395 @Override
396 public void onReceive(Context context, Intent intent) {
397 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
398 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900399 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
401 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900402 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700403 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
404 hideInputMethodMenu();
405 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800407 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409
410 // Inform the current client of the change in active status
411 try {
412 if (mCurClient != null && mCurClient.client != null) {
413 mCurClient.client.setActive(mScreenOn);
414 }
415 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800416 Slog.w(TAG, "Got RemoteException sending 'screen on/off' notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 + mCurClient.pid + " uid " + mCurClient.uid);
418 }
419 }
420 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800422 class MyPackageMonitor extends PackageMonitor {
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800425 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800427 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
429 final int N = mMethodList.size();
430 if (curInputMethodId != null) {
431 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 InputMethodInfo imi = mMethodList.get(i);
433 if (imi.getId().equals(curInputMethodId)) {
434 for (String pkg : packages) {
435 if (imi.getPackageName().equals(pkg)) {
436 if (!doit) {
437 return true;
438 }
satok723a27e2010-11-11 14:58:11 +0900439 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800440 chooseNewDefaultIMELocked();
441 return true;
442 }
443 }
444 }
445 }
446 }
447 }
448 return false;
449 }
450
451 @Override
452 public void onSomePackagesChanged() {
453 synchronized (mMethodMap) {
454 InputMethodInfo curIm = null;
455 String curInputMethodId = Settings.Secure.getString(mContext
456 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
457 final int N = mMethodList.size();
458 if (curInputMethodId != null) {
459 for (int i=0; i<N; i++) {
460 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900461 final String imiId = imi.getId();
462 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800463 curIm = imi;
464 }
satoke7c6998e2011-06-03 17:57:59 +0900465
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800466 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900467 if (isPackageModified(imi.getPackageName())) {
468 mFileManager.deleteAllInputMethodSubtypes(imiId);
469 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 if (change == PACKAGE_TEMPORARY_CHANGE
471 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800472 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 + imi.getComponent());
474 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476 }
477 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800478
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800479 buildInputMethodListLocked(mMethodList, mMethodMap);
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800482
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800483 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 int change = isPackageDisappearing(curIm.getPackageName());
485 if (change == PACKAGE_TEMPORARY_CHANGE
486 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800487 ServiceInfo si = null;
488 try {
489 si = mContext.getPackageManager().getServiceInfo(
490 curIm.getComponent(), 0);
491 } catch (PackageManager.NameNotFoundException ex) {
492 }
493 if (si == null) {
494 // Uh oh, current input method is no longer around!
495 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800496 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900497 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800499 changed = true;
500 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800501 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900502 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800503 }
504 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800505 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800506 }
satokab751aa2010-09-14 19:17:36 +0900507
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 if (curIm == null) {
509 // We currently don't have a default input method... is
510 // one now available?
511 changed = chooseNewDefaultIMELocked();
512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 if (changed) {
515 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517 }
518 }
519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800520
Jean Chalarde0d32a62011-10-20 20:36:07 +0900521 private static class MethodCallback extends IInputMethodCallback.Stub {
522 private final IInputMethod mMethod;
523 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Jean Chalarde0d32a62011-10-20 20:36:07 +0900525 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900527 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800529
satoke7c6998e2011-06-03 17:57:59 +0900530 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 public void finishedEvent(int seq, boolean handled) throws RemoteException {
532 }
533
satoke7c6998e2011-06-03 17:57:59 +0900534 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900536 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800539
satok01038492012-04-09 21:08:27 +0900540 private class HardKeyboardListener
541 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
542 @Override
543 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
544 mHandler.sendMessage(mHandler.obtainMessage(
545 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
546 }
547
548 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
549 if (DEBUG) {
550 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
551 + enabled);
552 }
553 synchronized(mMethodMap) {
554 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
555 && mSwitchingDialog.isShowing()) {
556 mSwitchingDialogTitleView.findViewById(
557 com.android.internal.R.id.hard_keyboard_section).setVisibility(
558 available ? View.VISIBLE : View.GONE);
559 }
560 }
561 }
562 }
563
564 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800566 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 mHandler = new Handler(this);
568 mIWindowManager = IWindowManager.Stub.asInterface(
569 ServiceManager.getService(Context.WINDOW_SERVICE));
570 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900571 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 public void executeMessage(Message msg) {
573 handleMessage(msg);
574 }
575 });
satok01038492012-04-09 21:08:27 +0900576 mWindowManagerService = windowManager;
577 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900578
satok7cfc0ed2011-06-20 21:29:36 +0900579 mImeSwitcherNotification = new Notification();
580 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
581 mImeSwitcherNotification.when = 0;
582 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
583 mImeSwitcherNotification.tickerText = null;
584 mImeSwitcherNotification.defaults = 0; // please be quiet
585 mImeSwitcherNotification.sound = null;
586 mImeSwitcherNotification.vibrate = null;
587 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900588 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900589
590 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900591
satoke7c6998e2011-06-03 17:57:59 +0900592 synchronized (mMethodMap) {
593 mFileManager = new InputMethodFileManager(mMethodMap);
594 }
satok688bd472012-02-09 20:09:17 +0900595 mImListManager = new InputMethodAndSubtypeListManager(context, this);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800596
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800597 (new MyPackageMonitor()).register(mContext, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 IntentFilter screenOnOffFilt = new IntentFilter();
600 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
601 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700602 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
satok7cfc0ed2011-06-20 21:29:36 +0900605 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900606
satokd87c2592010-09-29 11:52:06 +0900607 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900608 mSettings = new InputMethodSettings(
609 mRes, context.getContentResolver(), mMethodMap, mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900611 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
satokd87c2592010-09-29 11:52:06 +0900613 if (TextUtils.isEmpty(Settings.Secure.getString(
614 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 InputMethodInfo defIm = null;
satokd87c2592010-09-29 11:52:06 +0900616 for (InputMethodInfo imi: mMethodList) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 if (defIm == null && imi.getIsDefaultResourceId() != 0) {
618 try {
satokd87c2592010-09-29 11:52:06 +0900619 Resources res = context.createPackageContext(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 imi.getPackageName(), 0).getResources();
621 if (res.getBoolean(imi.getIsDefaultResourceId())) {
622 defIm = imi;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800623 Slog.i(TAG, "Selected default: " + imi.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 } catch (PackageManager.NameNotFoundException ex) {
626 } catch (Resources.NotFoundException ex) {
627 }
628 }
629 }
satokd87c2592010-09-29 11:52:06 +0900630 if (defIm == null && mMethodList.size() > 0) {
satokdc9ddae2011-10-06 12:22:36 +0900631 defIm = getMostApplicableDefaultIMELocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800632 Slog.i(TAG, "No default found, using " + defIm.getId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 if (defIm != null) {
satok723a27e2010-11-11 14:58:11 +0900635 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 mSettingsObserver = new SettingsObserver(mHandler);
640 updateFromSettingsLocked();
641 }
642
643 @Override
644 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
645 throws RemoteException {
646 try {
647 return super.onTransact(code, data, reply, flags);
648 } catch (RuntimeException e) {
649 // The input method manager only throws security exceptions, so let's
650 // log all others.
651 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800652 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 }
654 throw e;
655 }
656 }
657
Dianne Hackborn661cd522011-08-22 00:26:20 -0700658 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700659 synchronized (mMethodMap) {
660 if (!mSystemReady) {
661 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700662 mKeyguardManager = (KeyguardManager)
663 mContext.getSystemService(Context.KEYGUARD_SERVICE);
664 mNotificationManager = (NotificationManager)
665 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
666 mStatusBar = statusBar;
667 statusBar.setIconVisibility("ime", false);
668 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900669 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
670 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900671 if (mShowOngoingImeSwitcherForPhones) {
672 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
673 mHardKeyboardListener);
674 }
Dianne Hackborncc278702009-09-02 23:07:23 -0700675 try {
676 startInputInnerLocked();
677 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800678 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700679 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700680 }
681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
satok15452a42011-10-28 17:58:28 +0900684 private void setImeWindowVisibilityStatusHiddenLocked() {
685 mImeWindowVis = 0;
686 updateImeWindowStatusLocked();
687 }
688
satok3afd6c02011-11-18 08:38:19 +0900689 private void refreshImeWindowVisibilityLocked() {
690 final Configuration conf = mRes.getConfiguration();
691 final boolean haveHardKeyboard = conf.keyboard
692 != Configuration.KEYBOARD_NOKEYS;
693 final boolean hardKeyShown = haveHardKeyboard
694 && conf.hardKeyboardHidden
695 != Configuration.HARDKEYBOARDHIDDEN_YES;
696 final boolean isScreenLocked = mKeyguardManager != null
697 && mKeyguardManager.isKeyguardLocked()
698 && mKeyguardManager.isKeyguardSecure();
699 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
700 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
701 updateImeWindowStatusLocked();
702 }
703
satok15452a42011-10-28 17:58:28 +0900704 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900705 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700706 }
707
satoke7c6998e2011-06-03 17:57:59 +0900708 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 public List<InputMethodInfo> getInputMethodList() {
710 synchronized (mMethodMap) {
711 return new ArrayList<InputMethodInfo>(mMethodList);
712 }
713 }
714
satoke7c6998e2011-06-03 17:57:59 +0900715 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 public List<InputMethodInfo> getEnabledInputMethodList() {
717 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900718 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720 }
721
satokbb4aa062011-01-19 21:40:27 +0900722 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
723 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
724 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
725 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
726 for (InputMethodInfo imi: getEnabledInputMethodList()) {
727 enabledInputMethodAndSubtypes.put(
728 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
729 }
730 return enabledInputMethodAndSubtypes;
731 }
732
733 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
734 boolean allowsImplicitlySelectedSubtypes) {
735 if (imi == null && mCurMethodId != null) {
736 imi = mMethodMap.get(mCurMethodId);
737 }
satok7265d9b2011-02-14 15:47:30 +0900738 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900739 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900740 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900741 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900742 }
satok7265d9b2011-02-14 15:47:30 +0900743 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900744 }
745
satoke7c6998e2011-06-03 17:57:59 +0900746 @Override
satok16331c82010-12-20 23:48:46 +0900747 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
748 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900749 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900750 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900751 }
752 }
753
satoke7c6998e2011-06-03 17:57:59 +0900754 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void addClient(IInputMethodClient client,
756 IInputContext inputContext, int uid, int pid) {
757 synchronized (mMethodMap) {
758 mClients.put(client.asBinder(), new ClientState(client,
759 inputContext, uid, pid));
760 }
761 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762
satoke7c6998e2011-06-03 17:57:59 +0900763 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 public void removeClient(IInputMethodClient client) {
765 synchronized (mMethodMap) {
766 mClients.remove(client.asBinder());
767 }
768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 void executeOrSendMessage(IInterface target, Message msg) {
771 if (target.asBinder() instanceof Binder) {
772 mCaller.sendMessage(msg);
773 } else {
774 handleMessage(msg);
775 msg.recycle();
776 }
777 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700779 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800781 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 + mCurClient.client.asBinder());
783 if (mBoundToMethod) {
784 mBoundToMethod = false;
785 if (mCurMethod != null) {
786 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
787 MSG_UNBIND_INPUT, mCurMethod));
788 }
789 }
790 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
791 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
792 mCurClient.sessionRequested = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 // Call setActive(false) on the old client
795 try {
796 mCurClient.client.setActive(false);
797 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800798 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 + mCurClient.pid + " uid " + mCurClient.uid);
800 }
801 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802
The Android Open Source Project10592532009-03-18 17:39:46 -0700803 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 private int getImeShowFlags() {
808 int flags = 0;
809 if (mShowForced) {
810 flags |= InputMethod.SHOW_FORCED
811 | InputMethod.SHOW_EXPLICIT;
812 } else if (mShowExplicitlyRequested) {
813 flags |= InputMethod.SHOW_EXPLICIT;
814 }
815 return flags;
816 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 private int getAppShowFlags() {
819 int flags = 0;
820 if (mShowForced) {
821 flags |= InputMethodManager.SHOW_FORCED;
822 } else if (!mShowExplicitlyRequested) {
823 flags |= InputMethodManager.SHOW_IMPLICIT;
824 }
825 return flags;
826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827
Dianne Hackborn7663d802012-02-24 13:08:49 -0800828 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 if (!mBoundToMethod) {
830 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
831 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
832 mBoundToMethod = true;
833 }
834 final SessionState session = mCurClient.curSession;
835 if (initial) {
836 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
837 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
838 } else {
839 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
840 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
841 }
842 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800843 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800844 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800846 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800850 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 // If no method is currently selected, do nothing.
852 if (mCurMethodId == null) {
853 return mNoBinding;
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 ClientState cs = mClients.get(client.asBinder());
857 if (cs == null) {
858 throw new IllegalArgumentException("unknown client "
859 + client.asBinder());
860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 try {
863 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
864 // Check with the window manager to make sure this client actually
865 // has a window with focus. If not, reject. This is thread safe
866 // because if the focus changes some time before or after, the
867 // next client receiving focus that has any interest in input will
868 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800869 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
871 return null;
872 }
873 } catch (RemoteException e) {
874 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800875
Dianne Hackborn7663d802012-02-24 13:08:49 -0800876 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
877 }
878
879 InputBindResult startInputUncheckedLocked(ClientState cs,
880 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
881 // If no method is currently selected, do nothing.
882 if (mCurMethodId == null) {
883 return mNoBinding;
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 if (mCurClient != cs) {
887 // If the client is changing, we need to switch over to the new
888 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700889 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800890 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 + cs.client.asBinder());
892
893 // If the screen is on, inform the new client it is active
894 if (mScreenOn) {
895 try {
896 cs.client.setActive(mScreenOn);
897 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800898 Slog.w(TAG, "Got RemoteException sending setActive notification to pid "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 + cs.pid + " uid " + cs.uid);
900 }
901 }
902 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 // Bump up the sequence for this client and attach it.
905 mCurSeq++;
906 if (mCurSeq <= 0) mCurSeq = 1;
907 mCurClient = cs;
908 mCurInputContext = inputContext;
909 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Check if the input method is changing.
912 if (mCurId != null && mCurId.equals(mCurMethodId)) {
913 if (cs.curSession != null) {
914 // Fast case: if we are already connected to the input method,
915 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -0800916 return attachNewInputLocked(
917 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919 if (mHaveConnection) {
920 if (mCurMethod != null) {
921 if (!cs.sessionRequested) {
922 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800923 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
925 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +0900926 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 // Return to client, and we will get back with it when
929 // we have had a session made for it.
930 return new InputBindResult(null, mCurId, mCurSeq);
931 } else if (SystemClock.uptimeMillis()
932 < (mLastBindTime+TIME_TO_RECONNECT)) {
933 // In this case we have connected to the service, but
934 // don't yet have its interface. If it hasn't been too
935 // long since we did the connection, we'll return to
936 // the client and wait to get the service interface so
937 // we can report back. If it has been too long, we want
938 // to fall through so we can try a disconnect/reconnect
939 // to see if we can get back in touch with the service.
940 return new InputBindResult(null, mCurId, mCurSeq);
941 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
943 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
945 }
946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700948 return startInputInnerLocked();
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700951 InputBindResult startInputInnerLocked() {
952 if (mCurMethodId == null) {
953 return mNoBinding;
954 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800955
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700956 if (!mSystemReady) {
957 // If the system is not yet ready, we shouldn't be running third
958 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -0700959 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 InputMethodInfo info = mMethodMap.get(mCurMethodId);
963 if (info == null) {
964 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800966
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700967 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
970 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700971 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
972 com.android.internal.R.string.input_method_binding_label);
973 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
974 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700975 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
976 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 mLastBindTime = SystemClock.uptimeMillis();
978 mHaveConnection = true;
979 mCurId = info.getId();
980 mCurToken = new Binder();
981 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800982 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 mIWindowManager.addWindowToken(mCurToken,
984 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
985 } catch (RemoteException e) {
986 }
987 return new InputBindResult(null, mCurId, mCurSeq);
988 } else {
989 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800990 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 + mCurIntent);
992 }
993 return null;
994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800995
satoke7c6998e2011-06-03 17:57:59 +0900996 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800998 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 synchronized (mMethodMap) {
1000 final long ident = Binder.clearCallingIdentity();
1001 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001002 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 } finally {
1004 Binder.restoreCallingIdentity(ident);
1005 }
1006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public void finishInput(IInputMethodClient client) {
1011 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001012
satoke7c6998e2011-06-03 17:57:59 +09001013 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 public void onServiceConnected(ComponentName name, IBinder service) {
1015 synchronized (mMethodMap) {
1016 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1017 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001018 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001019 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001020 unbindCurrentMethodLocked(false);
1021 return;
1022 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001023 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001024 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1025 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001027 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001028 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001030 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001031 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
1033 }
1034 }
1035 }
1036
1037 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1038 synchronized (mMethodMap) {
1039 if (mCurMethod != null && method != null
1040 && mCurMethod.asBinder() == method.asBinder()) {
1041 if (mCurClient != null) {
1042 mCurClient.curSession = new SessionState(mCurClient,
1043 method, session);
1044 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001045 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (res.method != null) {
1047 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1048 MSG_BIND_METHOD, mCurClient.client, res));
1049 }
1050 }
1051 }
1052 }
1053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001054
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001055 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001056 if (mVisibleBound) {
1057 mContext.unbindService(mVisibleConnection);
1058 mVisibleBound = false;
1059 }
1060
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001061 if (mHaveConnection) {
1062 mContext.unbindService(this);
1063 mHaveConnection = false;
1064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001065
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001066 if (mCurToken != null) {
1067 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001068 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001069 mIWindowManager.removeWindowToken(mCurToken);
1070 } catch (RemoteException e) {
1071 }
1072 mCurToken = null;
1073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
The Android Open Source Project10592532009-03-18 17:39:46 -07001075 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001076 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001078 if (reportToClient && mCurClient != null) {
1079 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1080 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1081 }
1082 }
1083
Devin Taylor0c33ed22010-02-23 13:26:46 -06001084 private void finishSession(SessionState sessionState) {
1085 if (sessionState != null && sessionState.session != null) {
1086 try {
1087 sessionState.session.finishSession();
1088 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001089 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001090 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001091 }
1092 }
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001095 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 if (mCurMethod != null) {
1097 for (ClientState cs : mClients.values()) {
1098 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001099 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 cs.curSession = null;
1101 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001102
1103 finishSession(mEnabledSession);
1104 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 mCurMethod = null;
1106 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001107 if (mStatusBar != null) {
1108 mStatusBar.setIconVisibility("ime", false);
1109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001111
satoke7c6998e2011-06-03 17:57:59 +09001112 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 public void onServiceDisconnected(ComponentName name) {
1114 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 + " mCurIntent=" + mCurIntent);
1117 if (mCurMethod != null && mCurIntent != null
1118 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001119 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // We consider this to be a new bind attempt, since the system
1121 // should now try to restart the service for us.
1122 mLastBindTime = SystemClock.uptimeMillis();
1123 mShowRequested = mInputShown;
1124 mInputShown = false;
1125 if (mCurClient != null) {
1126 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1127 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1128 }
1129 }
1130 }
1131 }
1132
satokf9f01002011-05-19 21:31:50 +09001133 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001135 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 long ident = Binder.clearCallingIdentity();
1137 try {
1138 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001139 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 return;
1141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 synchronized (mMethodMap) {
1144 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001145 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001146 if (mStatusBar != null) {
1147 mStatusBar.setIconVisibility("ime", false);
1148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001150 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001151 CharSequence contentDescription = null;
1152 try {
1153 PackageManager packageManager = mContext.getPackageManager();
1154 contentDescription = packageManager.getApplicationLabel(
1155 packageManager.getApplicationInfo(packageName, 0));
1156 } catch (NameNotFoundException nnfe) {
1157 /* ignore */
1158 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001159 if (mStatusBar != null) {
1160 mStatusBar.setIcon("ime", packageName, iconId, 0,
1161 contentDescription != null
1162 ? contentDescription.toString() : null);
1163 mStatusBar.setIconVisibility("ime", true);
1164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 }
1167 } finally {
1168 Binder.restoreCallingIdentity(ident);
1169 }
1170 }
1171
satok7cfc0ed2011-06-20 21:29:36 +09001172 private boolean needsToShowImeSwitchOngoingNotification() {
1173 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001174 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001175 synchronized (mMethodMap) {
1176 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1177 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001178 if (N > 2) return true;
1179 if (N < 1) return false;
1180 int nonAuxCount = 0;
1181 int auxCount = 0;
1182 InputMethodSubtype nonAuxSubtype = null;
1183 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001184 for(int i = 0; i < N; ++i) {
1185 final InputMethodInfo imi = imis.get(i);
1186 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1187 imi, true);
1188 final int subtypeCount = subtypes.size();
1189 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001190 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001191 } else {
1192 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001193 final InputMethodSubtype subtype = subtypes.get(j);
1194 if (!subtype.isAuxiliary()) {
1195 ++nonAuxCount;
1196 nonAuxSubtype = subtype;
1197 } else {
1198 ++auxCount;
1199 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001200 }
1201 }
1202 }
satok7cfc0ed2011-06-20 21:29:36 +09001203 }
satokb6359412011-06-28 17:47:41 +09001204 if (nonAuxCount > 1 || auxCount > 1) {
1205 return true;
1206 } else if (nonAuxCount == 1 && auxCount == 1) {
1207 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001208 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1209 || auxSubtype.overridesImplicitlyEnabledSubtype()
1210 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001211 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1212 return false;
1213 }
1214 return true;
1215 }
1216 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001217 }
satok7cfc0ed2011-06-20 21:29:36 +09001218 }
1219
satokdbf29502011-08-25 15:28:23 +09001220 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001221 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001222 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001223 int uid = Binder.getCallingUid();
1224 long ident = Binder.clearCallingIdentity();
1225 try {
1226 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001227 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001228 return;
1229 }
1230
1231 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001232 mImeWindowVis = vis;
1233 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001234 if (mStatusBar != null) {
1235 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1236 }
satok7cfc0ed2011-06-20 21:29:36 +09001237 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001238 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1239 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001240 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001241 final CharSequence title = mRes.getText(
1242 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001243 final CharSequence imiLabel = imi.loadLabel(pm);
1244 final CharSequence summary = mCurrentSubtype != null
1245 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1246 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1247 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001248 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001249 : imiLabel;
1250
satok7cfc0ed2011-06-20 21:29:36 +09001251 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001252 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001253 if (mNotificationManager != null) {
1254 mNotificationManager.notify(
1255 com.android.internal.R.string.select_input_method,
1256 mImeSwitcherNotification);
1257 mNotificationShown = true;
1258 }
satok7cfc0ed2011-06-20 21:29:36 +09001259 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001260 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001261 mNotificationManager.cancel(
1262 com.android.internal.R.string.select_input_method);
1263 mNotificationShown = false;
1264 }
1265 }
satok06487a52010-10-29 11:37:18 +09001266 }
1267 } finally {
1268 Binder.restoreCallingIdentity(ident);
1269 }
1270 }
1271
satoke7c6998e2011-06-03 17:57:59 +09001272 @Override
satokf9f01002011-05-19 21:31:50 +09001273 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1274 synchronized (mMethodMap) {
1275 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1276 for (int i = 0; i < spans.length; ++i) {
1277 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001278 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001279 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001280 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001281 }
1282 }
1283 }
1284 }
1285
satoke7c6998e2011-06-03 17:57:59 +09001286 @Override
satokf9f01002011-05-19 21:31:50 +09001287 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1288 synchronized (mMethodMap) {
1289 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1290 // TODO: Do not send the intent if the process of the targetImi is already dead.
1291 if (targetImi != null) {
1292 final String[] suggestions = span.getSuggestions();
1293 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001294 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001295 final Intent intent = new Intent();
1296 // Ensures that only a class in the original IME package will receive the
1297 // notification.
satok42c5a162011-05-26 16:46:14 +09001298 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001299 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1300 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1301 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1302 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1303 mContext.sendBroadcast(intent);
1304 return true;
1305 }
1306 }
1307 return false;
1308 }
1309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001311 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1312 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1313 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1314 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001316 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001317 // There is no input method selected, try to choose new applicable input method.
1318 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1319 id = Settings.Secure.getString(mContext.getContentResolver(),
1320 Settings.Secure.DEFAULT_INPUT_METHOD);
1321 }
1322 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 try {
satokab751aa2010-09-14 19:17:36 +09001324 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001326 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001327 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001328 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
satokf3db1af2010-11-23 13:34:33 +09001330 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001331 } else {
1332 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001333 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001334 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001337
satokab751aa2010-09-14 19:17:36 +09001338 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 InputMethodInfo info = mMethodMap.get(id);
1340 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001341 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 if (id.equals(mCurMethodId)) {
satokcd7cd292010-11-20 15:46:23 +09001345 InputMethodSubtype subtype = null;
Ken Wakasa586f0512011-01-20 22:31:01 +09001346 if (subtypeId >= 0 && subtypeId < info.getSubtypeCount()) {
1347 subtype = info.getSubtypeAt(subtypeId);
satokcd7cd292010-11-20 15:46:23 +09001348 }
1349 if (subtype != mCurrentSubtype) {
1350 synchronized (mMethodMap) {
satokca830212011-01-13 21:15:04 +09001351 if (subtype != null) {
1352 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1353 }
satokcd7cd292010-11-20 15:46:23 +09001354 if (mCurMethod != null) {
1355 try {
satok3afd6c02011-11-18 08:38:19 +09001356 refreshImeWindowVisibilityLocked();
satokcd7cd292010-11-20 15:46:23 +09001357 // If subtype is null, try to find the most applicable one from
1358 // getCurrentInputMethodSubtype.
1359 if (subtype == null) {
1360 subtype = getCurrentInputMethodSubtype();
1361 }
1362 mCurMethod.changeInputMethodSubtype(subtype);
1363 } catch (RemoteException e) {
1364 return;
satokab751aa2010-09-14 19:17:36 +09001365 }
1366 }
1367 }
1368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 return;
1370 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 final long ident = Binder.clearCallingIdentity();
1373 try {
satokab751aa2010-09-14 19:17:36 +09001374 // Set a subtype to this input method.
1375 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001376 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1377 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1378 // because mCurMethodId is stored as a history in
1379 // setSelectedInputMethodAndSubtypeLocked().
1380 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 if (ActivityManagerNative.isSystemReady()) {
1383 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001384 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 intent.putExtra("input_method_id", id);
1386 mContext.sendBroadcast(intent);
1387 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001388 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 } finally {
1390 Binder.restoreCallingIdentity(ident);
1391 }
1392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001393
satok42c5a162011-05-26 16:46:14 +09001394 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001395 public boolean showSoftInput(IInputMethodClient client, int flags,
1396 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001397 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 long ident = Binder.clearCallingIdentity();
1399 try {
1400 synchronized (mMethodMap) {
1401 if (mCurClient == null || client == null
1402 || mCurClient.client.asBinder() != client.asBinder()) {
1403 try {
1404 // We need to check if this is the current client with
1405 // focus in the window manager, to allow this call to
1406 // be made before input is started in it.
1407 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001408 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001409 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 }
1411 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001412 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
Joe Onorato8a9b2202010-02-26 18:56:32 -08001416 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001417 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419 } finally {
1420 Binder.restoreCallingIdentity(ident);
1421 }
1422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001423
The Android Open Source Project4df24232009-03-05 14:34:35 -08001424 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 mShowRequested = true;
1426 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1427 mShowExplicitlyRequested = true;
1428 }
1429 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1430 mShowExplicitlyRequested = true;
1431 mShowForced = true;
1432 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001433
Dianne Hackborncc278702009-09-02 23:07:23 -07001434 if (!mSystemReady) {
1435 return false;
1436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
The Android Open Source Project4df24232009-03-05 14:34:35 -08001438 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001440 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1441 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1442 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001444 if (mHaveConnection && !mVisibleBound) {
1445 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1446 mVisibleBound = true;
1447 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001448 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001450 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 // The client has asked to have the input method shown, but
1452 // we have been sitting here too long with a connection to the
1453 // service and no interface received, so let's disconnect/connect
1454 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001455 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001457 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001459 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1460 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
The Android Open Source Project4df24232009-03-05 14:34:35 -08001463 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001465
satok42c5a162011-05-26 16:46:14 +09001466 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001467 public boolean hideSoftInput(IInputMethodClient client, int flags,
1468 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001469 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 long ident = Binder.clearCallingIdentity();
1471 try {
1472 synchronized (mMethodMap) {
1473 if (mCurClient == null || client == null
1474 || mCurClient.client.asBinder() != client.asBinder()) {
1475 try {
1476 // We need to check if this is the current client with
1477 // focus in the window manager, to allow this call to
1478 // be made before input is started in it.
1479 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001480 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1481 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001482 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001483 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001486 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001487 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001492 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 }
1494 } finally {
1495 Binder.restoreCallingIdentity(ident);
1496 }
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project4df24232009-03-05 14:34:35 -08001499 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1501 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001502 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001504 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001507 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001509 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001511 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001513 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1514 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1515 res = true;
1516 } else {
1517 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001519 if (mHaveConnection && mVisibleBound) {
1520 mContext.unbindService(mVisibleConnection);
1521 mVisibleBound = false;
1522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 mInputShown = false;
1524 mShowRequested = false;
1525 mShowExplicitlyRequested = false;
1526 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001527 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001529
satok42c5a162011-05-26 16:46:14 +09001530 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001531 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1532 int controlFlags, int softInputMode, int windowFlags,
1533 EditorInfo attribute, IInputContext inputContext) {
1534 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 long ident = Binder.clearCallingIdentity();
1536 try {
1537 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001538 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001539 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001541 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001542
Dianne Hackborn7663d802012-02-24 13:08:49 -08001543 ClientState cs = mClients.get(client.asBinder());
1544 if (cs == null) {
1545 throw new IllegalArgumentException("unknown client "
1546 + client.asBinder());
1547 }
1548
1549 try {
1550 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1551 // Check with the window manager to make sure this client actually
1552 // has a window with focus. If not, reject. This is thread safe
1553 // because if the focus changes some time before or after, the
1554 // next client receiving focus that has any interest in input will
1555 // be calling through here after that change happens.
1556 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1557 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1558 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001560 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001562
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001563 if (mCurFocusedWindow == windowToken) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001564 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001565 if (attribute != null) {
1566 return startInputUncheckedLocked(cs, inputContext, attribute,
1567 controlFlags);
1568 }
1569 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001570 }
1571 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001572
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001573 // Should we auto-show the IME even if the caller has not
1574 // specified what should be done with it?
1575 // We only do this automatically if the window can resize
1576 // to accommodate the IME (so what the user sees will give
1577 // them good context without input information being obscured
1578 // by the IME) or if running on a large screen where there
1579 // is more room for the target window + IME.
1580 final boolean doAutoShow =
1581 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1582 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1583 || mRes.getConfiguration().isLayoutSizeAtLeast(
1584 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001585 final boolean isTextEditor =
1586 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1587
1588 // We want to start input before showing the IME, but after closing
1589 // it. We want to do this after closing it to help the IME disappear
1590 // more quickly (not get stuck behind it initializing itself for the
1591 // new focused input, even if its window wants to hide the IME).
1592 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1595 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001596 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1598 // There is no focus view, and this window will
1599 // be behind any soft input window, so hide the
1600 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001601 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001602 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001604 } else if (isTextEditor && doAutoShow && (softInputMode &
1605 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 // There is a focus view, and we are navigating forward
1607 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001608 // We only do this automatically if the window can resize
1609 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001610 // them good context without input information being obscured
1611 // by the IME) or if running on a large screen where there
1612 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001613 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001614 if (attribute != null) {
1615 res = startInputUncheckedLocked(cs, inputContext, attribute,
1616 controlFlags);
1617 didStart = true;
1618 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001619 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
1621 break;
1622 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1623 // Do nothing.
1624 break;
1625 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1626 if ((softInputMode &
1627 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001628 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001629 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631 break;
1632 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001633 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001634 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 break;
1636 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1637 if ((softInputMode &
1638 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001639 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001640 if (attribute != null) {
1641 res = startInputUncheckedLocked(cs, inputContext, attribute,
1642 controlFlags);
1643 didStart = true;
1644 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001645 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647 break;
1648 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001649 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001650 if (attribute != null) {
1651 res = startInputUncheckedLocked(cs, inputContext, attribute,
1652 controlFlags);
1653 didStart = true;
1654 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001655 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 break;
1657 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001658
1659 if (!didStart && attribute != null) {
1660 res = startInputUncheckedLocked(cs, inputContext, attribute,
1661 controlFlags);
1662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664 } finally {
1665 Binder.restoreCallingIdentity(ident);
1666 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001667
1668 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001670
satok42c5a162011-05-26 16:46:14 +09001671 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1673 synchronized (mMethodMap) {
1674 if (mCurClient == null || client == null
1675 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001676 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001677 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
1679
satok440aab52010-11-25 09:43:11 +09001680 // Always call subtype picker, because subtype picker is a superset of input method
1681 // picker.
satokab751aa2010-09-14 19:17:36 +09001682 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1683 }
1684 }
1685
satok42c5a162011-05-26 16:46:14 +09001686 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001688 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1689 }
1690
satok42c5a162011-05-26 16:46:14 +09001691 @Override
satok28203512010-11-24 11:06:49 +09001692 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1693 synchronized (mMethodMap) {
1694 if (subtype != null) {
1695 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1696 mMethodMap.get(id), subtype.hashCode()));
1697 } else {
1698 setInputMethod(token, id);
1699 }
1700 }
satokab751aa2010-09-14 19:17:36 +09001701 }
1702
satok42c5a162011-05-26 16:46:14 +09001703 @Override
satokb416a712010-11-25 20:42:14 +09001704 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001705 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001706 synchronized (mMethodMap) {
1707 if (mCurClient == null || client == null
1708 || mCurClient.client.asBinder() != client.asBinder()) {
1709 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1710 }
satok7fee71f2010-12-17 18:54:26 +09001711 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1712 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001713 }
1714 }
1715
satok4fc87d62011-05-20 16:13:43 +09001716 @Override
satok735cf382010-11-11 20:40:09 +09001717 public boolean switchToLastInputMethod(IBinder token) {
1718 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001719 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001720 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001721 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001722 lastImi = mMethodMap.get(lastIme.first);
1723 } else {
1724 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001725 }
satok4fc87d62011-05-20 16:13:43 +09001726 String targetLastImiId = null;
1727 int subtypeId = NOT_A_SUBTYPE_ID;
1728 if (lastIme != null && lastImi != null) {
1729 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1730 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1731 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1732 : mCurrentSubtype.hashCode();
1733 // If the last IME is the same as the current IME and the last subtype is not
1734 // defined, there is no need to switch to the last IME.
1735 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1736 targetLastImiId = lastIme.first;
1737 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1738 }
1739 }
1740
1741 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1742 // This is a safety net. If the currentSubtype can't be added to the history
1743 // and the framework couldn't find the last ime, we will make the last ime be
1744 // the most applicable enabled keyboard subtype of the system imes.
1745 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1746 if (enabled != null) {
1747 final int N = enabled.size();
1748 final String locale = mCurrentSubtype == null
1749 ? mRes.getConfiguration().locale.toString()
1750 : mCurrentSubtype.getLocale();
1751 for (int i = 0; i < N; ++i) {
1752 final InputMethodInfo imi = enabled.get(i);
1753 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1754 InputMethodSubtype keyboardSubtype =
1755 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1756 SUBTYPE_MODE_KEYBOARD, locale, true);
1757 if (keyboardSubtype != null) {
1758 targetLastImiId = imi.getId();
1759 subtypeId = getSubtypeIdFromHashCode(
1760 imi, keyboardSubtype.hashCode());
1761 if(keyboardSubtype.getLocale().equals(locale)) {
1762 break;
1763 }
1764 }
1765 }
1766 }
1767 }
1768 }
1769
1770 if (!TextUtils.isEmpty(targetLastImiId)) {
1771 if (DEBUG) {
1772 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1773 + ", from: " + mCurMethodId + ", " + subtypeId);
1774 }
1775 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1776 return true;
1777 } else {
1778 return false;
1779 }
satok735cf382010-11-11 20:40:09 +09001780 }
1781 }
1782
satoke7c6998e2011-06-03 17:57:59 +09001783 @Override
satok688bd472012-02-09 20:09:17 +09001784 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1785 synchronized (mMethodMap) {
1786 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1787 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1788 if (nextSubtype == null) {
1789 return false;
1790 }
1791 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1792 return true;
1793 }
1794 }
1795
1796 @Override
satok68f1b782011-04-11 14:26:04 +09001797 public InputMethodSubtype getLastInputMethodSubtype() {
1798 synchronized (mMethodMap) {
1799 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1800 // TODO: Handle the case of the last IME with no subtypes
1801 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1802 || TextUtils.isEmpty(lastIme.second)) return null;
1803 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1804 if (lastImi == null) return null;
1805 try {
1806 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001807 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1808 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1809 return null;
1810 }
1811 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001812 } catch (NumberFormatException e) {
1813 return null;
1814 }
1815 }
1816 }
1817
satoke7c6998e2011-06-03 17:57:59 +09001818 @Override
satokee5e77c2011-09-02 18:50:15 +09001819 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001820 // By this IPC call, only a process which shares the same uid with the IME can add
1821 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001822 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001823 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001824 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001825 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001826 final PackageManager pm = mContext.getPackageManager();
1827 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1828 if (packageInfos != null) {
1829 final int packageNum = packageInfos.length;
1830 for (int i = 0; i < packageNum; ++i) {
1831 if (packageInfos[i].equals(imi.getPackageName())) {
1832 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001833 final long ident = Binder.clearCallingIdentity();
1834 try {
1835 buildInputMethodListLocked(mMethodList, mMethodMap);
1836 } finally {
1837 Binder.restoreCallingIdentity(ident);
1838 }
satokee5e77c2011-09-02 18:50:15 +09001839 return;
satok91e88122011-07-18 11:11:42 +09001840 }
1841 }
1842 }
satoke7c6998e2011-06-03 17:57:59 +09001843 }
satokee5e77c2011-09-02 18:50:15 +09001844 return;
satoke7c6998e2011-06-03 17:57:59 +09001845 }
1846
satok28203512010-11-24 11:06:49 +09001847 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 synchronized (mMethodMap) {
1849 if (token == null) {
1850 if (mContext.checkCallingOrSelfPermission(
1851 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1852 != PackageManager.PERMISSION_GRANTED) {
1853 throw new SecurityException(
1854 "Using null token requires permission "
1855 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1856 }
1857 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001858 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1859 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 return;
1861 }
1862
satokc5933802011-08-31 21:26:04 +09001863 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 try {
satokab751aa2010-09-14 19:17:36 +09001865 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 } finally {
1867 Binder.restoreCallingIdentity(ident);
1868 }
1869 }
1870 }
1871
satok42c5a162011-05-26 16:46:14 +09001872 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 public void hideMySoftInput(IBinder token, int flags) {
1874 synchronized (mMethodMap) {
1875 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001876 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1877 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 return;
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 long ident = Binder.clearCallingIdentity();
1881 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001882 hideCurrentInputLocked(flags, null);
1883 } finally {
1884 Binder.restoreCallingIdentity(ident);
1885 }
1886 }
1887 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001888
satok42c5a162011-05-26 16:46:14 +09001889 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001890 public void showMySoftInput(IBinder token, int flags) {
1891 synchronized (mMethodMap) {
1892 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001893 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1894 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001895 return;
1896 }
1897 long ident = Binder.clearCallingIdentity();
1898 try {
1899 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 } finally {
1901 Binder.restoreCallingIdentity(ident);
1902 }
1903 }
1904 }
1905
1906 void setEnabledSessionInMainThread(SessionState session) {
1907 if (mEnabledSession != session) {
1908 if (mEnabledSession != null) {
1909 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 mEnabledSession.method.setSessionEnabled(
1912 mEnabledSession.session, false);
1913 } catch (RemoteException e) {
1914 }
1915 }
1916 mEnabledSession = session;
1917 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001918 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 session.method.setSessionEnabled(
1920 session.session, true);
1921 } catch (RemoteException e) {
1922 }
1923 }
1924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001925
satok42c5a162011-05-26 16:46:14 +09001926 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 public boolean handleMessage(Message msg) {
1928 HandlerCaller.SomeArgs args;
1929 switch (msg.what) {
1930 case MSG_SHOW_IM_PICKER:
1931 showInputMethodMenu();
1932 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001933
satokab751aa2010-09-14 19:17:36 +09001934 case MSG_SHOW_IM_SUBTYPE_PICKER:
1935 showInputMethodSubtypeMenu();
1936 return true;
1937
satok47a44912010-10-06 16:03:58 +09001938 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09001939 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09001940 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09001941 return true;
1942
1943 case MSG_SHOW_IM_CONFIG:
1944 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09001945 return true;
1946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 case MSG_UNBIND_INPUT:
1950 try {
1951 ((IInputMethod)msg.obj).unbindInput();
1952 } catch (RemoteException e) {
1953 // There is nothing interesting about the method dying.
1954 }
1955 return true;
1956 case MSG_BIND_INPUT:
1957 args = (HandlerCaller.SomeArgs)msg.obj;
1958 try {
1959 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
1960 } catch (RemoteException e) {
1961 }
1962 return true;
1963 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001964 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001966 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
1967 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 } catch (RemoteException e) {
1969 }
1970 return true;
1971 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08001972 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001974 ((IInputMethod)args.arg1).hideSoftInput(0,
1975 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 } catch (RemoteException e) {
1977 }
1978 return true;
1979 case MSG_ATTACH_TOKEN:
1980 args = (HandlerCaller.SomeArgs)msg.obj;
1981 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001982 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
1984 } catch (RemoteException e) {
1985 }
1986 return true;
1987 case MSG_CREATE_SESSION:
1988 args = (HandlerCaller.SomeArgs)msg.obj;
1989 try {
1990 ((IInputMethod)args.arg1).createSession(
1991 (IInputMethodCallback)args.arg2);
1992 } catch (RemoteException e) {
1993 }
1994 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 case MSG_START_INPUT:
1998 args = (HandlerCaller.SomeArgs)msg.obj;
1999 try {
2000 SessionState session = (SessionState)args.arg1;
2001 setEnabledSessionInMainThread(session);
2002 session.method.startInput((IInputContext)args.arg2,
2003 (EditorInfo)args.arg3);
2004 } catch (RemoteException e) {
2005 }
2006 return true;
2007 case MSG_RESTART_INPUT:
2008 args = (HandlerCaller.SomeArgs)msg.obj;
2009 try {
2010 SessionState session = (SessionState)args.arg1;
2011 setEnabledSessionInMainThread(session);
2012 session.method.restartInput((IInputContext)args.arg2,
2013 (EditorInfo)args.arg3);
2014 } catch (RemoteException e) {
2015 }
2016 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 case MSG_UNBIND_METHOD:
2021 try {
2022 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2023 } catch (RemoteException e) {
2024 // There is nothing interesting about the last client dying.
2025 }
2026 return true;
2027 case MSG_BIND_METHOD:
2028 args = (HandlerCaller.SomeArgs)msg.obj;
2029 try {
2030 ((IInputMethodClient)args.arg1).onBindMethod(
2031 (InputBindResult)args.arg2);
2032 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002033 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035 return true;
satok01038492012-04-09 21:08:27 +09002036
2037 // --------------------------------------------------------------
2038 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2039 mHardKeyboardListener.handleHardKeyboardStatusChange(
2040 msg.arg1 == 1, msg.arg2 == 1);
2041 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 return false;
2044 }
2045
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002046 private boolean isSystemIme(InputMethodInfo inputMethod) {
2047 return (inputMethod.getServiceInfo().applicationInfo.flags
2048 & ApplicationInfo.FLAG_SYSTEM) != 0;
2049 }
2050
Ken Wakasa586f0512011-01-20 22:31:01 +09002051 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2052 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2053 final int subtypeCount = imi.getSubtypeCount();
2054 for (int i = 0; i < subtypeCount; ++i) {
2055 subtypes.add(imi.getSubtypeAt(i));
2056 }
2057 return subtypes;
2058 }
2059
satoka86f5e42011-09-02 17:12:42 +09002060
2061 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2062 InputMethodInfo imi, String mode) {
2063 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2064 final int subtypeCount = imi.getSubtypeCount();
2065 for (int i = 0; i < subtypeCount; ++i) {
2066 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2067 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2068 subtypes.add(subtype);
2069 }
2070 }
2071 return subtypes;
2072 }
2073
satokdc9ddae2011-10-06 12:22:36 +09002074 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002075 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002076 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002077 // We'd prefer to fall back on a system IME, since that is safer.
2078 int i=enabled.size();
2079 while (i > 0) {
2080 i--;
satokdc9ddae2011-10-06 12:22:36 +09002081 final InputMethodInfo imi = enabled.get(i);
2082 if (isSystemIme(imi) && !imi.isAuxiliaryIme()) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002083 break;
2084 }
2085 }
satokdc9ddae2011-10-06 12:22:36 +09002086 return enabled.get(i);
2087 }
2088 return null;
2089 }
2090
2091 private boolean chooseNewDefaultIMELocked() {
2092 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2093 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002094 if (DEBUG) {
2095 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2096 }
satok723a27e2010-11-11 14:58:11 +09002097 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002098 return true;
2099 }
2100
2101 return false;
2102 }
2103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2105 HashMap<String, InputMethodInfo> map) {
2106 list.clear();
2107 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002110 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002111 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2112 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2113 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2114 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 List<ResolveInfo> services = pm.queryIntentServices(
2117 new Intent(InputMethod.SERVICE_INTERFACE),
2118 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002119
satoke7c6998e2011-06-03 17:57:59 +09002120 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2121 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 for (int i = 0; i < services.size(); ++i) {
2123 ResolveInfo ri = services.get(i);
2124 ServiceInfo si = ri.serviceInfo;
2125 ComponentName compName = new ComponentName(si.packageName, si.name);
2126 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2127 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002128 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 + ": it does not require the permission "
2130 + android.Manifest.permission.BIND_INPUT_METHOD);
2131 continue;
2132 }
2133
Joe Onorato8a9b2202010-02-26 18:56:32 -08002134 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135
2136 try {
satoke7c6998e2011-06-03 17:57:59 +09002137 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002139 final String id = p.getId();
2140 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141
Amith Yamasanie861ec12010-03-24 21:39:27 -07002142 // System IMEs are enabled by default, unless there's a hard keyboard
2143 // and the system IME was explicitly disabled
2144 if (isSystemIme(p) && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
2145 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002146 }
2147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002149 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002153 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002155 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 }
2157 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002158
2159 String defaultIme = Settings.Secure.getString(mContext
2160 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok913a8922010-08-26 21:53:41 +09002161 if (!TextUtils.isEmpty(defaultIme) && !map.containsKey(defaultIme)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002162 if (chooseNewDefaultIMELocked()) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002163 updateFromSettingsLocked();
2164 }
2165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002169
satokab751aa2010-09-14 19:17:36 +09002170 private void showInputMethodMenu() {
2171 showInputMethodMenuInternal(false);
2172 }
2173
2174 private void showInputMethodSubtypeMenu() {
2175 showInputMethodMenuInternal(true);
2176 }
2177
satok217f5482010-12-15 05:19:19 +09002178 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002179 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002180 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002181 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2182 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002183 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002184 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002185 }
satok217f5482010-12-15 05:19:19 +09002186 mContext.startActivity(intent);
2187 }
2188
2189 private void showConfigureInputMethods() {
2190 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2192 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2193 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002194 mContext.startActivity(intent);
2195 }
2196
satok2c93efc2012-04-02 19:33:47 +09002197 private boolean isScreenLocked() {
2198 return mKeyguardManager != null
2199 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2200 }
satokab751aa2010-09-14 19:17:36 +09002201 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002202 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002206 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002207
satok2c93efc2012-04-02 19:33:47 +09002208 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002210 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002211 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002212
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002213 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002214 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2215 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002216 if (immis == null || immis.size() == 0) {
2217 return;
2218 }
2219
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002220 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
satok688bd472012-02-09 20:09:17 +09002222 final List<ImeSubtypeListItem> imList =
2223 mImListManager.getSortedInputMethodAndSubtypeList(
2224 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002225
satokc3690562012-01-10 20:14:43 +09002226 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2227 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2228 if (currentSubtype != null) {
2229 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2230 lastInputMethodSubtypeId =
2231 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2232 }
2233 }
2234
Ken Wakasa761eb372011-03-04 19:06:18 +09002235 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002236 mIms = new InputMethodInfo[N];
2237 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002238 int checkedItem = 0;
2239 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002240 final ImeSubtypeListItem item = imList.get(i);
2241 mIms[i] = item.mImi;
2242 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002243 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002244 int subtypeId = mSubtypeIds[i];
2245 if ((subtypeId == NOT_A_SUBTYPE_ID)
2246 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2247 || (subtypeId == lastInputMethodSubtypeId)) {
2248 checkedItem = i;
2249 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002252 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002253 com.android.internal.R.styleable.DialogPreference,
2254 com.android.internal.R.attr.alertDialogStyle, 0);
2255 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002256 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002257 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002258 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002260 }
2261 })
2262 .setIcon(a.getDrawable(
2263 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2264 a.recycle();
satok01038492012-04-09 21:08:27 +09002265 final LayoutInflater inflater =
2266 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2267 final View tv = inflater.inflate(
2268 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2269 mDialogBuilder.setCustomTitle(tv);
2270
2271 // Setup layout for a toggle switch of the hardware keyboard
2272 mSwitchingDialogTitleView = tv;
2273 mSwitchingDialogTitleView.findViewById(
2274 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2275 mWindowManagerService.isHardKeyboardAvailable() ?
2276 View.VISIBLE : View.GONE);
2277 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2278 com.android.internal.R.id.hard_keyboard_switch));
2279 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2280 hardKeySwitch.setOnCheckedChangeListener(
2281 new OnCheckedChangeListener() {
2282 @Override
2283 public void onCheckedChanged(
2284 CompoundButton buttonView, boolean isChecked) {
2285 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2286 }
2287 });
satokd87c2592010-09-29 11:52:06 +09002288
Ken Wakasa05dbb652011-08-22 15:22:43 +09002289 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2290 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2291 checkedItem);
2292
2293 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002294 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002295 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002296 public void onClick(DialogInterface dialog, int which) {
2297 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002298 if (mIms == null || mIms.length <= which
2299 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002300 return;
2301 }
2302 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002303 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002304 hideInputMethodMenu();
2305 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002306 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002307 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002308 subtypeId = NOT_A_SUBTYPE_ID;
2309 }
2310 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002311 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002314 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315
satokbc81b692011-08-26 16:22:22 +09002316 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002317 mDialogBuilder.setPositiveButton(
2318 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002319 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002320 @Override
satok7f35c8c2010-10-07 21:13:11 +09002321 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002322 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002323 }
2324 });
2325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002327 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 mSwitchingDialog.getWindow().setType(
2329 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002330 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 mSwitchingDialog.show();
2332 }
2333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002334
Ken Wakasa05dbb652011-08-22 15:22:43 +09002335 private static class ImeSubtypeListItem {
2336 public final CharSequence mImeName;
2337 public final CharSequence mSubtypeName;
2338 public final InputMethodInfo mImi;
2339 public final int mSubtypeId;
2340 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
2341 InputMethodInfo imi, int subtypeId) {
2342 mImeName = imeName;
2343 mSubtypeName = subtypeName;
2344 mImi = imi;
2345 mSubtypeId = subtypeId;
2346 }
2347 }
2348
2349 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2350 private final LayoutInflater mInflater;
2351 private final int mTextViewResourceId;
2352 private final List<ImeSubtypeListItem> mItemsList;
2353 private final int mCheckedItem;
2354 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2355 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2356 super(context, textViewResourceId, itemsList);
2357 mTextViewResourceId = textViewResourceId;
2358 mItemsList = itemsList;
2359 mCheckedItem = checkedItem;
2360 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2361 }
2362
2363 @Override
2364 public View getView(int position, View convertView, ViewGroup parent) {
2365 final View view = convertView != null ? convertView
2366 : mInflater.inflate(mTextViewResourceId, null);
2367 if (position < 0 || position >= mItemsList.size()) return view;
2368 final ImeSubtypeListItem item = mItemsList.get(position);
2369 final CharSequence imeName = item.mImeName;
2370 final CharSequence subtypeName = item.mSubtypeName;
2371 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2372 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2373 if (TextUtils.isEmpty(subtypeName)) {
2374 firstTextView.setText(imeName);
2375 secondTextView.setVisibility(View.GONE);
2376 } else {
2377 firstTextView.setText(subtypeName);
2378 secondTextView.setText(imeName);
2379 secondTextView.setVisibility(View.VISIBLE);
2380 }
2381 final RadioButton radioButton =
2382 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2383 radioButton.setChecked(position == mCheckedItem);
2384 return view;
2385 }
2386 }
2387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002389 synchronized (mMethodMap) {
2390 hideInputMethodMenuLocked();
2391 }
2392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002393
The Android Open Source Project10592532009-03-18 17:39:46 -07002394 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002395 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396
The Android Open Source Project10592532009-03-18 17:39:46 -07002397 if (mSwitchingDialog != null) {
2398 mSwitchingDialog.dismiss();
2399 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002401
The Android Open Source Project10592532009-03-18 17:39:46 -07002402 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002403 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
satok42c5a162011-05-26 16:46:14 +09002408 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 public boolean setInputMethodEnabled(String id, boolean enabled) {
2410 synchronized (mMethodMap) {
2411 if (mContext.checkCallingOrSelfPermission(
2412 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2413 != PackageManager.PERMISSION_GRANTED) {
2414 throw new SecurityException(
2415 "Requires permission "
2416 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2417 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 long ident = Binder.clearCallingIdentity();
2420 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002421 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 } finally {
2423 Binder.restoreCallingIdentity(ident);
2424 }
2425 }
2426 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002427
2428 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2429 // Make sure this is a valid input method.
2430 InputMethodInfo imm = mMethodMap.get(id);
2431 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002432 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002433 }
2434
satokd87c2592010-09-29 11:52:06 +09002435 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2436 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002437
satokd87c2592010-09-29 11:52:06 +09002438 if (enabled) {
2439 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2440 if (pair.first.equals(id)) {
2441 // We are enabling this input method, but it is already enabled.
2442 // Nothing to do. The previous state was enabled.
2443 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002444 }
2445 }
satokd87c2592010-09-29 11:52:06 +09002446 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2447 // Previous state was disabled.
2448 return false;
2449 } else {
2450 StringBuilder builder = new StringBuilder();
2451 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2452 builder, enabledInputMethodsList, id)) {
2453 // Disabled input method is currently selected, switch to another one.
2454 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2455 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002456 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2457 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2458 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002459 }
2460 // Previous state was enabled.
2461 return true;
2462 } else {
2463 // We are disabling the input method but it is already disabled.
2464 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002465 return false;
2466 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002467 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002468 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002469
satok57ffc002011-01-25 00:11:47 +09002470 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2471 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002472 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002473 }
2474
satok723a27e2010-11-11 14:58:11 +09002475 private void saveCurrentInputMethodAndSubtypeToHistory() {
2476 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2477 if (mCurrentSubtype != null) {
2478 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2479 }
satok57ffc002011-01-25 00:11:47 +09002480 if (canAddToLastInputMethod(mCurrentSubtype)) {
2481 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2482 }
satokab751aa2010-09-14 19:17:36 +09002483 }
2484
satok723a27e2010-11-11 14:58:11 +09002485 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2486 boolean setSubtypeOnly) {
2487 // Update the history of InputMethod and Subtype
2488 saveCurrentInputMethodAndSubtypeToHistory();
2489
2490 // Set Subtype here
2491 if (imi == null || subtypeId < 0) {
2492 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002493 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002494 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002495 if (subtypeId < imi.getSubtypeCount()) {
2496 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2497 mSettings.putSelectedSubtype(subtype.hashCode());
2498 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002499 } else {
2500 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
2501 mCurrentSubtype = null;
2502 }
satokab751aa2010-09-14 19:17:36 +09002503 }
satok723a27e2010-11-11 14:58:11 +09002504
2505 if (!setSubtypeOnly) {
2506 // Set InputMethod here
2507 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2508 }
2509 }
2510
2511 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2512 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2513 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2514 // newDefaultIme is empty when there is no candidate for the selected IME.
2515 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2516 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2517 if (subtypeHashCode != null) {
2518 try {
2519 lastSubtypeId = getSubtypeIdFromHashCode(
2520 imi, Integer.valueOf(subtypeHashCode));
2521 } catch (NumberFormatException e) {
2522 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2523 }
2524 }
2525 }
2526 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002527 }
2528
2529 private int getSelectedInputMethodSubtypeId(String id) {
2530 InputMethodInfo imi = mMethodMap.get(id);
2531 if (imi == null) {
2532 return NOT_A_SUBTYPE_ID;
2533 }
satokab751aa2010-09-14 19:17:36 +09002534 int subtypeId;
2535 try {
2536 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2537 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2538 } catch (SettingNotFoundException e) {
2539 return NOT_A_SUBTYPE_ID;
2540 }
satok723a27e2010-11-11 14:58:11 +09002541 return getSubtypeIdFromHashCode(imi, subtypeId);
2542 }
2543
2544 private int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002545 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002546 final int subtypeCount = imi.getSubtypeCount();
2547 for (int i = 0; i < subtypeCount; ++i) {
2548 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002549 if (subtypeHashCode == ims.hashCode()) {
2550 return i;
2551 }
satokab751aa2010-09-14 19:17:36 +09002552 }
2553 }
2554 return NOT_A_SUBTYPE_ID;
2555 }
2556
satoka86f5e42011-09-02 17:12:42 +09002557 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2558 Resources res, InputMethodInfo imi) {
2559 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002560 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002561 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002562 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002563 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002564 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002565 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002566 // scan overriding implicitly enabled subtypes.
2567 InputMethodSubtype subtype = subtypes.get(i);
2568 if (subtype.overridesImplicitlyEnabledSubtype()) {
2569 final String mode = subtype.getMode();
2570 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2571 applicableModeAndSubtypesMap.put(mode, subtype);
2572 }
2573 }
2574 }
2575 if (applicableModeAndSubtypesMap.size() > 0) {
2576 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2577 }
2578 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002579 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002580 final String locale = subtype.getLocale();
2581 final String mode = subtype.getMode();
2582 // When system locale starts with subtype's locale, that subtype will be applicable
2583 // for system locale
2584 // For instance, it's clearly applicable for cases like system locale = en_US and
2585 // subtype = en, but it is not necessarily considered applicable for cases like system
2586 // locale = en and subtype = en_US.
2587 // We just call systemLocale.startsWith(locale) in this function because there is no
2588 // need to find applicable subtypes aggressively unlike
2589 // findLastResortApplicableSubtypeLocked.
2590 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002591 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002592 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002593 if (applicableSubtype != null) {
2594 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2595 if (!systemLocale.equals(locale)) continue;
2596 }
satok3da92232011-01-11 22:46:30 +09002597 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002598 }
2599 }
2600 final InputMethodSubtype keyboardSubtype
2601 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2602 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2603 applicableModeAndSubtypesMap.values());
2604 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2605 for (int i = 0; i < N; ++i) {
2606 final InputMethodSubtype subtype = subtypes.get(i);
2607 final String mode = subtype.getMode();
2608 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2609 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2610 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002611 }
2612 }
2613 }
satokc3690562012-01-10 20:14:43 +09002614 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002615 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002616 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002617 if (lastResortKeyboardSubtype != null) {
2618 applicableSubtypes.add(lastResortKeyboardSubtype);
2619 }
2620 }
2621 return applicableSubtypes;
2622 }
2623
satok4e4569d2010-11-19 18:45:53 +09002624 /**
2625 * If there are no selected subtypes, tries finding the most applicable one according to the
2626 * given locale.
2627 * @param subtypes this function will search the most applicable subtype in subtypes
2628 * @param mode subtypes will be filtered by mode
2629 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002630 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2631 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002632 * @return the most applicable subtypeId
2633 */
satokdf31ae62011-01-15 06:19:44 +09002634 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2635 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002636 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002637 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002638 return null;
satok8fbb1e82010-11-02 23:15:58 +09002639 }
satok4e4569d2010-11-19 18:45:53 +09002640 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002641 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002642 }
satok8fbb1e82010-11-02 23:15:58 +09002643 final String language = locale.substring(0, 2);
2644 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002645 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002646 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002647 final int N = subtypes.size();
2648 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002649 InputMethodSubtype subtype = subtypes.get(i);
2650 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002651 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2652 // and all subtypes with all modes can be candidates.
2653 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002654 if (firstMatchedModeSubtype == null) {
2655 firstMatchedModeSubtype = subtype;
2656 }
satok9ef02832010-11-04 21:17:48 +09002657 if (locale.equals(subtypeLocale)) {
2658 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002659 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002660 break;
2661 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2662 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002663 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002664 partialMatchFound = true;
2665 }
satok8fbb1e82010-11-02 23:15:58 +09002666 }
2667 }
2668
satok7599a7f2010-12-22 13:45:23 +09002669 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2670 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002671 }
2672
satok8fbb1e82010-11-02 23:15:58 +09002673 // The first subtype applicable to the system locale will be defined as the most applicable
2674 // subtype.
2675 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002676 if (applicableSubtype != null) {
2677 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2678 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2679 }
satok8fbb1e82010-11-02 23:15:58 +09002680 }
satokcd7cd292010-11-20 15:46:23 +09002681 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002682 }
2683
satok4e4569d2010-11-19 18:45:53 +09002684 // If there are no selected shortcuts, tries finding the most applicable ones.
2685 private Pair<InputMethodInfo, InputMethodSubtype>
2686 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2687 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2688 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002689 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002690 boolean foundInSystemIME = false;
2691
2692 // Search applicable subtype for each InputMethodInfo
2693 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002694 final String imiId = imi.getId();
2695 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2696 continue;
2697 }
satokcd7cd292010-11-20 15:46:23 +09002698 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002699 final List<InputMethodSubtype> enabledSubtypes =
2700 getEnabledInputMethodSubtypeList(imi, true);
2701 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002702 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002703 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002704 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002705 }
satokdf31ae62011-01-15 06:19:44 +09002706 // 2. Search by the system locale from enabledSubtypes.
2707 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002708 if (subtype == null) {
2709 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002710 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002711 }
satoka86f5e42011-09-02 17:12:42 +09002712 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2713 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2714 final ArrayList<InputMethodSubtype> subtypesForSearch =
2715 overridingImplicitlyEnabledSubtypes.isEmpty()
2716 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002717 // 4. Search by the current subtype's locale from all subtypes.
2718 if (subtype == null && mCurrentSubtype != null) {
2719 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002720 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002721 }
2722 // 5. Search by the system locale from all subtypes.
2723 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002724 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002725 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002726 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002727 }
satokcd7cd292010-11-20 15:46:23 +09002728 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002729 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002730 // The current input method is the most applicable IME.
2731 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002732 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002733 break;
satok7599a7f2010-12-22 13:45:23 +09002734 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002735 // The system input method is 2nd applicable IME.
2736 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002737 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002738 if ((imi.getServiceInfo().applicationInfo.flags
2739 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2740 foundInSystemIME = true;
2741 }
satok4e4569d2010-11-19 18:45:53 +09002742 }
2743 }
2744 }
2745 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002746 if (mostApplicableIMI != null) {
2747 Slog.w(TAG, "Most applicable shortcut input method was:"
2748 + mostApplicableIMI.getId());
2749 if (mostApplicableSubtype != null) {
2750 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2751 + "," + mostApplicableSubtype.getMode() + ","
2752 + mostApplicableSubtype.getLocale());
2753 }
2754 }
satok4e4569d2010-11-19 18:45:53 +09002755 }
satokcd7cd292010-11-20 15:46:23 +09002756 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002757 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002758 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002759 } else {
2760 return null;
2761 }
2762 }
2763
satokab751aa2010-09-14 19:17:36 +09002764 /**
2765 * @return Return the current subtype of this input method.
2766 */
satok42c5a162011-05-26 16:46:14 +09002767 @Override
satokab751aa2010-09-14 19:17:36 +09002768 public InputMethodSubtype getCurrentInputMethodSubtype() {
satok4e4569d2010-11-19 18:45:53 +09002769 boolean subtypeIsSelected = false;
2770 try {
2771 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2772 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2773 } catch (SettingNotFoundException e) {
2774 }
satok3ef8b292010-11-23 06:06:29 +09002775 synchronized (mMethodMap) {
satok3ef8b292010-11-23 06:06:29 +09002776 if (!subtypeIsSelected || mCurrentSubtype == null) {
satok4e4569d2010-11-19 18:45:53 +09002777 String lastInputMethodId = Settings.Secure.getString(
2778 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok3ef8b292010-11-23 06:06:29 +09002779 int subtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
2780 if (subtypeId == NOT_A_SUBTYPE_ID) {
satok4e4569d2010-11-19 18:45:53 +09002781 InputMethodInfo imi = mMethodMap.get(lastInputMethodId);
2782 if (imi != null) {
2783 // If there are no selected subtypes, the framework will try to find
satokd8713432011-01-18 00:55:13 +09002784 // the most applicable subtype from explicitly or implicitly enabled
2785 // subtypes.
2786 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2787 getEnabledInputMethodSubtypeList(imi, true);
2788 // If there is only one explicitly or implicitly enabled subtype,
2789 // just returns it.
2790 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2791 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2792 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2793 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2794 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2795 SUBTYPE_MODE_KEYBOARD, null, true);
2796 if (mCurrentSubtype == null) {
2797 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2798 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2799 true);
2800 }
2801 }
satok4e4569d2010-11-19 18:45:53 +09002802 }
satokcd7cd292010-11-20 15:46:23 +09002803 } else {
satok3ef8b292010-11-23 06:06:29 +09002804 mCurrentSubtype =
Ken Wakasa586f0512011-01-20 22:31:01 +09002805 getSubtypes(mMethodMap.get(lastInputMethodId)).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002806 }
satok8fbb1e82010-11-02 23:15:58 +09002807 }
satok3ef8b292010-11-23 06:06:29 +09002808 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002809 }
satokab751aa2010-09-14 19:17:36 +09002810 }
2811
satokf3db1af2010-11-23 13:34:33 +09002812 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2813 InputMethodSubtype subtype) {
2814 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2815 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2816 } else {
2817 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2818 subtypes.add(subtype);
2819 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2820 }
2821 }
2822
satok4e4569d2010-11-19 18:45:53 +09002823 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002824 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002825 @Override
satok4e4569d2010-11-19 18:45:53 +09002826 public List getShortcutInputMethodsAndSubtypes() {
2827 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09002828 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09002829 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09002830 // If there are no selected shortcut subtypes, the framework will try to find
2831 // the most applicable subtype from all subtypes whose mode is
2832 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09002833 Pair<InputMethodInfo, InputMethodSubtype> info =
2834 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
2835 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09002836 if (info != null) {
satok3da92232011-01-11 22:46:30 +09002837 ret.add(info.first);
2838 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09002839 }
satok3da92232011-01-11 22:46:30 +09002840 return ret;
satokf3db1af2010-11-23 13:34:33 +09002841 }
satokf3db1af2010-11-23 13:34:33 +09002842 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
2843 ret.add(imi);
2844 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
2845 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09002846 }
2847 }
satokf3db1af2010-11-23 13:34:33 +09002848 return ret;
satok4e4569d2010-11-19 18:45:53 +09002849 }
2850 }
2851
satok42c5a162011-05-26 16:46:14 +09002852 @Override
satokb66d2872010-11-10 01:04:04 +09002853 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
2854 synchronized (mMethodMap) {
2855 if (subtype != null && mCurMethodId != null) {
2856 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2857 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
2858 if (subtypeId != NOT_A_SUBTYPE_ID) {
2859 setInputMethodLocked(mCurMethodId, subtypeId);
2860 return true;
2861 }
2862 }
2863 return false;
2864 }
2865 }
2866
satok688bd472012-02-09 20:09:17 +09002867 private static class InputMethodAndSubtypeListManager {
2868 private final Context mContext;
2869 private final PackageManager mPm;
2870 private final InputMethodManagerService mImms;
2871 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
2872 mContext = context;
2873 mPm = context.getPackageManager();
2874 mImms = imms;
2875 }
2876
2877 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
2878 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
2879 new Comparator<InputMethodInfo>() {
2880 @Override
2881 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
2882 if (imi2 == null) return 0;
2883 if (imi1 == null) return 1;
2884 if (mPm == null) {
2885 return imi1.getId().compareTo(imi2.getId());
2886 }
2887 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
2888 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
2889 return imiId1.toString().compareTo(imiId2.toString());
2890 }
2891 });
2892
2893 public ImeSubtypeListItem getNextInputMethod(
2894 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
2895 if (imi == null) {
2896 return null;
2897 }
2898 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
2899 if (imList.size() <= 1) {
2900 return null;
2901 }
2902 final int N = imList.size();
2903 final int currentSubtypeId = subtype != null
2904 ? mImms.getSubtypeIdFromHashCode(imi, subtype.hashCode())
2905 : NOT_A_SUBTYPE_ID;
2906 for (int i = 0; i < N; ++i) {
2907 final ImeSubtypeListItem isli = imList.get(i);
2908 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
2909 if (!onlyCurrentIme) {
2910 return imList.get((i + 1) % N);
2911 }
2912 for (int j = 0; j < N - 1; ++j) {
2913 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
2914 if (candidate.mImi.equals(imi)) {
2915 return candidate;
2916 }
2917 }
2918 return null;
2919 }
2920 }
2921 return null;
2922 }
2923
2924 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
2925 return getSortedInputMethodAndSubtypeList(true, false, false);
2926 }
2927
2928 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
2929 boolean inputShown, boolean isScreenLocked) {
2930 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
2931 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2932 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
2933 if (immis == null || immis.size() == 0) {
2934 return Collections.emptyList();
2935 }
2936 mSortedImmis.clear();
2937 mSortedImmis.putAll(immis);
2938 for (InputMethodInfo imi : mSortedImmis.keySet()) {
2939 if (imi == null) continue;
2940 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
2941 HashSet<String> enabledSubtypeSet = new HashSet<String>();
2942 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
2943 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
2944 }
2945 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
2946 final CharSequence imeLabel = imi.loadLabel(mPm);
2947 if (showSubtypes && enabledSubtypeSet.size() > 0) {
2948 final int subtypeCount = imi.getSubtypeCount();
2949 if (DEBUG) {
2950 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
2951 }
2952 for (int j = 0; j < subtypeCount; ++j) {
2953 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
2954 final String subtypeHashCode = String.valueOf(subtype.hashCode());
2955 // We show all enabled IMEs and subtypes when an IME is shown.
2956 if (enabledSubtypeSet.contains(subtypeHashCode)
2957 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
2958 final CharSequence subtypeLabel =
2959 subtype.overridesImplicitlyEnabledSubtype() ? null
2960 : subtype.getDisplayName(mContext, imi.getPackageName(),
2961 imi.getServiceInfo().applicationInfo);
2962 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j));
2963
2964 // Removing this subtype from enabledSubtypeSet because we no longer
2965 // need to add an entry of this subtype to imList to avoid duplicated
2966 // entries.
2967 enabledSubtypeSet.remove(subtypeHashCode);
2968 }
2969 }
2970 } else {
2971 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID));
2972 }
2973 }
2974 return imList;
2975 }
2976 }
2977
satokd87c2592010-09-29 11:52:06 +09002978 /**
2979 * Utility class for putting and getting settings for InputMethod
2980 * TODO: Move all putters and getters of settings to this class.
2981 */
2982 private static class InputMethodSettings {
2983 // The string for enabled input method is saved as follows:
2984 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
2985 private static final char INPUT_METHOD_SEPARATER = ':';
2986 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09002987 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09002988 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
2989
satok723a27e2010-11-11 14:58:11 +09002990 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09002991 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
2992
satokdf31ae62011-01-15 06:19:44 +09002993 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09002994 private final ContentResolver mResolver;
2995 private final HashMap<String, InputMethodInfo> mMethodMap;
2996 private final ArrayList<InputMethodInfo> mMethodList;
2997
2998 private String mEnabledInputMethodsStrCache;
2999
3000 private static void buildEnabledInputMethodsSettingString(
3001 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3002 String id = pair.first;
3003 ArrayList<String> subtypes = pair.second;
3004 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003005 // Inputmethod and subtypes are saved in the settings as follows:
3006 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3007 for (String subtypeId: subtypes) {
3008 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003009 }
3010 }
3011
3012 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003013 Resources res, ContentResolver resolver,
3014 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3015 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003016 mResolver = resolver;
3017 mMethodMap = methodMap;
3018 mMethodList = methodList;
3019 }
3020
3021 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3022 return createEnabledInputMethodListLocked(
3023 getEnabledInputMethodsAndSubtypeListLocked());
3024 }
3025
satok7f35c8c2010-10-07 21:13:11 +09003026 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003027 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3028 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003029 getEnabledInputMethodsAndSubtypeListLocked());
3030 }
3031
satok67ddf9c2010-11-17 09:45:54 +09003032 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3033 InputMethodInfo imi) {
3034 List<Pair<String, ArrayList<String>>> imsList =
3035 getEnabledInputMethodsAndSubtypeListLocked();
3036 ArrayList<InputMethodSubtype> enabledSubtypes =
3037 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003038 if (imi != null) {
3039 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3040 InputMethodInfo info = mMethodMap.get(imsPair.first);
3041 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003042 final int subtypeCount = info.getSubtypeCount();
3043 for (int i = 0; i < subtypeCount; ++i) {
3044 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003045 for (String s: imsPair.second) {
3046 if (String.valueOf(ims.hashCode()).equals(s)) {
3047 enabledSubtypes.add(ims);
3048 }
satok67ddf9c2010-11-17 09:45:54 +09003049 }
3050 }
satok884ef9a2010-11-18 10:39:46 +09003051 break;
satok67ddf9c2010-11-17 09:45:54 +09003052 }
satok67ddf9c2010-11-17 09:45:54 +09003053 }
3054 }
3055 return enabledSubtypes;
3056 }
3057
satokd87c2592010-09-29 11:52:06 +09003058 // At the initial boot, the settings for input methods are not set,
3059 // so we need to enable IME in that case.
3060 public void enableAllIMEsIfThereIsNoEnabledIME() {
3061 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3062 StringBuilder sb = new StringBuilder();
3063 final int N = mMethodList.size();
3064 for (int i = 0; i < N; i++) {
3065 InputMethodInfo imi = mMethodList.get(i);
3066 Slog.i(TAG, "Adding: " + imi.getId());
3067 if (i > 0) sb.append(':');
3068 sb.append(imi.getId());
3069 }
3070 putEnabledInputMethodsStr(sb.toString());
3071 }
3072 }
3073
satokbb4aa062011-01-19 21:40:27 +09003074 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003075 ArrayList<Pair<String, ArrayList<String>>> imsList
3076 = new ArrayList<Pair<String, ArrayList<String>>>();
3077 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3078 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3079 return imsList;
3080 }
satok723a27e2010-11-11 14:58:11 +09003081 mInputMethodSplitter.setString(enabledInputMethodsStr);
3082 while (mInputMethodSplitter.hasNext()) {
3083 String nextImsStr = mInputMethodSplitter.next();
3084 mSubtypeSplitter.setString(nextImsStr);
3085 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003086 ArrayList<String> subtypeHashes = new ArrayList<String>();
3087 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003088 String imeId = mSubtypeSplitter.next();
3089 while (mSubtypeSplitter.hasNext()) {
3090 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003091 }
3092 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3093 }
3094 }
3095 return imsList;
3096 }
3097
3098 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3099 if (reloadInputMethodStr) {
3100 getEnabledInputMethodsStr();
3101 }
3102 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3103 // Add in the newly enabled input method.
3104 putEnabledInputMethodsStr(id);
3105 } else {
3106 putEnabledInputMethodsStr(
3107 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3108 }
3109 }
3110
3111 /**
3112 * Build and put a string of EnabledInputMethods with removing specified Id.
3113 * @return the specified id was removed or not.
3114 */
3115 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3116 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3117 boolean isRemoved = false;
3118 boolean needsAppendSeparator = false;
3119 for (Pair<String, ArrayList<String>> ims: imsList) {
3120 String curId = ims.first;
3121 if (curId.equals(id)) {
3122 // We are disabling this input method, and it is
3123 // currently enabled. Skip it to remove from the
3124 // new list.
3125 isRemoved = true;
3126 } else {
3127 if (needsAppendSeparator) {
3128 builder.append(INPUT_METHOD_SEPARATER);
3129 } else {
3130 needsAppendSeparator = true;
3131 }
3132 buildEnabledInputMethodsSettingString(builder, ims);
3133 }
3134 }
3135 if (isRemoved) {
3136 // Update the setting with the new list of input methods.
3137 putEnabledInputMethodsStr(builder.toString());
3138 }
3139 return isRemoved;
3140 }
3141
3142 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3143 List<Pair<String, ArrayList<String>>> imsList) {
3144 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3145 for (Pair<String, ArrayList<String>> ims: imsList) {
3146 InputMethodInfo info = mMethodMap.get(ims.first);
3147 if (info != null) {
3148 res.add(info);
3149 }
3150 }
3151 return res;
3152 }
3153
satok7f35c8c2010-10-07 21:13:11 +09003154 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003155 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003156 List<Pair<String, ArrayList<String>>> imsList) {
3157 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3158 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3159 for (Pair<String, ArrayList<String>> ims : imsList) {
3160 InputMethodInfo info = mMethodMap.get(ims.first);
3161 if (info != null) {
3162 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3163 }
3164 }
3165 return res;
3166 }
3167
satokd87c2592010-09-29 11:52:06 +09003168 private void putEnabledInputMethodsStr(String str) {
3169 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3170 mEnabledInputMethodsStrCache = str;
3171 }
3172
3173 private String getEnabledInputMethodsStr() {
3174 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3175 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003176 if (DEBUG) {
3177 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3178 }
satokd87c2592010-09-29 11:52:06 +09003179 return mEnabledInputMethodsStrCache;
3180 }
satok723a27e2010-11-11 14:58:11 +09003181
3182 private void saveSubtypeHistory(
3183 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3184 StringBuilder builder = new StringBuilder();
3185 boolean isImeAdded = false;
3186 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3187 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3188 newSubtypeId);
3189 isImeAdded = true;
3190 }
3191 for (Pair<String, String> ime: savedImes) {
3192 String imeId = ime.first;
3193 String subtypeId = ime.second;
3194 if (TextUtils.isEmpty(subtypeId)) {
3195 subtypeId = NOT_A_SUBTYPE_ID_STR;
3196 }
3197 if (isImeAdded) {
3198 builder.append(INPUT_METHOD_SEPARATER);
3199 } else {
3200 isImeAdded = true;
3201 }
3202 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3203 subtypeId);
3204 }
3205 // Remove the last INPUT_METHOD_SEPARATER
3206 putSubtypeHistoryStr(builder.toString());
3207 }
3208
3209 public void addSubtypeToHistory(String imeId, String subtypeId) {
3210 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3211 for (Pair<String, String> ime: subtypeHistory) {
3212 if (ime.first.equals(imeId)) {
3213 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003214 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003215 + ime.second);
3216 }
3217 // We should break here
3218 subtypeHistory.remove(ime);
3219 break;
3220 }
3221 }
satokbb4aa062011-01-19 21:40:27 +09003222 if (DEBUG) {
3223 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3224 }
satok723a27e2010-11-11 14:58:11 +09003225 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3226 }
3227
3228 private void putSubtypeHistoryStr(String str) {
3229 if (DEBUG) {
3230 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3231 }
3232 Settings.Secure.putString(
3233 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3234 }
3235
3236 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3237 // Gets the first one from the history
3238 return getLastSubtypeForInputMethodLockedInternal(null);
3239 }
3240
3241 public String getLastSubtypeForInputMethodLocked(String imeId) {
3242 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3243 if (ime != null) {
3244 return ime.second;
3245 } else {
3246 return null;
3247 }
3248 }
3249
3250 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3251 List<Pair<String, ArrayList<String>>> enabledImes =
3252 getEnabledInputMethodsAndSubtypeListLocked();
3253 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003254 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003255 final String imeInTheHistory = imeAndSubtype.first;
3256 // If imeId is empty, returns the first IME and subtype in the history
3257 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3258 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003259 final String subtypeHashCode =
3260 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3261 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003262 if (!TextUtils.isEmpty(subtypeHashCode)) {
3263 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003264 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003265 }
3266 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3267 }
3268 }
3269 }
3270 if (DEBUG) {
3271 Slog.d(TAG, "No enabled IME found in the history");
3272 }
3273 return null;
3274 }
3275
satokdf31ae62011-01-15 06:19:44 +09003276 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003277 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3278 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3279 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003280 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
3281 if (explicitlyEnabledSubtypes.size() == 0) {
3282 // If there are no explicitly enabled subtypes, applicable subtypes are
3283 // enabled implicitly.
satoka86f5e42011-09-02 17:12:42 +09003284 InputMethodInfo imi = mMethodMap.get(imeId);
satokdf31ae62011-01-15 06:19:44 +09003285 // If IME is enabled and no subtypes are enabled, applicable subtypes
3286 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003287 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003288 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003289 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003290 if (implicitlySelectedSubtypes != null) {
3291 final int N = implicitlySelectedSubtypes.size();
3292 for (int i = 0; i < N; ++i) {
3293 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3294 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3295 return subtypeHashCode;
3296 }
3297 }
3298 }
3299 }
3300 } else {
satokf6cafb62011-01-17 16:29:02 +09003301 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003302 if (s.equals(subtypeHashCode)) {
3303 // If both imeId and subtypeId are enabled, return subtypeId.
3304 return s;
3305 }
satok723a27e2010-11-11 14:58:11 +09003306 }
3307 }
3308 // If imeId was enabled but subtypeId was disabled.
3309 return NOT_A_SUBTYPE_ID_STR;
3310 }
3311 }
3312 // If both imeId and subtypeId are disabled, return null
3313 return null;
3314 }
3315
3316 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3317 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3318 final String subtypeHistoryStr = getSubtypeHistoryStr();
3319 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3320 return imsList;
3321 }
3322 mInputMethodSplitter.setString(subtypeHistoryStr);
3323 while (mInputMethodSplitter.hasNext()) {
3324 String nextImsStr = mInputMethodSplitter.next();
3325 mSubtypeSplitter.setString(nextImsStr);
3326 if (mSubtypeSplitter.hasNext()) {
3327 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3328 // The first element is ime id.
3329 String imeId = mSubtypeSplitter.next();
3330 while (mSubtypeSplitter.hasNext()) {
3331 subtypeId = mSubtypeSplitter.next();
3332 break;
3333 }
3334 imsList.add(new Pair<String, String>(imeId, subtypeId));
3335 }
3336 }
3337 return imsList;
3338 }
3339
3340 private String getSubtypeHistoryStr() {
3341 if (DEBUG) {
3342 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3343 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3344 }
3345 return Settings.Secure.getString(
3346 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3347 }
3348
3349 public void putSelectedInputMethod(String imeId) {
3350 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3351 }
3352
3353 public void putSelectedSubtype(int subtypeId) {
3354 Settings.Secure.putInt(
3355 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3356 }
satokd87c2592010-09-29 11:52:06 +09003357 }
3358
satoke7c6998e2011-06-03 17:57:59 +09003359 private static class InputMethodFileManager {
3360 private static final String SYSTEM_PATH = "system";
3361 private static final String INPUT_METHOD_PATH = "inputmethod";
3362 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3363 private static final String NODE_SUBTYPES = "subtypes";
3364 private static final String NODE_SUBTYPE = "subtype";
3365 private static final String NODE_IMI = "imi";
3366 private static final String ATTR_ID = "id";
3367 private static final String ATTR_LABEL = "label";
3368 private static final String ATTR_ICON = "icon";
3369 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3370 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3371 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3372 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3373 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3374 private final HashMap<String, InputMethodInfo> mMethodMap;
3375 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3376 new HashMap<String, List<InputMethodSubtype>>();
3377 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3378 if (methodMap == null) {
3379 throw new NullPointerException("methodMap is null");
3380 }
3381 mMethodMap = methodMap;
3382 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3383 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3384 if (!inputMethodDir.mkdirs()) {
3385 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3386 }
3387 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3388 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3389 if (!subtypeFile.exists()) {
3390 // If "subtypes.xml" doesn't exist, create a blank file.
3391 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3392 methodMap);
3393 } else {
3394 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3395 }
3396 }
3397
3398 private void deleteAllInputMethodSubtypes(String imiId) {
3399 synchronized (mMethodMap) {
3400 mSubtypesMap.remove(imiId);
3401 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3402 mMethodMap);
3403 }
3404 }
3405
3406 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003407 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003408 synchronized (mMethodMap) {
3409 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3410 final int N = additionalSubtypes.length;
3411 for (int i = 0; i < N; ++i) {
3412 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003413 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003414 subtypes.add(subtype);
3415 }
3416 }
satok4a28bde2011-06-29 21:03:40 +09003417 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003418 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3419 mMethodMap);
3420 }
3421 }
3422
3423 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3424 synchronized (mMethodMap) {
3425 return mSubtypesMap;
3426 }
3427 }
3428
3429 private static void writeAdditionalInputMethodSubtypes(
3430 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3431 HashMap<String, InputMethodInfo> methodMap) {
3432 // Safety net for the case that this function is called before methodMap is set.
3433 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3434 FileOutputStream fos = null;
3435 try {
3436 fos = subtypesFile.startWrite();
3437 final XmlSerializer out = new FastXmlSerializer();
3438 out.setOutput(fos, "utf-8");
3439 out.startDocument(null, true);
3440 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3441 out.startTag(null, NODE_SUBTYPES);
3442 for (String imiId : allSubtypes.keySet()) {
3443 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3444 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3445 continue;
3446 }
3447 out.startTag(null, NODE_IMI);
3448 out.attribute(null, ATTR_ID, imiId);
3449 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3450 final int N = subtypesList.size();
3451 for (int i = 0; i < N; ++i) {
3452 final InputMethodSubtype subtype = subtypesList.get(i);
3453 out.startTag(null, NODE_SUBTYPE);
3454 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3455 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3456 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3457 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3458 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3459 out.attribute(null, ATTR_IS_AUXILIARY,
3460 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3461 out.endTag(null, NODE_SUBTYPE);
3462 }
3463 out.endTag(null, NODE_IMI);
3464 }
3465 out.endTag(null, NODE_SUBTYPES);
3466 out.endDocument();
3467 subtypesFile.finishWrite(fos);
3468 } catch (java.io.IOException e) {
3469 Slog.w(TAG, "Error writing subtypes", e);
3470 if (fos != null) {
3471 subtypesFile.failWrite(fos);
3472 }
3473 }
3474 }
3475
3476 private static void readAdditionalInputMethodSubtypes(
3477 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3478 if (allSubtypes == null || subtypesFile == null) return;
3479 allSubtypes.clear();
3480 FileInputStream fis = null;
3481 try {
3482 fis = subtypesFile.openRead();
3483 final XmlPullParser parser = Xml.newPullParser();
3484 parser.setInput(fis, null);
3485 int type = parser.getEventType();
3486 // Skip parsing until START_TAG
3487 while ((type = parser.next()) != XmlPullParser.START_TAG
3488 && type != XmlPullParser.END_DOCUMENT) {}
3489 String firstNodeName = parser.getName();
3490 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3491 throw new XmlPullParserException("Xml doesn't start with subtypes");
3492 }
3493 final int depth =parser.getDepth();
3494 String currentImiId = null;
3495 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3496 while (((type = parser.next()) != XmlPullParser.END_TAG
3497 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3498 if (type != XmlPullParser.START_TAG)
3499 continue;
3500 final String nodeName = parser.getName();
3501 if (NODE_IMI.equals(nodeName)) {
3502 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3503 if (TextUtils.isEmpty(currentImiId)) {
3504 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3505 continue;
3506 }
3507 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3508 allSubtypes.put(currentImiId, tempSubtypesArray);
3509 } else if (NODE_SUBTYPE.equals(nodeName)) {
3510 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3511 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3512 continue;
3513 }
3514 final int icon = Integer.valueOf(
3515 parser.getAttributeValue(null, ATTR_ICON));
3516 final int label = Integer.valueOf(
3517 parser.getAttributeValue(null, ATTR_LABEL));
3518 final String imeSubtypeLocale =
3519 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3520 final String imeSubtypeMode =
3521 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3522 final String imeSubtypeExtraValue =
3523 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003524 final boolean isAuxiliary = "1".equals(String.valueOf(
3525 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003526 final InputMethodSubtype subtype =
3527 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3528 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3529 tempSubtypesArray.add(subtype);
3530 }
3531 }
3532 } catch (XmlPullParserException e) {
3533 Slog.w(TAG, "Error reading subtypes: " + e);
3534 return;
3535 } catch (java.io.IOException e) {
3536 Slog.w(TAG, "Error reading subtypes: " + e);
3537 return;
3538 } catch (NumberFormatException e) {
3539 Slog.w(TAG, "Error reading subtypes: " + e);
3540 return;
3541 } finally {
3542 if (fis != null) {
3543 try {
3544 fis.close();
3545 } catch (java.io.IOException e1) {
3546 Slog.w(TAG, "Failed to close.");
3547 }
3548 }
3549 }
3550 }
3551 }
3552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 @Override
3556 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3557 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3558 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3561 + Binder.getCallingPid()
3562 + ", uid=" + Binder.getCallingUid());
3563 return;
3564 }
3565
3566 IInputMethod method;
3567 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 synchronized (mMethodMap) {
3572 p.println("Current Input Method Manager state:");
3573 int N = mMethodList.size();
3574 p.println(" Input Methods:");
3575 for (int i=0; i<N; i++) {
3576 InputMethodInfo info = mMethodList.get(i);
3577 p.println(" InputMethod #" + i + ":");
3578 info.dump(p, " ");
3579 }
3580 p.println(" Clients:");
3581 for (ClientState ci : mClients.values()) {
3582 p.println(" Client " + ci + ":");
3583 p.println(" client=" + ci.client);
3584 p.println(" inputContext=" + ci.inputContext);
3585 p.println(" sessionRequested=" + ci.sessionRequested);
3586 p.println(" curSession=" + ci.curSession);
3587 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003588 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003590 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3591 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3593 + " mBoundToMethod=" + mBoundToMethod);
3594 p.println(" mCurToken=" + mCurToken);
3595 p.println(" mCurIntent=" + mCurIntent);
3596 method = mCurMethod;
3597 p.println(" mCurMethod=" + mCurMethod);
3598 p.println(" mEnabledSession=" + mEnabledSession);
3599 p.println(" mShowRequested=" + mShowRequested
3600 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3601 + " mShowForced=" + mShowForced
3602 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003603 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003605
Jeff Brownb88102f2010-09-08 11:49:43 -07003606 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 pw.flush();
3609 try {
3610 client.client.asBinder().dump(fd, args);
3611 } catch (RemoteException e) {
3612 p.println("Input method client dead: " + e);
3613 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003614 } else {
3615 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003617
Jeff Brownb88102f2010-09-08 11:49:43 -07003618 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 pw.flush();
3621 try {
3622 method.asBinder().dump(fd, args);
3623 } catch (RemoteException e) {
3624 p.println("Input method service dead: " + e);
3625 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003626 } else {
3627 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 }
3629 }
3630}