blob: 2d41f4399523a67ae3e2818976d0c3d731df48b9 [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;
satok5b927c432012-05-01 20:09:34 +090042import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.ComponentName;
44import android.content.ContentResolver;
45import android.content.Context;
46import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.DialogInterface.OnCancelListener;
48import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090049import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070051import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070056import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.res.Resources;
58import android.content.res.TypedArray;
59import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080060import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.IInterface;
66import android.os.Message;
67import android.os.Parcel;
68import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080069import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.ServiceManager;
71import android.os.SystemClock;
72import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070073import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090074import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090076import android.text.style.SuggestionSpan;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090078import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090079import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.PrintWriterPrinter;
81import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090082import android.util.Slog;
83import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090085import android.view.LayoutInflater;
86import android.view.View;
87import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090089import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.inputmethod.InputBinding;
91import android.view.inputmethod.InputMethod;
92import android.view.inputmethod.InputMethodInfo;
93import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +090094import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +090095import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +090096import android.widget.CompoundButton;
97import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +090098import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +090099import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900100import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
satoke7c6998e2011-06-03 17:57:59 +0900102import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900104import java.io.FileInputStream;
105import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import java.io.IOException;
107import java.io.PrintWriter;
108import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900109import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900110import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900112import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900114import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900115import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117/**
118 * This class provides a system service that manages input methods.
119 */
120public class InputMethodManagerService extends IInputMethodManager.Stub
121 implements ServiceConnection, Handler.Callback {
122 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700123 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900126 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900127 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900128 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 static final int MSG_UNBIND_INPUT = 1000;
131 static final int MSG_BIND_INPUT = 1010;
132 static final int MSG_SHOW_SOFT_INPUT = 1020;
133 static final int MSG_HIDE_SOFT_INPUT = 1030;
134 static final int MSG_ATTACH_TOKEN = 1040;
135 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_START_INPUT = 2000;
138 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 static final int MSG_UNBIND_METHOD = 3000;
141 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700142 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
satok01038492012-04-09 21:08:27 +0900144 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800147
satokf9f01002011-05-19 21:31:50 +0900148 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
149
satokab751aa2010-09-14 19:17:36 +0900150 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900151 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900152 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
153 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900154 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900155 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
156 "EnabledWhenDefaultIsNotAsciiCapable";
157 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900158 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800161 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900163 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final IWindowManager mIWindowManager;
166 final HandlerCaller mCaller;
satoke7c6998e2011-06-03 17:57:59 +0900167 private final InputMethodFileManager mFileManager;
satok688bd472012-02-09 20:09:17 +0900168 private final InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900169 private final HardKeyboardListener mHardKeyboardListener;
170 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 // All known input methods. mMethodMap also serves as the global
175 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900176 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
177 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900178 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
179 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700181 // Used to bring IME service up to visible adjustment while it is being shown.
182 final ServiceConnection mVisibleConnection = new ServiceConnection() {
183 @Override public void onServiceConnected(ComponentName name, IBinder service) {
184 }
185
186 @Override public void onServiceDisconnected(ComponentName name) {
187 }
188 };
189 boolean mVisibleBound = false;
190
satok7cfc0ed2011-06-20 21:29:36 +0900191 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700192 private NotificationManager mNotificationManager;
193 private KeyguardManager mKeyguardManager;
194 private StatusBarManagerService mStatusBar;
195 private Notification mImeSwitcherNotification;
196 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900197 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900198 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900199 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 class SessionState {
202 final ClientState client;
203 final IInputMethod method;
204 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 @Override
207 public String toString() {
208 return "SessionState{uid " + client.uid + " pid " + client.pid
209 + " method " + Integer.toHexString(
210 System.identityHashCode(method))
211 + " session " + Integer.toHexString(
212 System.identityHashCode(session))
213 + "}";
214 }
215
216 SessionState(ClientState _client, IInputMethod _method,
217 IInputMethodSession _session) {
218 client = _client;
219 method = _method;
220 session = _session;
221 }
222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 class ClientState {
225 final IInputMethodClient client;
226 final IInputContext inputContext;
227 final int uid;
228 final int pid;
229 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 boolean sessionRequested;
232 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 @Override
235 public String toString() {
236 return "ClientState{" + Integer.toHexString(
237 System.identityHashCode(this)) + " uid " + uid
238 + " pid " + pid + "}";
239 }
240
241 ClientState(IInputMethodClient _client, IInputContext _inputContext,
242 int _uid, int _pid) {
243 client = _client;
244 inputContext = _inputContext;
245 uid = _uid;
246 pid = _pid;
247 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 final HashMap<IBinder, ClientState> mClients
252 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700255 * Set once the system is ready to run third party code.
256 */
257 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800258
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Id of the currently selected input method.
261 */
262 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
265 * The current binding sequence number, incremented every time there is
266 * a new bind performed.
267 */
268 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The client that is currently bound to an input method.
272 */
273 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700276 * The last window token that gained focus.
277 */
278 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700280 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * The input context last provided by the current client.
282 */
283 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 /**
286 * The attributes last provided by the current client.
287 */
288 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The input method ID of the input method service that we are currently
292 * connected to or in the process of connecting to.
293 */
294 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
satokab751aa2010-09-14 19:17:36 +0900297 * The current subtype of the current input method.
298 */
299 private InputMethodSubtype mCurrentSubtype;
300
satok4e4569d2010-11-19 18:45:53 +0900301 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900302 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
303 mShortcutInputMethodsAndSubtypes =
304 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900305
306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 * Set to true if our ServiceConnection is currently actively bound to
308 * a service (whether or not we have gotten its IBinder back yet).
309 */
310 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
313 * Set if the client has asked for the input method to be shown.
314 */
315 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 /**
318 * Set if we were explicitly told to show the input method.
319 */
320 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 /**
323 * Set if we were forced to be shown.
324 */
325 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 /**
328 * Set if we last told the input method to show itself.
329 */
330 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 /**
333 * The Intent used to connect to the current input method.
334 */
335 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * The token we have made for the currently active input method, to
339 * identify it in the future.
340 */
341 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * If non-null, this is the input method service we are currently connected
345 * to.
346 */
347 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * Time that we last initiated a bind to the input method, to determine
351 * if we should try to disconnect and reconnect to it.
352 */
353 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Have we called mCurMethod.bindInput()?
357 */
358 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 /**
361 * Currently enabled session. Only touched by service thread, not
362 * protected by a lock.
363 */
364 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * True if the screen is on. The value is true initially.
368 */
369 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Joe Onorato857fd9b2011-01-27 15:08:35 -0800371 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
372 int mImeWindowVis;
373
Ken Wakasa05dbb652011-08-22 15:22:43 +0900374 private AlertDialog.Builder mDialogBuilder;
375 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900376 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900377 private InputMethodInfo[] mIms;
378 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900379 private Locale mLastSystemLocale;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 class SettingsObserver extends ContentObserver {
382 SettingsObserver(Handler handler) {
383 super(handler);
384 ContentResolver resolver = mContext.getContentResolver();
385 resolver.registerContentObserver(Settings.Secure.getUriFor(
386 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900387 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900388 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
389 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900390 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 @Override public void onChange(boolean selfChange) {
394 synchronized (mMethodMap) {
395 updateFromSettingsLocked();
396 }
397 }
398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 class ScreenOnOffReceiver extends android.content.BroadcastReceiver {
401 @Override
402 public void onReceive(Context context, Intent intent) {
403 if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
404 mScreenOn = true;
satok3afd6c02011-11-18 08:38:19 +0900405 refreshImeWindowVisibilityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
407 mScreenOn = false;
satok15452a42011-10-28 17:58:28 +0900408 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -0700409 } else if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
410 hideInputMethodMenu();
411 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800413 Slog.w(TAG, "Unexpected intent " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415
416 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700417 if (mCurClient != null && mCurClient.client != null) {
418 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
419 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421 }
422 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800423
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800424 class MyPackageMonitor extends PackageMonitor {
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800427 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 synchronized (mMethodMap) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800429 String curInputMethodId = Settings.Secure.getString(mContext
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
431 final int N = mMethodList.size();
432 if (curInputMethodId != null) {
433 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800434 InputMethodInfo imi = mMethodList.get(i);
435 if (imi.getId().equals(curInputMethodId)) {
436 for (String pkg : packages) {
437 if (imi.getPackageName().equals(pkg)) {
438 if (!doit) {
439 return true;
440 }
satok723a27e2010-11-11 14:58:11 +0900441 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 chooseNewDefaultIMELocked();
443 return true;
444 }
445 }
446 }
447 }
448 }
449 }
450 return false;
451 }
452
453 @Override
454 public void onSomePackagesChanged() {
455 synchronized (mMethodMap) {
456 InputMethodInfo curIm = null;
457 String curInputMethodId = Settings.Secure.getString(mContext
458 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
459 final int N = mMethodList.size();
460 if (curInputMethodId != null) {
461 for (int i=0; i<N; i++) {
462 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900463 final String imiId = imi.getId();
464 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800465 curIm = imi;
466 }
satoke7c6998e2011-06-03 17:57:59 +0900467
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800468 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900469 if (isPackageModified(imi.getPackageName())) {
470 mFileManager.deleteAllInputMethodSubtypes(imiId);
471 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 if (change == PACKAGE_TEMPORARY_CHANGE
473 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800475 + imi.getComponent());
476 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800480
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800481 buildInputMethodListLocked(mMethodList, mMethodMap);
482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800484
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800485 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 int change = isPackageDisappearing(curIm.getPackageName());
487 if (change == PACKAGE_TEMPORARY_CHANGE
488 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800489 ServiceInfo si = null;
490 try {
491 si = mContext.getPackageManager().getServiceInfo(
492 curIm.getComponent(), 0);
493 } catch (PackageManager.NameNotFoundException ex) {
494 }
495 if (si == null) {
496 // Uh oh, current input method is no longer around!
497 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800498 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900499 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800500 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800501 changed = true;
502 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800503 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900504 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800505 }
506 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800507 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800508 }
satokab751aa2010-09-14 19:17:36 +0900509
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 if (curIm == null) {
511 // We currently don't have a default input method... is
512 // one now available?
513 changed = chooseNewDefaultIMELocked();
514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800515
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516 if (changed) {
517 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519 }
520 }
521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800522
Jean Chalarde0d32a62011-10-20 20:36:07 +0900523 private static class MethodCallback extends IInputMethodCallback.Stub {
524 private final IInputMethod mMethod;
525 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800526
Jean Chalarde0d32a62011-10-20 20:36:07 +0900527 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900529 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800531
satoke7c6998e2011-06-03 17:57:59 +0900532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 public void finishedEvent(int seq, boolean handled) throws RemoteException {
534 }
535
satoke7c6998e2011-06-03 17:57:59 +0900536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900538 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800541
satok01038492012-04-09 21:08:27 +0900542 private class HardKeyboardListener
543 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
544 @Override
545 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
546 mHandler.sendMessage(mHandler.obtainMessage(
547 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
548 }
549
550 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
551 if (DEBUG) {
552 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
553 + enabled);
554 }
555 synchronized(mMethodMap) {
556 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
557 && mSwitchingDialog.isShowing()) {
558 mSwitchingDialogTitleView.findViewById(
559 com.android.internal.R.id.hard_keyboard_section).setVisibility(
560 available ? View.VISIBLE : View.GONE);
561 }
562 }
563 }
564 }
565
566 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800568 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mHandler = new Handler(this);
570 mIWindowManager = IWindowManager.Stub.asInterface(
571 ServiceManager.getService(Context.WINDOW_SERVICE));
572 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900573 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 public void executeMessage(Message msg) {
575 handleMessage(msg);
576 }
577 });
satok01038492012-04-09 21:08:27 +0900578 mWindowManagerService = windowManager;
579 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900580
satok7cfc0ed2011-06-20 21:29:36 +0900581 mImeSwitcherNotification = new Notification();
582 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
583 mImeSwitcherNotification.when = 0;
584 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
585 mImeSwitcherNotification.tickerText = null;
586 mImeSwitcherNotification.defaults = 0; // please be quiet
587 mImeSwitcherNotification.sound = null;
588 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400589
590 // Tag this notification specially so SystemUI knows it's important
591 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
592
satok7cfc0ed2011-06-20 21:29:36 +0900593 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900594 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900595
596 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900597
satoke7c6998e2011-06-03 17:57:59 +0900598 synchronized (mMethodMap) {
599 mFileManager = new InputMethodFileManager(mMethodMap);
600 }
satok688bd472012-02-09 20:09:17 +0900601 mImListManager = new InputMethodAndSubtypeListManager(context, this);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800602
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700603 (new MyPackageMonitor()).register(mContext, null, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 IntentFilter screenOnOffFilt = new IntentFilter();
606 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_ON);
607 screenOnOffFilt.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project10592532009-03-18 17:39:46 -0700608 screenOnOffFilt.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 mContext.registerReceiver(new ScreenOnOffReceiver(), screenOnOffFilt);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800610
satok7cfc0ed2011-06-20 21:29:36 +0900611 mNotificationShown = false;
satok913a8922010-08-26 21:53:41 +0900612
satokd87c2592010-09-29 11:52:06 +0900613 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900614 mSettings = new InputMethodSettings(
615 mRes, context.getContentResolver(), mMethodMap, mMethodList);
satok0a1bcf42012-05-16 19:26:31 +0900616
617 // Just checking if defaultImiId is empty or not
618 final String defaultImiId = Settings.Secure.getString(
619 mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
620 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900623 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
satok0a1bcf42012-05-16 19:26:31 +0900625 if (!mImeSelectedOnBoot) {
626 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900627 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 mSettingsObserver = new SettingsObserver(mHandler);
631 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900632
633 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
634 // according to the new system locale.
635 final IntentFilter filter = new IntentFilter();
636 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
637 mContext.registerReceiver(
638 new BroadcastReceiver() {
639 @Override
640 public void onReceive(Context context, Intent intent) {
641 synchronized(mMethodMap) {
642 checkCurrentLocaleChangedLocked();
643 }
644 }
645 }, filter);
646 }
647
648 private void checkCurrentLocaleChangedLocked() {
satok0a1bcf42012-05-16 19:26:31 +0900649 if (!mSystemReady) {
650 // not system ready
651 return;
652 }
satok5b927c432012-05-01 20:09:34 +0900653 final Locale newLocale = mRes.getConfiguration().locale;
654 if (newLocale != null && !newLocale.equals(mLastSystemLocale)) {
655 if (DEBUG) {
656 Slog.i(TAG, "Locale has been changed to " + newLocale);
657 }
658 buildInputMethodListLocked(mMethodList, mMethodMap);
659 // Reset the current ime to the proper one
660 resetDefaultImeLocked(mContext);
satokd81e9502012-05-21 12:58:45 +0900661 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900662 mLastSystemLocale = newLocale;
663 }
664 }
665
666 private void resetDefaultImeLocked(Context context) {
667 // Do not reset the default (current) IME when it is a 3rd-party IME
668 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
669 return;
670 }
671
672 InputMethodInfo defIm = null;
673 for (InputMethodInfo imi : mMethodList) {
674 if (defIm == null) {
675 if (isValidSystemDefaultIme(imi, context)) {
676 defIm = imi;
677 Slog.i(TAG, "Selected default: " + imi.getId());
678 }
679 }
680 }
681 if (defIm == null && mMethodList.size() > 0) {
682 defIm = getMostApplicableDefaultIMELocked();
683 Slog.i(TAG, "No default found, using " + defIm.getId());
684 }
685 if (defIm != null) {
686 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
687 }
688 }
689
satok0a1bcf42012-05-16 19:26:31 +0900690 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
691 if (!mSystemReady) {
692 return false;
693 }
satok5b927c432012-05-01 20:09:34 +0900694 if (!isSystemIme(imi)) {
695 return false;
696 }
697 if (imi.getIsDefaultResourceId() != 0) {
698 try {
699 Resources res = context.createPackageContext(
700 imi.getPackageName(), 0).getResources();
701 if (res.getBoolean(imi.getIsDefaultResourceId())
702 && containsSubtypeOf(imi, context.getResources().getConfiguration().
703 locale.getLanguage())) {
704 return true;
705 }
706 } catch (PackageManager.NameNotFoundException ex) {
707 } catch (Resources.NotFoundException ex) {
708 }
709 }
710 if (imi.getSubtypeCount() == 0) {
711 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
712 }
713 return false;
714 }
715
716 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
717 if (!isSystemIme(imi)) {
718 return false;
719 }
720 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
721 }
722
723 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
724 final int N = imi.getSubtypeCount();
725 for (int i = 0; i < N; ++i) {
726 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
727 return true;
728 }
729 }
730 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 @Override
734 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
735 throws RemoteException {
736 try {
737 return super.onTransact(code, data, reply, flags);
738 } catch (RuntimeException e) {
739 // The input method manager only throws security exceptions, so let's
740 // log all others.
741 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800742 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 throw e;
745 }
746 }
747
Dianne Hackborn661cd522011-08-22 00:26:20 -0700748 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700749 synchronized (mMethodMap) {
750 if (!mSystemReady) {
751 mSystemReady = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700752 mKeyguardManager = (KeyguardManager)
753 mContext.getSystemService(Context.KEYGUARD_SERVICE);
754 mNotificationManager = (NotificationManager)
755 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
756 mStatusBar = statusBar;
757 statusBar.setIconVisibility("ime", false);
758 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900759 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
760 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900761 if (mShowOngoingImeSwitcherForPhones) {
762 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
763 mHardKeyboardListener);
764 }
satok0a1bcf42012-05-16 19:26:31 +0900765 buildInputMethodListLocked(mMethodList, mMethodMap);
766 if (!mImeSelectedOnBoot) {
767 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
768 checkCurrentLocaleChangedLocked();
769 }
770 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700771 try {
772 startInputInnerLocked();
773 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800774 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700775 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700776 }
777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800779
satok15452a42011-10-28 17:58:28 +0900780 private void setImeWindowVisibilityStatusHiddenLocked() {
781 mImeWindowVis = 0;
782 updateImeWindowStatusLocked();
783 }
784
satok3afd6c02011-11-18 08:38:19 +0900785 private void refreshImeWindowVisibilityLocked() {
786 final Configuration conf = mRes.getConfiguration();
787 final boolean haveHardKeyboard = conf.keyboard
788 != Configuration.KEYBOARD_NOKEYS;
789 final boolean hardKeyShown = haveHardKeyboard
790 && conf.hardKeyboardHidden
791 != Configuration.HARDKEYBOARDHIDDEN_YES;
792 final boolean isScreenLocked = mKeyguardManager != null
793 && mKeyguardManager.isKeyguardLocked()
794 && mKeyguardManager.isKeyguardSecure();
795 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
796 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
797 updateImeWindowStatusLocked();
798 }
799
satok15452a42011-10-28 17:58:28 +0900800 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900801 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700802 }
803
satoke7c6998e2011-06-03 17:57:59 +0900804 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 public List<InputMethodInfo> getInputMethodList() {
806 synchronized (mMethodMap) {
807 return new ArrayList<InputMethodInfo>(mMethodList);
808 }
809 }
810
satoke7c6998e2011-06-03 17:57:59 +0900811 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 public List<InputMethodInfo> getEnabledInputMethodList() {
813 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900814 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816 }
817
satokbb4aa062011-01-19 21:40:27 +0900818 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
819 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
820 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
821 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
822 for (InputMethodInfo imi: getEnabledInputMethodList()) {
823 enabledInputMethodAndSubtypes.put(
824 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
825 }
826 return enabledInputMethodAndSubtypes;
827 }
828
829 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
830 boolean allowsImplicitlySelectedSubtypes) {
831 if (imi == null && mCurMethodId != null) {
832 imi = mMethodMap.get(mCurMethodId);
833 }
satok7265d9b2011-02-14 15:47:30 +0900834 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900835 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900836 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900837 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900838 }
satok7265d9b2011-02-14 15:47:30 +0900839 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900840 }
841
satoke7c6998e2011-06-03 17:57:59 +0900842 @Override
satok16331c82010-12-20 23:48:46 +0900843 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
844 boolean allowsImplicitlySelectedSubtypes) {
satok67ddf9c2010-11-17 09:45:54 +0900845 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900846 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900847 }
848 }
849
satoke7c6998e2011-06-03 17:57:59 +0900850 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 public void addClient(IInputMethodClient client,
852 IInputContext inputContext, int uid, int pid) {
853 synchronized (mMethodMap) {
854 mClients.put(client.asBinder(), new ClientState(client,
855 inputContext, uid, pid));
856 }
857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800858
satoke7c6998e2011-06-03 17:57:59 +0900859 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 public void removeClient(IInputMethodClient client) {
861 synchronized (mMethodMap) {
862 mClients.remove(client.asBinder());
863 }
864 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 void executeOrSendMessage(IInterface target, Message msg) {
867 if (target.asBinder() instanceof Binder) {
868 mCaller.sendMessage(msg);
869 } else {
870 handleMessage(msg);
871 msg.recycle();
872 }
873 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800874
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700875 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800877 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 + mCurClient.client.asBinder());
879 if (mBoundToMethod) {
880 mBoundToMethod = false;
881 if (mCurMethod != null) {
882 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
883 MSG_UNBIND_INPUT, mCurMethod));
884 }
885 }
Dianne Hackborna6e41342012-05-22 16:30:34 -0700886
887 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
888 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
890 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
891 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800893
The Android Open Source Project10592532009-03-18 17:39:46 -0700894 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 }
896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 private int getImeShowFlags() {
899 int flags = 0;
900 if (mShowForced) {
901 flags |= InputMethod.SHOW_FORCED
902 | InputMethod.SHOW_EXPLICIT;
903 } else if (mShowExplicitlyRequested) {
904 flags |= InputMethod.SHOW_EXPLICIT;
905 }
906 return flags;
907 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 private int getAppShowFlags() {
910 int flags = 0;
911 if (mShowForced) {
912 flags |= InputMethodManager.SHOW_FORCED;
913 } else if (!mShowExplicitlyRequested) {
914 flags |= InputMethodManager.SHOW_IMPLICIT;
915 }
916 return flags;
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
Dianne Hackborn7663d802012-02-24 13:08:49 -0800919 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if (!mBoundToMethod) {
921 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
922 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
923 mBoundToMethod = true;
924 }
925 final SessionState session = mCurClient.curSession;
926 if (initial) {
927 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
928 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
929 } else {
930 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
931 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
932 }
933 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800934 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -0800935 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Dianne Hackborn7663d802012-02-24 13:08:49 -0800937 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -0800941 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // If no method is currently selected, do nothing.
943 if (mCurMethodId == null) {
944 return mNoBinding;
945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 ClientState cs = mClients.get(client.asBinder());
948 if (cs == null) {
949 throw new IllegalArgumentException("unknown client "
950 + client.asBinder());
951 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 try {
954 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
955 // Check with the window manager to make sure this client actually
956 // has a window with focus. If not, reject. This is thread safe
957 // because if the focus changes some time before or after, the
958 // next client receiving focus that has any interest in input will
959 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800960 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
962 return null;
963 }
964 } catch (RemoteException e) {
965 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800966
Dianne Hackborn7663d802012-02-24 13:08:49 -0800967 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
968 }
969
970 InputBindResult startInputUncheckedLocked(ClientState cs,
971 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
972 // If no method is currently selected, do nothing.
973 if (mCurMethodId == null) {
974 return mNoBinding;
975 }
976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (mCurClient != cs) {
978 // If the client is changing, we need to switch over to the new
979 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700980 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800981 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 + cs.client.asBinder());
983
984 // If the screen is on, inform the new client it is active
985 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -0700986 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
987 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 // Bump up the sequence for this client and attach it.
992 mCurSeq++;
993 if (mCurSeq <= 0) mCurSeq = 1;
994 mCurClient = cs;
995 mCurInputContext = inputContext;
996 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 // Check if the input method is changing.
999 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1000 if (cs.curSession != null) {
1001 // Fast case: if we are already connected to the input method,
1002 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001003 return attachNewInputLocked(
1004 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006 if (mHaveConnection) {
1007 if (mCurMethod != null) {
1008 if (!cs.sessionRequested) {
1009 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001010 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1012 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001013 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 // Return to client, and we will get back with it when
1016 // we have had a session made for it.
1017 return new InputBindResult(null, mCurId, mCurSeq);
1018 } else if (SystemClock.uptimeMillis()
1019 < (mLastBindTime+TIME_TO_RECONNECT)) {
1020 // In this case we have connected to the service, but
1021 // don't yet have its interface. If it hasn't been too
1022 // long since we did the connection, we'll return to
1023 // the client and wait to get the service interface so
1024 // we can report back. If it has been too long, we want
1025 // to fall through so we can try a disconnect/reconnect
1026 // to see if we can get back in touch with the service.
1027 return new InputBindResult(null, mCurId, mCurSeq);
1028 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001029 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1030 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032 }
1033 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001034
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001035 return startInputInnerLocked();
1036 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001037
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001038 InputBindResult startInputInnerLocked() {
1039 if (mCurMethodId == null) {
1040 return mNoBinding;
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001043 if (!mSystemReady) {
1044 // If the system is not yet ready, we shouldn't be running third
1045 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001046 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1050 if (info == null) {
1051 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001054 unbindCurrentMethodLocked(false);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1057 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001058 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1059 com.android.internal.R.string.input_method_binding_label);
1060 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1061 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001062 if (mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1063 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 mLastBindTime = SystemClock.uptimeMillis();
1065 mHaveConnection = true;
1066 mCurId = info.getId();
1067 mCurToken = new Binder();
1068 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001069 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 mIWindowManager.addWindowToken(mCurToken,
1071 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1072 } catch (RemoteException e) {
1073 }
1074 return new InputBindResult(null, mCurId, mCurSeq);
1075 } else {
1076 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 + mCurIntent);
1079 }
1080 return null;
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
satoke7c6998e2011-06-03 17:57:59 +09001083 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001085 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 synchronized (mMethodMap) {
1087 final long ident = Binder.clearCallingIdentity();
1088 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001089 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 } finally {
1091 Binder.restoreCallingIdentity(ident);
1092 }
1093 }
1094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
satoke7c6998e2011-06-03 17:57:59 +09001096 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 public void finishInput(IInputMethodClient client) {
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
satoke7c6998e2011-06-03 17:57:59 +09001100 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 public void onServiceConnected(ComponentName name, IBinder service) {
1102 synchronized (mMethodMap) {
1103 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1104 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001105 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001106 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborncc278702009-09-02 23:07:23 -07001107 unbindCurrentMethodLocked(false);
1108 return;
1109 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001111 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1112 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001114 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001115 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001117 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001118 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 }
1121 }
1122 }
1123
1124 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1125 synchronized (mMethodMap) {
1126 if (mCurMethod != null && method != null
1127 && mCurMethod.asBinder() == method.asBinder()) {
1128 if (mCurClient != null) {
1129 mCurClient.curSession = new SessionState(mCurClient,
1130 method, session);
1131 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001132 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 if (res.method != null) {
1134 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1135 MSG_BIND_METHOD, mCurClient.client, res));
1136 }
1137 }
1138 }
1139 }
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001142 void unbindCurrentMethodLocked(boolean reportToClient) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001143 if (mVisibleBound) {
1144 mContext.unbindService(mVisibleConnection);
1145 mVisibleBound = false;
1146 }
1147
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001148 if (mHaveConnection) {
1149 mContext.unbindService(this);
1150 mHaveConnection = false;
1151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001153 if (mCurToken != null) {
1154 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001155 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
satoke0a99412012-05-10 02:22:58 +09001156 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0) {
1157 // The current IME is shown. Hence an IME switch (transition) is happening.
1158 mWindowManagerService.saveLastInputMethodWindowForTransition();
1159 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001160 mIWindowManager.removeWindowToken(mCurToken);
1161 } catch (RemoteException e) {
1162 }
1163 mCurToken = null;
1164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
The Android Open Source Project10592532009-03-18 17:39:46 -07001166 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001167 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001169 if (reportToClient && mCurClient != null) {
1170 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1171 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1172 }
1173 }
1174
Devin Taylor0c33ed22010-02-23 13:26:46 -06001175 private void finishSession(SessionState sessionState) {
1176 if (sessionState != null && sessionState.session != null) {
1177 try {
1178 sessionState.session.finishSession();
1179 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001180 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001181 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001182 }
1183 }
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001186 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 if (mCurMethod != null) {
1188 for (ClientState cs : mClients.values()) {
1189 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001190 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 cs.curSession = null;
1192 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001193
1194 finishSession(mEnabledSession);
1195 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 mCurMethod = null;
1197 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001198 if (mStatusBar != null) {
1199 mStatusBar.setIconVisibility("ime", false);
1200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
satoke7c6998e2011-06-03 17:57:59 +09001203 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 public void onServiceDisconnected(ComponentName name) {
1205 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001206 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 + " mCurIntent=" + mCurIntent);
1208 if (mCurMethod != null && mCurIntent != null
1209 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001210 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 // We consider this to be a new bind attempt, since the system
1212 // should now try to restart the service for us.
1213 mLastBindTime = SystemClock.uptimeMillis();
1214 mShowRequested = mInputShown;
1215 mInputShown = false;
1216 if (mCurClient != null) {
1217 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1218 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1219 }
1220 }
1221 }
1222 }
1223
satokf9f01002011-05-19 21:31:50 +09001224 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001226 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 long ident = Binder.clearCallingIdentity();
1228 try {
1229 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001230 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 return;
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 synchronized (mMethodMap) {
1235 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001236 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001237 if (mStatusBar != null) {
1238 mStatusBar.setIconVisibility("ime", false);
1239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001241 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001242 CharSequence contentDescription = null;
1243 try {
1244 PackageManager packageManager = mContext.getPackageManager();
1245 contentDescription = packageManager.getApplicationLabel(
1246 packageManager.getApplicationInfo(packageName, 0));
1247 } catch (NameNotFoundException nnfe) {
1248 /* ignore */
1249 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001250 if (mStatusBar != null) {
1251 mStatusBar.setIcon("ime", packageName, iconId, 0,
1252 contentDescription != null
1253 ? contentDescription.toString() : null);
1254 mStatusBar.setIconVisibility("ime", true);
1255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257 }
1258 } finally {
1259 Binder.restoreCallingIdentity(ident);
1260 }
1261 }
1262
satok7cfc0ed2011-06-20 21:29:36 +09001263 private boolean needsToShowImeSwitchOngoingNotification() {
1264 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001265 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001266 synchronized (mMethodMap) {
1267 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1268 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001269 if (N > 2) return true;
1270 if (N < 1) return false;
1271 int nonAuxCount = 0;
1272 int auxCount = 0;
1273 InputMethodSubtype nonAuxSubtype = null;
1274 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001275 for(int i = 0; i < N; ++i) {
1276 final InputMethodInfo imi = imis.get(i);
1277 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1278 imi, true);
1279 final int subtypeCount = subtypes.size();
1280 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001281 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001282 } else {
1283 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001284 final InputMethodSubtype subtype = subtypes.get(j);
1285 if (!subtype.isAuxiliary()) {
1286 ++nonAuxCount;
1287 nonAuxSubtype = subtype;
1288 } else {
1289 ++auxCount;
1290 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001291 }
1292 }
1293 }
satok7cfc0ed2011-06-20 21:29:36 +09001294 }
satokb6359412011-06-28 17:47:41 +09001295 if (nonAuxCount > 1 || auxCount > 1) {
1296 return true;
1297 } else if (nonAuxCount == 1 && auxCount == 1) {
1298 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001299 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1300 || auxSubtype.overridesImplicitlyEnabledSubtype()
1301 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001302 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1303 return false;
1304 }
1305 return true;
1306 }
1307 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001308 }
satok7cfc0ed2011-06-20 21:29:36 +09001309 }
1310
satokdbf29502011-08-25 15:28:23 +09001311 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001312 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001313 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
satok06487a52010-10-29 11:37:18 +09001314 int uid = Binder.getCallingUid();
1315 long ident = Binder.clearCallingIdentity();
1316 try {
1317 if (token == null || mCurToken != token) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001318 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001319 return;
1320 }
1321
1322 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001323 mImeWindowVis = vis;
1324 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001325 if (mStatusBar != null) {
1326 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1327 }
satok7cfc0ed2011-06-20 21:29:36 +09001328 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001329 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1330 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
satok7cfc0ed2011-06-20 21:29:36 +09001331 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001332 final CharSequence title = mRes.getText(
1333 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001334 final CharSequence imiLabel = imi.loadLabel(pm);
1335 final CharSequence summary = mCurrentSubtype != null
1336 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1337 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1338 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001339 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001340 : imiLabel;
1341
satok7cfc0ed2011-06-20 21:29:36 +09001342 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001343 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001344 if (mNotificationManager != null) {
1345 mNotificationManager.notify(
1346 com.android.internal.R.string.select_input_method,
1347 mImeSwitcherNotification);
1348 mNotificationShown = true;
1349 }
satok7cfc0ed2011-06-20 21:29:36 +09001350 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001351 if (mNotificationShown && mNotificationManager != null) {
satok7cfc0ed2011-06-20 21:29:36 +09001352 mNotificationManager.cancel(
1353 com.android.internal.R.string.select_input_method);
1354 mNotificationShown = false;
1355 }
1356 }
satok06487a52010-10-29 11:37:18 +09001357 }
1358 } finally {
1359 Binder.restoreCallingIdentity(ident);
1360 }
1361 }
1362
satoke7c6998e2011-06-03 17:57:59 +09001363 @Override
satokf9f01002011-05-19 21:31:50 +09001364 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
1365 synchronized (mMethodMap) {
1366 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1367 for (int i = 0; i < spans.length; ++i) {
1368 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001369 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001370 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001371 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001372 }
1373 }
1374 }
1375 }
1376
satoke7c6998e2011-06-03 17:57:59 +09001377 @Override
satokf9f01002011-05-19 21:31:50 +09001378 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
1379 synchronized (mMethodMap) {
1380 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1381 // TODO: Do not send the intent if the process of the targetImi is already dead.
1382 if (targetImi != null) {
1383 final String[] suggestions = span.getSuggestions();
1384 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001385 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001386 final Intent intent = new Intent();
1387 // Ensures that only a class in the original IME package will receive the
1388 // notification.
satok42c5a162011-05-26 16:46:14 +09001389 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001390 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1391 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1392 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1393 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
1394 mContext.sendBroadcast(intent);
1395 return true;
1396 }
1397 }
1398 return false;
1399 }
1400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001402 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1403 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1404 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1405 // enabled.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 String id = Settings.Secure.getString(mContext.getContentResolver(),
satokab751aa2010-09-14 19:17:36 +09001407 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09001408 // There is no input method selected, try to choose new applicable input method.
1409 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
1410 id = Settings.Secure.getString(mContext.getContentResolver(),
1411 Settings.Secure.DEFAULT_INPUT_METHOD);
1412 }
1413 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 try {
satokab751aa2010-09-14 19:17:36 +09001415 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001417 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001418 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001419 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
satokf3db1af2010-11-23 13:34:33 +09001421 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001422 } else {
1423 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001424 mCurMethodId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001425 unbindCurrentMethodLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
satokab751aa2010-09-14 19:17:36 +09001429 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 InputMethodInfo info = mMethodMap.get(id);
1431 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001432 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001434
satokd81e9502012-05-21 12:58:45 +09001435 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001437 final int subtypeCount = info.getSubtypeCount();
1438 if (subtypeCount <= 0) {
1439 return;
satokcd7cd292010-11-20 15:46:23 +09001440 }
satokd81e9502012-05-21 12:58:45 +09001441 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1442 final InputMethodSubtype newSubtype;
1443 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1444 newSubtype = info.getSubtypeAt(subtypeId);
1445 } else {
1446 // If subtype is null, try to find the most applicable one from
1447 // getCurrentInputMethodSubtype.
1448 newSubtype = getCurrentInputMethodSubtype();
1449 }
1450 if (newSubtype == null || oldSubtype == null) {
1451 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1452 + ", new subtype = " + newSubtype);
1453 return;
1454 }
1455 if (newSubtype != oldSubtype) {
1456 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1457 if (mCurMethod != null) {
1458 try {
1459 refreshImeWindowVisibilityLocked();
1460 mCurMethod.changeInputMethodSubtype(newSubtype);
1461 } catch (RemoteException e) {
1462 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001463 }
1464 }
1465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 return;
1467 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001468
satokd81e9502012-05-21 12:58:45 +09001469 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 final long ident = Binder.clearCallingIdentity();
1471 try {
satokab751aa2010-09-14 19:17:36 +09001472 // Set a subtype to this input method.
1473 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001474 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1475 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1476 // because mCurMethodId is stored as a history in
1477 // setSelectedInputMethodAndSubtypeLocked().
1478 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479
1480 if (ActivityManagerNative.isSystemReady()) {
1481 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001482 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 intent.putExtra("input_method_id", id);
1484 mContext.sendBroadcast(intent);
1485 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001486 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 } finally {
1488 Binder.restoreCallingIdentity(ident);
1489 }
1490 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001491
satok42c5a162011-05-26 16:46:14 +09001492 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001493 public boolean showSoftInput(IInputMethodClient client, int flags,
1494 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001495 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 long ident = Binder.clearCallingIdentity();
1497 try {
1498 synchronized (mMethodMap) {
1499 if (mCurClient == null || client == null
1500 || mCurClient.client.asBinder() != client.asBinder()) {
1501 try {
1502 // We need to check if this is the current client with
1503 // focus in the window manager, to allow this call to
1504 // be made before input is started in it.
1505 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001506 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001507 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001510 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001513
Joe Onorato8a9b2202010-02-26 18:56:32 -08001514 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001515 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517 } finally {
1518 Binder.restoreCallingIdentity(ident);
1519 }
1520 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001521
The Android Open Source Project4df24232009-03-05 14:34:35 -08001522 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 mShowRequested = true;
1524 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1525 mShowExplicitlyRequested = true;
1526 }
1527 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1528 mShowExplicitlyRequested = true;
1529 mShowForced = true;
1530 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
Dianne Hackborncc278702009-09-02 23:07:23 -07001532 if (!mSystemReady) {
1533 return false;
1534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001535
The Android Open Source Project4df24232009-03-05 14:34:35 -08001536 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001538 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1539 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1540 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001542 if (mHaveConnection && !mVisibleBound) {
1543 mContext.bindService(mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
1544 mVisibleBound = true;
1545 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001546 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001548 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 // The client has asked to have the input method shown, but
1550 // we have been sitting here too long with a connection to the
1551 // service and no interface received, so let's disconnect/connect
1552 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001553 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001555 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 mContext.unbindService(this);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001557 mContext.bindService(mCurIntent, this, Context.BIND_AUTO_CREATE
1558 | Context.BIND_NOT_VISIBLE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
The Android Open Source Project4df24232009-03-05 14:34:35 -08001561 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001563
satok42c5a162011-05-26 16:46:14 +09001564 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001565 public boolean hideSoftInput(IInputMethodClient client, int flags,
1566 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001567 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 long ident = Binder.clearCallingIdentity();
1569 try {
1570 synchronized (mMethodMap) {
1571 if (mCurClient == null || client == null
1572 || mCurClient.client.asBinder() != client.asBinder()) {
1573 try {
1574 // We need to check if this is the current client with
1575 // focus in the window manager, to allow this call to
1576 // be made before input is started in it.
1577 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001578 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1579 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001580 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001581 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001584 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001585 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
1587 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001588
Joe Onorato8a9b2202010-02-26 18:56:32 -08001589 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001590 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592 } finally {
1593 Binder.restoreCallingIdentity(ident);
1594 }
1595 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001596
The Android Open Source Project4df24232009-03-05 14:34:35 -08001597 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1599 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001602 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
1604 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001605 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001607 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001609 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001611 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1612 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1613 res = true;
1614 } else {
1615 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001617 if (mHaveConnection && mVisibleBound) {
1618 mContext.unbindService(mVisibleConnection);
1619 mVisibleBound = false;
1620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 mInputShown = false;
1622 mShowRequested = false;
1623 mShowExplicitlyRequested = false;
1624 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001625 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001627
satok42c5a162011-05-26 16:46:14 +09001628 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001629 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1630 int controlFlags, int softInputMode, int windowFlags,
1631 EditorInfo attribute, IInputContext inputContext) {
1632 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 long ident = Binder.clearCallingIdentity();
1634 try {
1635 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001637 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001639 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
Dianne Hackborn7663d802012-02-24 13:08:49 -08001641 ClientState cs = mClients.get(client.asBinder());
1642 if (cs == null) {
1643 throw new IllegalArgumentException("unknown client "
1644 + client.asBinder());
1645 }
1646
1647 try {
1648 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1649 // Check with the window manager to make sure this client actually
1650 // has a window with focus. If not, reject. This is thread safe
1651 // because if the focus changes some time before or after, the
1652 // next client receiving focus that has any interest in input will
1653 // be calling through here after that change happens.
1654 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1655 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1656 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001658 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001661 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001662 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
1663 + " attribute=" + attribute);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001664 if (attribute != null) {
1665 return startInputUncheckedLocked(cs, inputContext, attribute,
1666 controlFlags);
1667 }
1668 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001669 }
1670 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001671
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001672 // Should we auto-show the IME even if the caller has not
1673 // specified what should be done with it?
1674 // We only do this automatically if the window can resize
1675 // to accommodate the IME (so what the user sees will give
1676 // them good context without input information being obscured
1677 // by the IME) or if running on a large screen where there
1678 // is more room for the target window + IME.
1679 final boolean doAutoShow =
1680 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1681 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1682 || mRes.getConfiguration().isLayoutSizeAtLeast(
1683 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001684 final boolean isTextEditor =
1685 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1686
1687 // We want to start input before showing the IME, but after closing
1688 // it. We want to do this after closing it to help the IME disappear
1689 // more quickly (not get stuck behind it initializing itself for the
1690 // new focused input, even if its window wants to hide the IME).
1691 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1694 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001695 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1697 // There is no focus view, and this window will
1698 // be behind any soft input window, so hide the
1699 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001701 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001703 } else if (isTextEditor && doAutoShow && (softInputMode &
1704 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 // There is a focus view, and we are navigating forward
1706 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001707 // We only do this automatically if the window can resize
1708 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001709 // them good context without input information being obscured
1710 // by the IME) or if running on a large screen where there
1711 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001712 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001713 if (attribute != null) {
1714 res = startInputUncheckedLocked(cs, inputContext, attribute,
1715 controlFlags);
1716 didStart = true;
1717 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001718 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 }
1720 break;
1721 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1722 // Do nothing.
1723 break;
1724 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1725 if ((softInputMode &
1726 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001727 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001728 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730 break;
1731 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001732 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001733 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 break;
1735 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1736 if ((softInputMode &
1737 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001738 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001739 if (attribute != null) {
1740 res = startInputUncheckedLocked(cs, inputContext, attribute,
1741 controlFlags);
1742 didStart = true;
1743 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001744 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
1746 break;
1747 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001748 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001749 if (attribute != null) {
1750 res = startInputUncheckedLocked(cs, inputContext, attribute,
1751 controlFlags);
1752 didStart = true;
1753 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 break;
1756 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001757
1758 if (!didStart && attribute != null) {
1759 res = startInputUncheckedLocked(cs, inputContext, attribute,
1760 controlFlags);
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763 } finally {
1764 Binder.restoreCallingIdentity(ident);
1765 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001766
1767 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001769
satok42c5a162011-05-26 16:46:14 +09001770 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 public void showInputMethodPickerFromClient(IInputMethodClient client) {
1772 synchronized (mMethodMap) {
1773 if (mCurClient == null || client == null
1774 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001775 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001776 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778
satok440aab52010-11-25 09:43:11 +09001779 // Always call subtype picker, because subtype picker is a superset of input method
1780 // picker.
satokab751aa2010-09-14 19:17:36 +09001781 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1782 }
1783 }
1784
satok42c5a162011-05-26 16:46:14 +09001785 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 public void setInputMethod(IBinder token, String id) {
satok28203512010-11-24 11:06:49 +09001787 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1788 }
1789
satok42c5a162011-05-26 16:46:14 +09001790 @Override
satok28203512010-11-24 11:06:49 +09001791 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
1792 synchronized (mMethodMap) {
1793 if (subtype != null) {
1794 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1795 mMethodMap.get(id), subtype.hashCode()));
1796 } else {
1797 setInputMethod(token, id);
1798 }
1799 }
satokab751aa2010-09-14 19:17:36 +09001800 }
1801
satok42c5a162011-05-26 16:46:14 +09001802 @Override
satokb416a712010-11-25 20:42:14 +09001803 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09001804 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09001805 synchronized (mMethodMap) {
1806 if (mCurClient == null || client == null
1807 || mCurClient.client.asBinder() != client.asBinder()) {
1808 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
1809 }
satok7fee71f2010-12-17 18:54:26 +09001810 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1811 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09001812 }
1813 }
1814
satok4fc87d62011-05-20 16:13:43 +09001815 @Override
satok735cf382010-11-11 20:40:09 +09001816 public boolean switchToLastInputMethod(IBinder token) {
1817 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09001818 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09001819 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09001820 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09001821 lastImi = mMethodMap.get(lastIme.first);
1822 } else {
1823 lastImi = null;
satok735cf382010-11-11 20:40:09 +09001824 }
satok4fc87d62011-05-20 16:13:43 +09001825 String targetLastImiId = null;
1826 int subtypeId = NOT_A_SUBTYPE_ID;
1827 if (lastIme != null && lastImi != null) {
1828 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
1829 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
1830 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
1831 : mCurrentSubtype.hashCode();
1832 // If the last IME is the same as the current IME and the last subtype is not
1833 // defined, there is no need to switch to the last IME.
1834 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
1835 targetLastImiId = lastIme.first;
1836 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1837 }
1838 }
1839
1840 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
1841 // This is a safety net. If the currentSubtype can't be added to the history
1842 // and the framework couldn't find the last ime, we will make the last ime be
1843 // the most applicable enabled keyboard subtype of the system imes.
1844 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1845 if (enabled != null) {
1846 final int N = enabled.size();
1847 final String locale = mCurrentSubtype == null
1848 ? mRes.getConfiguration().locale.toString()
1849 : mCurrentSubtype.getLocale();
1850 for (int i = 0; i < N; ++i) {
1851 final InputMethodInfo imi = enabled.get(i);
1852 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
1853 InputMethodSubtype keyboardSubtype =
1854 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
1855 SUBTYPE_MODE_KEYBOARD, locale, true);
1856 if (keyboardSubtype != null) {
1857 targetLastImiId = imi.getId();
1858 subtypeId = getSubtypeIdFromHashCode(
1859 imi, keyboardSubtype.hashCode());
1860 if(keyboardSubtype.getLocale().equals(locale)) {
1861 break;
1862 }
1863 }
1864 }
1865 }
1866 }
1867 }
1868
1869 if (!TextUtils.isEmpty(targetLastImiId)) {
1870 if (DEBUG) {
1871 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
1872 + ", from: " + mCurMethodId + ", " + subtypeId);
1873 }
1874 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
1875 return true;
1876 } else {
1877 return false;
1878 }
satok735cf382010-11-11 20:40:09 +09001879 }
1880 }
1881
satoke7c6998e2011-06-03 17:57:59 +09001882 @Override
satok688bd472012-02-09 20:09:17 +09001883 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
1884 synchronized (mMethodMap) {
1885 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
1886 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
1887 if (nextSubtype == null) {
1888 return false;
1889 }
1890 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
1891 return true;
1892 }
1893 }
1894
1895 @Override
satok68f1b782011-04-11 14:26:04 +09001896 public InputMethodSubtype getLastInputMethodSubtype() {
1897 synchronized (mMethodMap) {
1898 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
1899 // TODO: Handle the case of the last IME with no subtypes
1900 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
1901 || TextUtils.isEmpty(lastIme.second)) return null;
1902 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
1903 if (lastImi == null) return null;
1904 try {
1905 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09001906 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
1907 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
1908 return null;
1909 }
1910 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09001911 } catch (NumberFormatException e) {
1912 return null;
1913 }
1914 }
1915 }
1916
satoke7c6998e2011-06-03 17:57:59 +09001917 @Override
satokee5e77c2011-09-02 18:50:15 +09001918 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09001919 // By this IPC call, only a process which shares the same uid with the IME can add
1920 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09001921 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09001922 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09001923 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09001924 if (imi == null) return;
satok91e88122011-07-18 11:11:42 +09001925 final PackageManager pm = mContext.getPackageManager();
1926 final String[] packageInfos = pm.getPackagesForUid(Binder.getCallingUid());
1927 if (packageInfos != null) {
1928 final int packageNum = packageInfos.length;
1929 for (int i = 0; i < packageNum; ++i) {
1930 if (packageInfos[i].equals(imi.getPackageName())) {
1931 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09001932 final long ident = Binder.clearCallingIdentity();
1933 try {
1934 buildInputMethodListLocked(mMethodList, mMethodMap);
1935 } finally {
1936 Binder.restoreCallingIdentity(ident);
1937 }
satokee5e77c2011-09-02 18:50:15 +09001938 return;
satok91e88122011-07-18 11:11:42 +09001939 }
1940 }
1941 }
satoke7c6998e2011-06-03 17:57:59 +09001942 }
satokee5e77c2011-09-02 18:50:15 +09001943 return;
satoke7c6998e2011-06-03 17:57:59 +09001944 }
1945
satok28203512010-11-24 11:06:49 +09001946 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 synchronized (mMethodMap) {
1948 if (token == null) {
1949 if (mContext.checkCallingOrSelfPermission(
1950 android.Manifest.permission.WRITE_SECURE_SETTINGS)
1951 != PackageManager.PERMISSION_GRANTED) {
1952 throw new SecurityException(
1953 "Using null token requires permission "
1954 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
1955 }
1956 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001957 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
1958 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 return;
1960 }
1961
satokc5933802011-08-31 21:26:04 +09001962 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 try {
satokab751aa2010-09-14 19:17:36 +09001964 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 } finally {
1966 Binder.restoreCallingIdentity(ident);
1967 }
1968 }
1969 }
1970
satok42c5a162011-05-26 16:46:14 +09001971 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 public void hideMySoftInput(IBinder token, int flags) {
1973 synchronized (mMethodMap) {
1974 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001975 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
1976 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 return;
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 long ident = Binder.clearCallingIdentity();
1980 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001981 hideCurrentInputLocked(flags, null);
1982 } finally {
1983 Binder.restoreCallingIdentity(ident);
1984 }
1985 }
1986 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001987
satok42c5a162011-05-26 16:46:14 +09001988 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001989 public void showMySoftInput(IBinder token, int flags) {
1990 synchronized (mMethodMap) {
1991 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001992 Slog.w(TAG, "Ignoring showMySoftInput of uid "
1993 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 return;
1995 }
1996 long ident = Binder.clearCallingIdentity();
1997 try {
1998 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 } finally {
2000 Binder.restoreCallingIdentity(ident);
2001 }
2002 }
2003 }
2004
2005 void setEnabledSessionInMainThread(SessionState session) {
2006 if (mEnabledSession != session) {
2007 if (mEnabledSession != null) {
2008 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002009 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 mEnabledSession.method.setSessionEnabled(
2011 mEnabledSession.session, false);
2012 } catch (RemoteException e) {
2013 }
2014 }
2015 mEnabledSession = session;
2016 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002017 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 session.method.setSessionEnabled(
2019 session.session, true);
2020 } catch (RemoteException e) {
2021 }
2022 }
2023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002024
satok42c5a162011-05-26 16:46:14 +09002025 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 public boolean handleMessage(Message msg) {
2027 HandlerCaller.SomeArgs args;
2028 switch (msg.what) {
2029 case MSG_SHOW_IM_PICKER:
2030 showInputMethodMenu();
2031 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002032
satokab751aa2010-09-14 19:17:36 +09002033 case MSG_SHOW_IM_SUBTYPE_PICKER:
2034 showInputMethodSubtypeMenu();
2035 return true;
2036
satok47a44912010-10-06 16:03:58 +09002037 case MSG_SHOW_IM_SUBTYPE_ENABLER:
satok217f5482010-12-15 05:19:19 +09002038 args = (HandlerCaller.SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002039 showInputMethodAndSubtypeEnabler((String)args.arg1);
satok217f5482010-12-15 05:19:19 +09002040 return true;
2041
2042 case MSG_SHOW_IM_CONFIG:
2043 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002044 return true;
2045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 case MSG_UNBIND_INPUT:
2049 try {
2050 ((IInputMethod)msg.obj).unbindInput();
2051 } catch (RemoteException e) {
2052 // There is nothing interesting about the method dying.
2053 }
2054 return true;
2055 case MSG_BIND_INPUT:
2056 args = (HandlerCaller.SomeArgs)msg.obj;
2057 try {
2058 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2059 } catch (RemoteException e) {
2060 }
2061 return true;
2062 case MSG_SHOW_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002063 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002064 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002065 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2066 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 } catch (RemoteException e) {
2068 }
2069 return true;
2070 case MSG_HIDE_SOFT_INPUT:
The Android Open Source Project4df24232009-03-05 14:34:35 -08002071 args = (HandlerCaller.SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002073 ((IInputMethod)args.arg1).hideSoftInput(0,
2074 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 } catch (RemoteException e) {
2076 }
2077 return true;
2078 case MSG_ATTACH_TOKEN:
2079 args = (HandlerCaller.SomeArgs)msg.obj;
2080 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002081 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2083 } catch (RemoteException e) {
2084 }
2085 return true;
2086 case MSG_CREATE_SESSION:
2087 args = (HandlerCaller.SomeArgs)msg.obj;
2088 try {
2089 ((IInputMethod)args.arg1).createSession(
2090 (IInputMethodCallback)args.arg2);
2091 } catch (RemoteException e) {
2092 }
2093 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 case MSG_START_INPUT:
2097 args = (HandlerCaller.SomeArgs)msg.obj;
2098 try {
2099 SessionState session = (SessionState)args.arg1;
2100 setEnabledSessionInMainThread(session);
2101 session.method.startInput((IInputContext)args.arg2,
2102 (EditorInfo)args.arg3);
2103 } catch (RemoteException e) {
2104 }
2105 return true;
2106 case MSG_RESTART_INPUT:
2107 args = (HandlerCaller.SomeArgs)msg.obj;
2108 try {
2109 SessionState session = (SessionState)args.arg1;
2110 setEnabledSessionInMainThread(session);
2111 session.method.restartInput((IInputContext)args.arg2,
2112 (EditorInfo)args.arg3);
2113 } catch (RemoteException e) {
2114 }
2115 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 case MSG_UNBIND_METHOD:
2120 try {
2121 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2122 } catch (RemoteException e) {
2123 // There is nothing interesting about the last client dying.
2124 }
2125 return true;
2126 case MSG_BIND_METHOD:
2127 args = (HandlerCaller.SomeArgs)msg.obj;
2128 try {
2129 ((IInputMethodClient)args.arg1).onBindMethod(
2130 (InputBindResult)args.arg2);
2131 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002132 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
2134 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002135 case MSG_SET_ACTIVE:
2136 try {
2137 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2138 } catch (RemoteException e) {
2139 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2140 + ((ClientState)msg.obj).pid + " uid "
2141 + ((ClientState)msg.obj).uid);
2142 }
2143 return true;
satok01038492012-04-09 21:08:27 +09002144
2145 // --------------------------------------------------------------
2146 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2147 mHardKeyboardListener.handleHardKeyboardStatusChange(
2148 msg.arg1 == 1, msg.arg2 == 1);
2149 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151 return false;
2152 }
2153
satok5b927c432012-05-01 20:09:34 +09002154 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002155 return (inputMethod.getServiceInfo().applicationInfo.flags
2156 & ApplicationInfo.FLAG_SYSTEM) != 0;
2157 }
2158
Ken Wakasa586f0512011-01-20 22:31:01 +09002159 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2160 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2161 final int subtypeCount = imi.getSubtypeCount();
2162 for (int i = 0; i < subtypeCount; ++i) {
2163 subtypes.add(imi.getSubtypeAt(i));
2164 }
2165 return subtypes;
2166 }
2167
satoka86f5e42011-09-02 17:12:42 +09002168 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2169 InputMethodInfo imi, String mode) {
2170 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2171 final int subtypeCount = imi.getSubtypeCount();
2172 for (int i = 0; i < subtypeCount; ++i) {
2173 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2174 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2175 subtypes.add(subtype);
2176 }
2177 }
2178 return subtypes;
2179 }
2180
satokdc9ddae2011-10-06 12:22:36 +09002181 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002182 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002183 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002184 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002185 int i = enabled.size();
2186 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002187 while (i > 0) {
2188 i--;
satokdc9ddae2011-10-06 12:22:36 +09002189 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002190 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2191 return imi;
2192 }
2193 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2194 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002195 }
2196 }
satok0a1bcf42012-05-16 19:26:31 +09002197 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002198 }
2199 return null;
2200 }
2201
2202 private boolean chooseNewDefaultIMELocked() {
2203 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2204 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002205 if (DEBUG) {
2206 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2207 }
satok723a27e2010-11-11 14:58:11 +09002208 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002209 return true;
2210 }
2211
2212 return false;
2213 }
2214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2216 HashMap<String, InputMethodInfo> map) {
2217 list.clear();
2218 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002221 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002222 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
2223 String disabledSysImes = Settings.Secure.getString(mContext.getContentResolver(),
2224 Secure.DISABLED_SYSTEM_INPUT_METHODS);
2225 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226
2227 List<ResolveInfo> services = pm.queryIntentServices(
2228 new Intent(InputMethod.SERVICE_INTERFACE),
2229 PackageManager.GET_META_DATA);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002230
satoke7c6998e2011-06-03 17:57:59 +09002231 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2232 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 for (int i = 0; i < services.size(); ++i) {
2234 ResolveInfo ri = services.get(i);
2235 ServiceInfo si = ri.serviceInfo;
2236 ComponentName compName = new ComponentName(si.packageName, si.name);
2237 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2238 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002239 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 + ": it does not require the permission "
2241 + android.Manifest.permission.BIND_INPUT_METHOD);
2242 continue;
2243 }
2244
Joe Onorato8a9b2202010-02-26 18:56:32 -08002245 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246
2247 try {
satoke7c6998e2011-06-03 17:57:59 +09002248 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002250 final String id = p.getId();
2251 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252
satok5b927c432012-05-01 20:09:34 +09002253 // Valid system default IMEs and IMEs that have English subtypes are enabled
2254 // by default, unless there's a hard keyboard and the system IME was explicitly
2255 // disabled
2256 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2257 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002258 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002259 }
2260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002262 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002266 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002268 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 }
2270 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002271
satok0a1bcf42012-05-16 19:26:31 +09002272 final String defaultImiId = Settings.Secure.getString(mContext
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002273 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satok0a1bcf42012-05-16 19:26:31 +09002274 if (!TextUtils.isEmpty(defaultImiId)) {
2275 if (!map.containsKey(defaultImiId)) {
2276 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2277 if (chooseNewDefaultIMELocked()) {
2278 updateFromSettingsLocked();
2279 }
2280 } else {
2281 // Double check that the default IME is certainly enabled.
2282 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002283 }
2284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002288
satokab751aa2010-09-14 19:17:36 +09002289 private void showInputMethodMenu() {
2290 showInputMethodMenuInternal(false);
2291 }
2292
2293 private void showInputMethodSubtypeMenu() {
2294 showInputMethodMenuInternal(true);
2295 }
2296
satok217f5482010-12-15 05:19:19 +09002297 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002298 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002299 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002300 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2301 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002302 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002303 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002304 }
satok217f5482010-12-15 05:19:19 +09002305 mContext.startActivity(intent);
2306 }
2307
2308 private void showConfigureInputMethods() {
2309 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2310 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2311 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2312 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok47a44912010-10-06 16:03:58 +09002313 mContext.startActivity(intent);
2314 }
2315
satok2c93efc2012-04-02 19:33:47 +09002316 private boolean isScreenLocked() {
2317 return mKeyguardManager != null
2318 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2319 }
satokab751aa2010-09-14 19:17:36 +09002320 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002321 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 final Context context = mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 final PackageManager pm = context.getPackageManager();
satok2c93efc2012-04-02 19:33:47 +09002325 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002326
satok2c93efc2012-04-02 19:33:47 +09002327 final String lastInputMethodId = Settings.Secure.getString(context
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
satokab751aa2010-09-14 19:17:36 +09002329 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002330 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002331
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002332 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002333 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2334 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002335 if (immis == null || immis.size() == 0) {
2336 return;
2337 }
2338
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002339 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340
satok688bd472012-02-09 20:09:17 +09002341 final List<ImeSubtypeListItem> imList =
2342 mImListManager.getSortedInputMethodAndSubtypeList(
2343 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002344
satokc3690562012-01-10 20:14:43 +09002345 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
2346 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtype();
2347 if (currentSubtype != null) {
2348 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2349 lastInputMethodSubtypeId =
2350 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2351 }
2352 }
2353
Ken Wakasa761eb372011-03-04 19:06:18 +09002354 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002355 mIms = new InputMethodInfo[N];
2356 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002357 int checkedItem = 0;
2358 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002359 final ImeSubtypeListItem item = imList.get(i);
2360 mIms[i] = item.mImi;
2361 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002362 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002363 int subtypeId = mSubtypeIds[i];
2364 if ((subtypeId == NOT_A_SUBTYPE_ID)
2365 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2366 || (subtypeId == lastInputMethodSubtypeId)) {
2367 checkedItem = i;
2368 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002371 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002372 com.android.internal.R.styleable.DialogPreference,
2373 com.android.internal.R.attr.alertDialogStyle, 0);
2374 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002375 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002376 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002377 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002379 }
2380 })
2381 .setIcon(a.getDrawable(
2382 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2383 a.recycle();
satok01038492012-04-09 21:08:27 +09002384 final LayoutInflater inflater =
2385 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2386 final View tv = inflater.inflate(
2387 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2388 mDialogBuilder.setCustomTitle(tv);
2389
2390 // Setup layout for a toggle switch of the hardware keyboard
2391 mSwitchingDialogTitleView = tv;
2392 mSwitchingDialogTitleView.findViewById(
2393 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2394 mWindowManagerService.isHardKeyboardAvailable() ?
2395 View.VISIBLE : View.GONE);
2396 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2397 com.android.internal.R.id.hard_keyboard_switch));
2398 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2399 hardKeySwitch.setOnCheckedChangeListener(
2400 new OnCheckedChangeListener() {
2401 @Override
2402 public void onCheckedChanged(
2403 CompoundButton buttonView, boolean isChecked) {
2404 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2405 }
2406 });
satokd87c2592010-09-29 11:52:06 +09002407
Ken Wakasa05dbb652011-08-22 15:22:43 +09002408 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2409 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2410 checkedItem);
2411
2412 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002413 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002414 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002415 public void onClick(DialogInterface dialog, int which) {
2416 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002417 if (mIms == null || mIms.length <= which
2418 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002419 return;
2420 }
2421 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002422 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002423 hideInputMethodMenu();
2424 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002425 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002426 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002427 subtypeId = NOT_A_SUBTYPE_ID;
2428 }
2429 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002430 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002433 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434
satokbc81b692011-08-26 16:22:22 +09002435 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002436 mDialogBuilder.setPositiveButton(
2437 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002438 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002439 @Override
satok7f35c8c2010-10-07 21:13:11 +09002440 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002441 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002442 }
2443 });
2444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002446 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 mSwitchingDialog.getWindow().setType(
2448 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002449 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 mSwitchingDialog.show();
2451 }
2452 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002453
satok93d744d2012-05-09 17:14:08 +09002454 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002455 public final CharSequence mImeName;
2456 public final CharSequence mSubtypeName;
2457 public final InputMethodInfo mImi;
2458 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002459 private final boolean mIsSystemLocale;
2460 private final boolean mIsSystemLanguage;
2461
Ken Wakasa05dbb652011-08-22 15:22:43 +09002462 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002463 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002464 mImeName = imeName;
2465 mSubtypeName = subtypeName;
2466 mImi = imi;
2467 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002468 if (TextUtils.isEmpty(subtypeLocale)) {
2469 mIsSystemLocale = false;
2470 mIsSystemLanguage = false;
2471 } else {
2472 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2473 mIsSystemLanguage = mIsSystemLocale
2474 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2475 }
2476 }
2477
2478 @Override
2479 public int compareTo(ImeSubtypeListItem other) {
2480 if (TextUtils.isEmpty(mImeName)) {
2481 return 1;
2482 }
2483 if (TextUtils.isEmpty(other.mImeName)) {
2484 return -1;
2485 }
2486 if (!TextUtils.equals(mImeName, other.mImeName)) {
2487 return mImeName.toString().compareTo(other.mImeName.toString());
2488 }
2489 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2490 return 0;
2491 }
2492 if (mIsSystemLocale) {
2493 return -1;
2494 }
2495 if (other.mIsSystemLocale) {
2496 return 1;
2497 }
2498 if (mIsSystemLanguage) {
2499 return -1;
2500 }
2501 if (other.mIsSystemLanguage) {
2502 return 1;
2503 }
2504 if (TextUtils.isEmpty(mSubtypeName)) {
2505 return 1;
2506 }
2507 if (TextUtils.isEmpty(other.mSubtypeName)) {
2508 return -1;
2509 }
2510 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002511 }
2512 }
2513
2514 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2515 private final LayoutInflater mInflater;
2516 private final int mTextViewResourceId;
2517 private final List<ImeSubtypeListItem> mItemsList;
2518 private final int mCheckedItem;
2519 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2520 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2521 super(context, textViewResourceId, itemsList);
2522 mTextViewResourceId = textViewResourceId;
2523 mItemsList = itemsList;
2524 mCheckedItem = checkedItem;
2525 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2526 }
2527
2528 @Override
2529 public View getView(int position, View convertView, ViewGroup parent) {
2530 final View view = convertView != null ? convertView
2531 : mInflater.inflate(mTextViewResourceId, null);
2532 if (position < 0 || position >= mItemsList.size()) return view;
2533 final ImeSubtypeListItem item = mItemsList.get(position);
2534 final CharSequence imeName = item.mImeName;
2535 final CharSequence subtypeName = item.mSubtypeName;
2536 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2537 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2538 if (TextUtils.isEmpty(subtypeName)) {
2539 firstTextView.setText(imeName);
2540 secondTextView.setVisibility(View.GONE);
2541 } else {
2542 firstTextView.setText(subtypeName);
2543 secondTextView.setText(imeName);
2544 secondTextView.setVisibility(View.VISIBLE);
2545 }
2546 final RadioButton radioButton =
2547 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2548 radioButton.setChecked(position == mCheckedItem);
2549 return view;
2550 }
2551 }
2552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002554 synchronized (mMethodMap) {
2555 hideInputMethodMenuLocked();
2556 }
2557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002558
The Android Open Source Project10592532009-03-18 17:39:46 -07002559 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002560 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561
The Android Open Source Project10592532009-03-18 17:39:46 -07002562 if (mSwitchingDialog != null) {
2563 mSwitchingDialog.dismiss();
2564 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002566
The Android Open Source Project10592532009-03-18 17:39:46 -07002567 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002568 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002572
satok42c5a162011-05-26 16:46:14 +09002573 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 public boolean setInputMethodEnabled(String id, boolean enabled) {
2575 synchronized (mMethodMap) {
2576 if (mContext.checkCallingOrSelfPermission(
2577 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2578 != PackageManager.PERMISSION_GRANTED) {
2579 throw new SecurityException(
2580 "Requires permission "
2581 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2582 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 long ident = Binder.clearCallingIdentity();
2585 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002586 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 } finally {
2588 Binder.restoreCallingIdentity(ident);
2589 }
2590 }
2591 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002592
2593 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2594 // Make sure this is a valid input method.
2595 InputMethodInfo imm = mMethodMap.get(id);
2596 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002597 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002598 }
2599
satokd87c2592010-09-29 11:52:06 +09002600 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2601 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002602
satokd87c2592010-09-29 11:52:06 +09002603 if (enabled) {
2604 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2605 if (pair.first.equals(id)) {
2606 // We are enabling this input method, but it is already enabled.
2607 // Nothing to do. The previous state was enabled.
2608 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002609 }
2610 }
satokd87c2592010-09-29 11:52:06 +09002611 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2612 // Previous state was disabled.
2613 return false;
2614 } else {
2615 StringBuilder builder = new StringBuilder();
2616 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2617 builder, enabledInputMethodsList, id)) {
2618 // Disabled input method is currently selected, switch to another one.
2619 String selId = Settings.Secure.getString(mContext.getContentResolver(),
2620 Settings.Secure.DEFAULT_INPUT_METHOD);
satok03eb319a2010-11-11 18:17:42 +09002621 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2622 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2623 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002624 }
2625 // Previous state was enabled.
2626 return true;
2627 } else {
2628 // We are disabling the input method but it is already disabled.
2629 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002630 return false;
2631 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002632 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002633 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002634
satok57ffc002011-01-25 00:11:47 +09002635 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2636 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002637 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002638 }
2639
satok723a27e2010-11-11 14:58:11 +09002640 private void saveCurrentInputMethodAndSubtypeToHistory() {
2641 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2642 if (mCurrentSubtype != null) {
2643 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2644 }
satok57ffc002011-01-25 00:11:47 +09002645 if (canAddToLastInputMethod(mCurrentSubtype)) {
2646 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2647 }
satokab751aa2010-09-14 19:17:36 +09002648 }
2649
satok723a27e2010-11-11 14:58:11 +09002650 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2651 boolean setSubtypeOnly) {
2652 // Update the history of InputMethod and Subtype
2653 saveCurrentInputMethodAndSubtypeToHistory();
2654
2655 // Set Subtype here
2656 if (imi == null || subtypeId < 0) {
2657 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002658 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002659 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002660 if (subtypeId < imi.getSubtypeCount()) {
2661 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2662 mSettings.putSelectedSubtype(subtype.hashCode());
2663 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002664 } else {
2665 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002666 // If the subtype is not specified, choose the most applicable one
2667 mCurrentSubtype = getCurrentInputMethodSubtype();
satok723a27e2010-11-11 14:58:11 +09002668 }
satokab751aa2010-09-14 19:17:36 +09002669 }
satok723a27e2010-11-11 14:58:11 +09002670
2671 if (!setSubtypeOnly) {
2672 // Set InputMethod here
2673 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2674 }
2675 }
2676
2677 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2678 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2679 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2680 // newDefaultIme is empty when there is no candidate for the selected IME.
2681 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2682 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2683 if (subtypeHashCode != null) {
2684 try {
2685 lastSubtypeId = getSubtypeIdFromHashCode(
2686 imi, Integer.valueOf(subtypeHashCode));
2687 } catch (NumberFormatException e) {
2688 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2689 }
2690 }
2691 }
2692 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002693 }
2694
2695 private int getSelectedInputMethodSubtypeId(String id) {
2696 InputMethodInfo imi = mMethodMap.get(id);
2697 if (imi == null) {
2698 return NOT_A_SUBTYPE_ID;
2699 }
satokab751aa2010-09-14 19:17:36 +09002700 int subtypeId;
2701 try {
2702 subtypeId = Settings.Secure.getInt(mContext.getContentResolver(),
2703 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE);
2704 } catch (SettingNotFoundException e) {
2705 return NOT_A_SUBTYPE_ID;
2706 }
satok723a27e2010-11-11 14:58:11 +09002707 return getSubtypeIdFromHashCode(imi, subtypeId);
2708 }
2709
satokfdf419e2012-05-08 16:52:08 +09002710 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2711 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2712 }
2713
2714 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002715 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002716 final int subtypeCount = imi.getSubtypeCount();
2717 for (int i = 0; i < subtypeCount; ++i) {
2718 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002719 if (subtypeHashCode == ims.hashCode()) {
2720 return i;
2721 }
satokab751aa2010-09-14 19:17:36 +09002722 }
2723 }
2724 return NOT_A_SUBTYPE_ID;
2725 }
2726
satoka86f5e42011-09-02 17:12:42 +09002727 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2728 Resources res, InputMethodInfo imi) {
2729 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002730 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002731 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002732 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002733 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002734 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002735 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002736 // scan overriding implicitly enabled subtypes.
2737 InputMethodSubtype subtype = subtypes.get(i);
2738 if (subtype.overridesImplicitlyEnabledSubtype()) {
2739 final String mode = subtype.getMode();
2740 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2741 applicableModeAndSubtypesMap.put(mode, subtype);
2742 }
2743 }
2744 }
2745 if (applicableModeAndSubtypesMap.size() > 0) {
2746 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2747 }
2748 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002749 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002750 final String locale = subtype.getLocale();
2751 final String mode = subtype.getMode();
2752 // When system locale starts with subtype's locale, that subtype will be applicable
2753 // for system locale
2754 // For instance, it's clearly applicable for cases like system locale = en_US and
2755 // subtype = en, but it is not necessarily considered applicable for cases like system
2756 // locale = en and subtype = en_US.
2757 // We just call systemLocale.startsWith(locale) in this function because there is no
2758 // need to find applicable subtypes aggressively unlike
2759 // findLastResortApplicableSubtypeLocked.
2760 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09002761 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09002762 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09002763 if (applicableSubtype != null) {
2764 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
2765 if (!systemLocale.equals(locale)) continue;
2766 }
satok3da92232011-01-11 22:46:30 +09002767 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09002768 }
2769 }
2770 final InputMethodSubtype keyboardSubtype
2771 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
2772 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
2773 applicableModeAndSubtypesMap.values());
2774 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
2775 for (int i = 0; i < N; ++i) {
2776 final InputMethodSubtype subtype = subtypes.get(i);
2777 final String mode = subtype.getMode();
2778 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
2779 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
2780 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09002781 }
2782 }
2783 }
satokc3690562012-01-10 20:14:43 +09002784 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09002785 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002786 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09002787 if (lastResortKeyboardSubtype != null) {
2788 applicableSubtypes.add(lastResortKeyboardSubtype);
2789 }
2790 }
2791 return applicableSubtypes;
2792 }
2793
satok4e4569d2010-11-19 18:45:53 +09002794 /**
2795 * If there are no selected subtypes, tries finding the most applicable one according to the
2796 * given locale.
2797 * @param subtypes this function will search the most applicable subtype in subtypes
2798 * @param mode subtypes will be filtered by mode
2799 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09002800 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
2801 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09002802 * @return the most applicable subtypeId
2803 */
satokdf31ae62011-01-15 06:19:44 +09002804 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
2805 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09002806 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09002807 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09002808 return null;
satok8fbb1e82010-11-02 23:15:58 +09002809 }
satok4e4569d2010-11-19 18:45:53 +09002810 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09002811 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09002812 }
satok8fbb1e82010-11-02 23:15:58 +09002813 final String language = locale.substring(0, 2);
2814 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09002815 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09002816 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09002817 final int N = subtypes.size();
2818 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09002819 InputMethodSubtype subtype = subtypes.get(i);
2820 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09002821 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
2822 // and all subtypes with all modes can be candidates.
2823 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09002824 if (firstMatchedModeSubtype == null) {
2825 firstMatchedModeSubtype = subtype;
2826 }
satok9ef02832010-11-04 21:17:48 +09002827 if (locale.equals(subtypeLocale)) {
2828 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09002829 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002830 break;
2831 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
2832 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09002833 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09002834 partialMatchFound = true;
2835 }
satok8fbb1e82010-11-02 23:15:58 +09002836 }
2837 }
2838
satok7599a7f2010-12-22 13:45:23 +09002839 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
2840 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09002841 }
2842
satok8fbb1e82010-11-02 23:15:58 +09002843 // The first subtype applicable to the system locale will be defined as the most applicable
2844 // subtype.
2845 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09002846 if (applicableSubtype != null) {
2847 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
2848 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
2849 }
satok8fbb1e82010-11-02 23:15:58 +09002850 }
satokcd7cd292010-11-20 15:46:23 +09002851 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002852 }
2853
satok4e4569d2010-11-19 18:45:53 +09002854 // If there are no selected shortcuts, tries finding the most applicable ones.
2855 private Pair<InputMethodInfo, InputMethodSubtype>
2856 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2857 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2858 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002859 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002860 boolean foundInSystemIME = false;
2861
2862 // Search applicable subtype for each InputMethodInfo
2863 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002864 final String imiId = imi.getId();
2865 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2866 continue;
2867 }
satokcd7cd292010-11-20 15:46:23 +09002868 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002869 final List<InputMethodSubtype> enabledSubtypes =
2870 getEnabledInputMethodSubtypeList(imi, true);
2871 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002872 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09002873 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002874 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002875 }
satokdf31ae62011-01-15 06:19:44 +09002876 // 2. Search by the system locale from enabledSubtypes.
2877 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002878 if (subtype == null) {
2879 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002880 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002881 }
satoka86f5e42011-09-02 17:12:42 +09002882 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
2883 getOverridingImplicitlyEnabledSubtypes(imi, mode);
2884 final ArrayList<InputMethodSubtype> subtypesForSearch =
2885 overridingImplicitlyEnabledSubtypes.isEmpty()
2886 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002887 // 4. Search by the current subtype's locale from all subtypes.
2888 if (subtype == null && mCurrentSubtype != null) {
2889 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002890 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002891 }
2892 // 5. Search by the system locale from all subtypes.
2893 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002894 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09002895 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002896 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002897 }
satokcd7cd292010-11-20 15:46:23 +09002898 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09002899 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002900 // The current input method is the most applicable IME.
2901 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002902 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002903 break;
satok7599a7f2010-12-22 13:45:23 +09002904 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002905 // The system input method is 2nd applicable IME.
2906 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002907 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09002908 if ((imi.getServiceInfo().applicationInfo.flags
2909 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2910 foundInSystemIME = true;
2911 }
satok4e4569d2010-11-19 18:45:53 +09002912 }
2913 }
2914 }
2915 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09002916 if (mostApplicableIMI != null) {
2917 Slog.w(TAG, "Most applicable shortcut input method was:"
2918 + mostApplicableIMI.getId());
2919 if (mostApplicableSubtype != null) {
2920 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
2921 + "," + mostApplicableSubtype.getMode() + ","
2922 + mostApplicableSubtype.getLocale());
2923 }
2924 }
satok4e4569d2010-11-19 18:45:53 +09002925 }
satokcd7cd292010-11-20 15:46:23 +09002926 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09002927 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09002928 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09002929 } else {
2930 return null;
2931 }
2932 }
2933
satokab751aa2010-09-14 19:17:36 +09002934 /**
2935 * @return Return the current subtype of this input method.
2936 */
satok42c5a162011-05-26 16:46:14 +09002937 @Override
satokab751aa2010-09-14 19:17:36 +09002938 public InputMethodSubtype getCurrentInputMethodSubtype() {
satokfdf419e2012-05-08 16:52:08 +09002939 if (mCurMethodId == null) {
2940 return null;
2941 }
satok4e4569d2010-11-19 18:45:53 +09002942 boolean subtypeIsSelected = false;
2943 try {
2944 subtypeIsSelected = Settings.Secure.getInt(mContext.getContentResolver(),
2945 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE) != NOT_A_SUBTYPE_ID;
2946 } catch (SettingNotFoundException e) {
2947 }
satok3ef8b292010-11-23 06:06:29 +09002948 synchronized (mMethodMap) {
satokfdf419e2012-05-08 16:52:08 +09002949 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2950 if (imi == null || imi.getSubtypeCount() == 0) {
2951 return null;
2952 }
2953 if (!subtypeIsSelected || mCurrentSubtype == null
2954 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
2955 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
satok3ef8b292010-11-23 06:06:29 +09002956 if (subtypeId == NOT_A_SUBTYPE_ID) {
satokfdf419e2012-05-08 16:52:08 +09002957 // If there are no selected subtypes, the framework will try to find
2958 // the most applicable subtype from explicitly or implicitly enabled
2959 // subtypes.
2960 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
2961 getEnabledInputMethodSubtypeList(imi, true);
2962 // If there is only one explicitly or implicitly enabled subtype,
2963 // just returns it.
2964 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
2965 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
2966 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
2967 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
2968 mRes, explicitlyOrImplicitlyEnabledSubtypes,
2969 SUBTYPE_MODE_KEYBOARD, null, true);
2970 if (mCurrentSubtype == null) {
satokd8713432011-01-18 00:55:13 +09002971 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
satokfdf419e2012-05-08 16:52:08 +09002972 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
2973 true);
satokd8713432011-01-18 00:55:13 +09002974 }
satok4e4569d2010-11-19 18:45:53 +09002975 }
satokcd7cd292010-11-20 15:46:23 +09002976 } else {
satokfdf419e2012-05-08 16:52:08 +09002977 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok3ef8b292010-11-23 06:06:29 +09002978 }
satok8fbb1e82010-11-02 23:15:58 +09002979 }
satok3ef8b292010-11-23 06:06:29 +09002980 return mCurrentSubtype;
satok8fbb1e82010-11-02 23:15:58 +09002981 }
satokab751aa2010-09-14 19:17:36 +09002982 }
2983
satokf3db1af2010-11-23 13:34:33 +09002984 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
2985 InputMethodSubtype subtype) {
2986 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
2987 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
2988 } else {
2989 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2990 subtypes.add(subtype);
2991 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
2992 }
2993 }
2994
satok4e4569d2010-11-19 18:45:53 +09002995 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09002996 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09002997 @Override
satok4e4569d2010-11-19 18:45:53 +09002998 public List getShortcutInputMethodsAndSubtypes() {
2999 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003000 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003001 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003002 // If there are no selected shortcut subtypes, the framework will try to find
3003 // the most applicable subtype from all subtypes whose mode is
3004 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003005 Pair<InputMethodInfo, InputMethodSubtype> info =
3006 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3007 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003008 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003009 ret.add(info.first);
3010 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003011 }
satok3da92232011-01-11 22:46:30 +09003012 return ret;
satokf3db1af2010-11-23 13:34:33 +09003013 }
satokf3db1af2010-11-23 13:34:33 +09003014 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3015 ret.add(imi);
3016 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3017 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003018 }
3019 }
satokf3db1af2010-11-23 13:34:33 +09003020 return ret;
satok4e4569d2010-11-19 18:45:53 +09003021 }
3022 }
3023
satok42c5a162011-05-26 16:46:14 +09003024 @Override
satokb66d2872010-11-10 01:04:04 +09003025 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
3026 synchronized (mMethodMap) {
3027 if (subtype != null && mCurMethodId != null) {
3028 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3029 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3030 if (subtypeId != NOT_A_SUBTYPE_ID) {
3031 setInputMethodLocked(mCurMethodId, subtypeId);
3032 return true;
3033 }
3034 }
3035 return false;
3036 }
3037 }
3038
satok688bd472012-02-09 20:09:17 +09003039 private static class InputMethodAndSubtypeListManager {
3040 private final Context mContext;
3041 private final PackageManager mPm;
3042 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003043 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003044 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3045 mContext = context;
3046 mPm = context.getPackageManager();
3047 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003048 final Locale locale = context.getResources().getConfiguration().locale;
3049 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003050 }
3051
3052 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3053 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3054 new Comparator<InputMethodInfo>() {
3055 @Override
3056 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3057 if (imi2 == null) return 0;
3058 if (imi1 == null) return 1;
3059 if (mPm == null) {
3060 return imi1.getId().compareTo(imi2.getId());
3061 }
3062 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3063 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3064 return imiId1.toString().compareTo(imiId2.toString());
3065 }
3066 });
3067
3068 public ImeSubtypeListItem getNextInputMethod(
3069 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3070 if (imi == null) {
3071 return null;
3072 }
3073 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3074 if (imList.size() <= 1) {
3075 return null;
3076 }
3077 final int N = imList.size();
3078 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003079 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003080 : NOT_A_SUBTYPE_ID;
3081 for (int i = 0; i < N; ++i) {
3082 final ImeSubtypeListItem isli = imList.get(i);
3083 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3084 if (!onlyCurrentIme) {
3085 return imList.get((i + 1) % N);
3086 }
3087 for (int j = 0; j < N - 1; ++j) {
3088 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3089 if (candidate.mImi.equals(imi)) {
3090 return candidate;
3091 }
3092 }
3093 return null;
3094 }
3095 }
3096 return null;
3097 }
3098
3099 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3100 return getSortedInputMethodAndSubtypeList(true, false, false);
3101 }
3102
3103 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3104 boolean inputShown, boolean isScreenLocked) {
3105 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3106 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3107 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3108 if (immis == null || immis.size() == 0) {
3109 return Collections.emptyList();
3110 }
3111 mSortedImmis.clear();
3112 mSortedImmis.putAll(immis);
3113 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3114 if (imi == null) continue;
3115 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3116 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3117 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3118 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3119 }
3120 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3121 final CharSequence imeLabel = imi.loadLabel(mPm);
3122 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3123 final int subtypeCount = imi.getSubtypeCount();
3124 if (DEBUG) {
3125 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3126 }
3127 for (int j = 0; j < subtypeCount; ++j) {
3128 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3129 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3130 // We show all enabled IMEs and subtypes when an IME is shown.
3131 if (enabledSubtypeSet.contains(subtypeHashCode)
3132 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3133 final CharSequence subtypeLabel =
3134 subtype.overridesImplicitlyEnabledSubtype() ? null
3135 : subtype.getDisplayName(mContext, imi.getPackageName(),
3136 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003137 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3138 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003139
3140 // Removing this subtype from enabledSubtypeSet because we no longer
3141 // need to add an entry of this subtype to imList to avoid duplicated
3142 // entries.
3143 enabledSubtypeSet.remove(subtypeHashCode);
3144 }
3145 }
3146 } else {
satok93d744d2012-05-09 17:14:08 +09003147 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3148 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003149 }
3150 }
satok93d744d2012-05-09 17:14:08 +09003151 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003152 return imList;
3153 }
3154 }
3155
satokd87c2592010-09-29 11:52:06 +09003156 /**
3157 * Utility class for putting and getting settings for InputMethod
3158 * TODO: Move all putters and getters of settings to this class.
3159 */
3160 private static class InputMethodSettings {
3161 // The string for enabled input method is saved as follows:
3162 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3163 private static final char INPUT_METHOD_SEPARATER = ':';
3164 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003165 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003166 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3167
satok723a27e2010-11-11 14:58:11 +09003168 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003169 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3170
satokdf31ae62011-01-15 06:19:44 +09003171 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003172 private final ContentResolver mResolver;
3173 private final HashMap<String, InputMethodInfo> mMethodMap;
3174 private final ArrayList<InputMethodInfo> mMethodList;
3175
3176 private String mEnabledInputMethodsStrCache;
3177
3178 private static void buildEnabledInputMethodsSettingString(
3179 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3180 String id = pair.first;
3181 ArrayList<String> subtypes = pair.second;
3182 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003183 // Inputmethod and subtypes are saved in the settings as follows:
3184 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3185 for (String subtypeId: subtypes) {
3186 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003187 }
3188 }
3189
3190 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003191 Resources res, ContentResolver resolver,
3192 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
3193 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003194 mResolver = resolver;
3195 mMethodMap = methodMap;
3196 mMethodList = methodList;
3197 }
3198
3199 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3200 return createEnabledInputMethodListLocked(
3201 getEnabledInputMethodsAndSubtypeListLocked());
3202 }
3203
satok7f35c8c2010-10-07 21:13:11 +09003204 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003205 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3206 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003207 getEnabledInputMethodsAndSubtypeListLocked());
3208 }
3209
satok67ddf9c2010-11-17 09:45:54 +09003210 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3211 InputMethodInfo imi) {
3212 List<Pair<String, ArrayList<String>>> imsList =
3213 getEnabledInputMethodsAndSubtypeListLocked();
3214 ArrayList<InputMethodSubtype> enabledSubtypes =
3215 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003216 if (imi != null) {
3217 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3218 InputMethodInfo info = mMethodMap.get(imsPair.first);
3219 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003220 final int subtypeCount = info.getSubtypeCount();
3221 for (int i = 0; i < subtypeCount; ++i) {
3222 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003223 for (String s: imsPair.second) {
3224 if (String.valueOf(ims.hashCode()).equals(s)) {
3225 enabledSubtypes.add(ims);
3226 }
satok67ddf9c2010-11-17 09:45:54 +09003227 }
3228 }
satok884ef9a2010-11-18 10:39:46 +09003229 break;
satok67ddf9c2010-11-17 09:45:54 +09003230 }
satok67ddf9c2010-11-17 09:45:54 +09003231 }
3232 }
3233 return enabledSubtypes;
3234 }
3235
satokd87c2592010-09-29 11:52:06 +09003236 // At the initial boot, the settings for input methods are not set,
3237 // so we need to enable IME in that case.
3238 public void enableAllIMEsIfThereIsNoEnabledIME() {
3239 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3240 StringBuilder sb = new StringBuilder();
3241 final int N = mMethodList.size();
3242 for (int i = 0; i < N; i++) {
3243 InputMethodInfo imi = mMethodList.get(i);
3244 Slog.i(TAG, "Adding: " + imi.getId());
3245 if (i > 0) sb.append(':');
3246 sb.append(imi.getId());
3247 }
3248 putEnabledInputMethodsStr(sb.toString());
3249 }
3250 }
3251
satokbb4aa062011-01-19 21:40:27 +09003252 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003253 ArrayList<Pair<String, ArrayList<String>>> imsList
3254 = new ArrayList<Pair<String, ArrayList<String>>>();
3255 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3256 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3257 return imsList;
3258 }
satok723a27e2010-11-11 14:58:11 +09003259 mInputMethodSplitter.setString(enabledInputMethodsStr);
3260 while (mInputMethodSplitter.hasNext()) {
3261 String nextImsStr = mInputMethodSplitter.next();
3262 mSubtypeSplitter.setString(nextImsStr);
3263 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003264 ArrayList<String> subtypeHashes = new ArrayList<String>();
3265 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003266 String imeId = mSubtypeSplitter.next();
3267 while (mSubtypeSplitter.hasNext()) {
3268 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003269 }
3270 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3271 }
3272 }
3273 return imsList;
3274 }
3275
3276 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3277 if (reloadInputMethodStr) {
3278 getEnabledInputMethodsStr();
3279 }
3280 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3281 // Add in the newly enabled input method.
3282 putEnabledInputMethodsStr(id);
3283 } else {
3284 putEnabledInputMethodsStr(
3285 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3286 }
3287 }
3288
3289 /**
3290 * Build and put a string of EnabledInputMethods with removing specified Id.
3291 * @return the specified id was removed or not.
3292 */
3293 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3294 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3295 boolean isRemoved = false;
3296 boolean needsAppendSeparator = false;
3297 for (Pair<String, ArrayList<String>> ims: imsList) {
3298 String curId = ims.first;
3299 if (curId.equals(id)) {
3300 // We are disabling this input method, and it is
3301 // currently enabled. Skip it to remove from the
3302 // new list.
3303 isRemoved = true;
3304 } else {
3305 if (needsAppendSeparator) {
3306 builder.append(INPUT_METHOD_SEPARATER);
3307 } else {
3308 needsAppendSeparator = true;
3309 }
3310 buildEnabledInputMethodsSettingString(builder, ims);
3311 }
3312 }
3313 if (isRemoved) {
3314 // Update the setting with the new list of input methods.
3315 putEnabledInputMethodsStr(builder.toString());
3316 }
3317 return isRemoved;
3318 }
3319
3320 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3321 List<Pair<String, ArrayList<String>>> imsList) {
3322 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3323 for (Pair<String, ArrayList<String>> ims: imsList) {
3324 InputMethodInfo info = mMethodMap.get(ims.first);
3325 if (info != null) {
3326 res.add(info);
3327 }
3328 }
3329 return res;
3330 }
3331
satok7f35c8c2010-10-07 21:13:11 +09003332 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003333 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003334 List<Pair<String, ArrayList<String>>> imsList) {
3335 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3336 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3337 for (Pair<String, ArrayList<String>> ims : imsList) {
3338 InputMethodInfo info = mMethodMap.get(ims.first);
3339 if (info != null) {
3340 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3341 }
3342 }
3343 return res;
3344 }
3345
satokd87c2592010-09-29 11:52:06 +09003346 private void putEnabledInputMethodsStr(String str) {
3347 Settings.Secure.putString(mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str);
3348 mEnabledInputMethodsStrCache = str;
3349 }
3350
3351 private String getEnabledInputMethodsStr() {
3352 mEnabledInputMethodsStrCache = Settings.Secure.getString(
3353 mResolver, Settings.Secure.ENABLED_INPUT_METHODS);
satok723a27e2010-11-11 14:58:11 +09003354 if (DEBUG) {
3355 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache);
3356 }
satokd87c2592010-09-29 11:52:06 +09003357 return mEnabledInputMethodsStrCache;
3358 }
satok723a27e2010-11-11 14:58:11 +09003359
3360 private void saveSubtypeHistory(
3361 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3362 StringBuilder builder = new StringBuilder();
3363 boolean isImeAdded = false;
3364 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3365 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3366 newSubtypeId);
3367 isImeAdded = true;
3368 }
3369 for (Pair<String, String> ime: savedImes) {
3370 String imeId = ime.first;
3371 String subtypeId = ime.second;
3372 if (TextUtils.isEmpty(subtypeId)) {
3373 subtypeId = NOT_A_SUBTYPE_ID_STR;
3374 }
3375 if (isImeAdded) {
3376 builder.append(INPUT_METHOD_SEPARATER);
3377 } else {
3378 isImeAdded = true;
3379 }
3380 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3381 subtypeId);
3382 }
3383 // Remove the last INPUT_METHOD_SEPARATER
3384 putSubtypeHistoryStr(builder.toString());
3385 }
3386
3387 public void addSubtypeToHistory(String imeId, String subtypeId) {
3388 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3389 for (Pair<String, String> ime: subtypeHistory) {
3390 if (ime.first.equals(imeId)) {
3391 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003392 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003393 + ime.second);
3394 }
3395 // We should break here
3396 subtypeHistory.remove(ime);
3397 break;
3398 }
3399 }
satokbb4aa062011-01-19 21:40:27 +09003400 if (DEBUG) {
3401 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3402 }
satok723a27e2010-11-11 14:58:11 +09003403 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3404 }
3405
3406 private void putSubtypeHistoryStr(String str) {
3407 if (DEBUG) {
3408 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3409 }
3410 Settings.Secure.putString(
3411 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str);
3412 }
3413
3414 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3415 // Gets the first one from the history
3416 return getLastSubtypeForInputMethodLockedInternal(null);
3417 }
3418
3419 public String getLastSubtypeForInputMethodLocked(String imeId) {
3420 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3421 if (ime != null) {
3422 return ime.second;
3423 } else {
3424 return null;
3425 }
3426 }
3427
3428 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3429 List<Pair<String, ArrayList<String>>> enabledImes =
3430 getEnabledInputMethodsAndSubtypeListLocked();
3431 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003432 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003433 final String imeInTheHistory = imeAndSubtype.first;
3434 // If imeId is empty, returns the first IME and subtype in the history
3435 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3436 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003437 final String subtypeHashCode =
3438 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3439 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003440 if (!TextUtils.isEmpty(subtypeHashCode)) {
3441 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003442 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003443 }
3444 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3445 }
3446 }
3447 }
3448 if (DEBUG) {
3449 Slog.d(TAG, "No enabled IME found in the history");
3450 }
3451 return null;
3452 }
3453
satokdf31ae62011-01-15 06:19:44 +09003454 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003455 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3456 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3457 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003458 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003459 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003460 if (explicitlyEnabledSubtypes.size() == 0) {
3461 // If there are no explicitly enabled subtypes, applicable subtypes are
3462 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003463 // If IME is enabled and no subtypes are enabled, applicable subtypes
3464 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003465 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003466 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003467 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003468 if (implicitlySelectedSubtypes != null) {
3469 final int N = implicitlySelectedSubtypes.size();
3470 for (int i = 0; i < N; ++i) {
3471 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3472 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3473 return subtypeHashCode;
3474 }
3475 }
3476 }
3477 }
3478 } else {
satokf6cafb62011-01-17 16:29:02 +09003479 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003480 if (s.equals(subtypeHashCode)) {
3481 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003482 try {
3483 final int hashCode = Integer.valueOf(subtypeHashCode);
3484 // Check whether the subtype id is valid or not
3485 if (isValidSubtypeId(imi, hashCode)) {
3486 return s;
3487 } else {
3488 return NOT_A_SUBTYPE_ID_STR;
3489 }
3490 } catch (NumberFormatException e) {
3491 return NOT_A_SUBTYPE_ID_STR;
3492 }
satokdf31ae62011-01-15 06:19:44 +09003493 }
satok723a27e2010-11-11 14:58:11 +09003494 }
3495 }
3496 // If imeId was enabled but subtypeId was disabled.
3497 return NOT_A_SUBTYPE_ID_STR;
3498 }
3499 }
3500 // If both imeId and subtypeId are disabled, return null
3501 return null;
3502 }
3503
3504 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3505 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3506 final String subtypeHistoryStr = getSubtypeHistoryStr();
3507 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3508 return imsList;
3509 }
3510 mInputMethodSplitter.setString(subtypeHistoryStr);
3511 while (mInputMethodSplitter.hasNext()) {
3512 String nextImsStr = mInputMethodSplitter.next();
3513 mSubtypeSplitter.setString(nextImsStr);
3514 if (mSubtypeSplitter.hasNext()) {
3515 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3516 // The first element is ime id.
3517 String imeId = mSubtypeSplitter.next();
3518 while (mSubtypeSplitter.hasNext()) {
3519 subtypeId = mSubtypeSplitter.next();
3520 break;
3521 }
3522 imsList.add(new Pair<String, String>(imeId, subtypeId));
3523 }
3524 }
3525 return imsList;
3526 }
3527
3528 private String getSubtypeHistoryStr() {
3529 if (DEBUG) {
3530 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getString(
3531 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY));
3532 }
3533 return Settings.Secure.getString(
3534 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY);
3535 }
3536
3537 public void putSelectedInputMethod(String imeId) {
3538 Settings.Secure.putString(mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId);
3539 }
3540
3541 public void putSelectedSubtype(int subtypeId) {
3542 Settings.Secure.putInt(
3543 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtypeId);
3544 }
satokd87c2592010-09-29 11:52:06 +09003545 }
3546
satoke7c6998e2011-06-03 17:57:59 +09003547 private static class InputMethodFileManager {
3548 private static final String SYSTEM_PATH = "system";
3549 private static final String INPUT_METHOD_PATH = "inputmethod";
3550 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3551 private static final String NODE_SUBTYPES = "subtypes";
3552 private static final String NODE_SUBTYPE = "subtype";
3553 private static final String NODE_IMI = "imi";
3554 private static final String ATTR_ID = "id";
3555 private static final String ATTR_LABEL = "label";
3556 private static final String ATTR_ICON = "icon";
3557 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3558 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3559 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3560 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3561 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3562 private final HashMap<String, InputMethodInfo> mMethodMap;
3563 private final HashMap<String, List<InputMethodSubtype>> mSubtypesMap =
3564 new HashMap<String, List<InputMethodSubtype>>();
3565 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap) {
3566 if (methodMap == null) {
3567 throw new NullPointerException("methodMap is null");
3568 }
3569 mMethodMap = methodMap;
3570 final File systemDir = new File(Environment.getDataDirectory(), SYSTEM_PATH);
3571 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3572 if (!inputMethodDir.mkdirs()) {
3573 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3574 }
3575 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3576 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3577 if (!subtypeFile.exists()) {
3578 // If "subtypes.xml" doesn't exist, create a blank file.
3579 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3580 methodMap);
3581 } else {
3582 readAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile);
3583 }
3584 }
3585
3586 private void deleteAllInputMethodSubtypes(String imiId) {
3587 synchronized (mMethodMap) {
3588 mSubtypesMap.remove(imiId);
3589 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3590 mMethodMap);
3591 }
3592 }
3593
3594 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003595 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003596 synchronized (mMethodMap) {
3597 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3598 final int N = additionalSubtypes.length;
3599 for (int i = 0; i < N; ++i) {
3600 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003601 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003602 subtypes.add(subtype);
3603 }
3604 }
satok4a28bde2011-06-29 21:03:40 +09003605 mSubtypesMap.put(imi.getId(), subtypes);
satoke7c6998e2011-06-03 17:57:59 +09003606 writeAdditionalInputMethodSubtypes(mSubtypesMap, mAdditionalInputMethodSubtypeFile,
3607 mMethodMap);
3608 }
3609 }
3610
3611 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3612 synchronized (mMethodMap) {
3613 return mSubtypesMap;
3614 }
3615 }
3616
3617 private static void writeAdditionalInputMethodSubtypes(
3618 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3619 HashMap<String, InputMethodInfo> methodMap) {
3620 // Safety net for the case that this function is called before methodMap is set.
3621 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3622 FileOutputStream fos = null;
3623 try {
3624 fos = subtypesFile.startWrite();
3625 final XmlSerializer out = new FastXmlSerializer();
3626 out.setOutput(fos, "utf-8");
3627 out.startDocument(null, true);
3628 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3629 out.startTag(null, NODE_SUBTYPES);
3630 for (String imiId : allSubtypes.keySet()) {
3631 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3632 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3633 continue;
3634 }
3635 out.startTag(null, NODE_IMI);
3636 out.attribute(null, ATTR_ID, imiId);
3637 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3638 final int N = subtypesList.size();
3639 for (int i = 0; i < N; ++i) {
3640 final InputMethodSubtype subtype = subtypesList.get(i);
3641 out.startTag(null, NODE_SUBTYPE);
3642 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3643 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3644 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3645 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3646 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3647 out.attribute(null, ATTR_IS_AUXILIARY,
3648 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3649 out.endTag(null, NODE_SUBTYPE);
3650 }
3651 out.endTag(null, NODE_IMI);
3652 }
3653 out.endTag(null, NODE_SUBTYPES);
3654 out.endDocument();
3655 subtypesFile.finishWrite(fos);
3656 } catch (java.io.IOException e) {
3657 Slog.w(TAG, "Error writing subtypes", e);
3658 if (fos != null) {
3659 subtypesFile.failWrite(fos);
3660 }
3661 }
3662 }
3663
3664 private static void readAdditionalInputMethodSubtypes(
3665 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3666 if (allSubtypes == null || subtypesFile == null) return;
3667 allSubtypes.clear();
3668 FileInputStream fis = null;
3669 try {
3670 fis = subtypesFile.openRead();
3671 final XmlPullParser parser = Xml.newPullParser();
3672 parser.setInput(fis, null);
3673 int type = parser.getEventType();
3674 // Skip parsing until START_TAG
3675 while ((type = parser.next()) != XmlPullParser.START_TAG
3676 && type != XmlPullParser.END_DOCUMENT) {}
3677 String firstNodeName = parser.getName();
3678 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3679 throw new XmlPullParserException("Xml doesn't start with subtypes");
3680 }
3681 final int depth =parser.getDepth();
3682 String currentImiId = null;
3683 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3684 while (((type = parser.next()) != XmlPullParser.END_TAG
3685 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3686 if (type != XmlPullParser.START_TAG)
3687 continue;
3688 final String nodeName = parser.getName();
3689 if (NODE_IMI.equals(nodeName)) {
3690 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3691 if (TextUtils.isEmpty(currentImiId)) {
3692 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3693 continue;
3694 }
3695 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3696 allSubtypes.put(currentImiId, tempSubtypesArray);
3697 } else if (NODE_SUBTYPE.equals(nodeName)) {
3698 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3699 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3700 continue;
3701 }
3702 final int icon = Integer.valueOf(
3703 parser.getAttributeValue(null, ATTR_ICON));
3704 final int label = Integer.valueOf(
3705 parser.getAttributeValue(null, ATTR_LABEL));
3706 final String imeSubtypeLocale =
3707 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3708 final String imeSubtypeMode =
3709 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3710 final String imeSubtypeExtraValue =
3711 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003712 final boolean isAuxiliary = "1".equals(String.valueOf(
3713 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003714 final InputMethodSubtype subtype =
3715 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3716 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3717 tempSubtypesArray.add(subtype);
3718 }
3719 }
3720 } catch (XmlPullParserException e) {
3721 Slog.w(TAG, "Error reading subtypes: " + e);
3722 return;
3723 } catch (java.io.IOException e) {
3724 Slog.w(TAG, "Error reading subtypes: " + e);
3725 return;
3726 } catch (NumberFormatException e) {
3727 Slog.w(TAG, "Error reading subtypes: " + e);
3728 return;
3729 } finally {
3730 if (fis != null) {
3731 try {
3732 fis.close();
3733 } catch (java.io.IOException e1) {
3734 Slog.w(TAG, "Failed to close.");
3735 }
3736 }
3737 }
3738 }
3739 }
3740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 @Override
3744 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3745 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3746 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3749 + Binder.getCallingPid()
3750 + ", uid=" + Binder.getCallingUid());
3751 return;
3752 }
3753
3754 IInputMethod method;
3755 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 synchronized (mMethodMap) {
3760 p.println("Current Input Method Manager state:");
3761 int N = mMethodList.size();
3762 p.println(" Input Methods:");
3763 for (int i=0; i<N; i++) {
3764 InputMethodInfo info = mMethodList.get(i);
3765 p.println(" InputMethod #" + i + ":");
3766 info.dump(p, " ");
3767 }
3768 p.println(" Clients:");
3769 for (ClientState ci : mClients.values()) {
3770 p.println(" Client " + ci + ":");
3771 p.println(" client=" + ci.client);
3772 p.println(" inputContext=" + ci.inputContext);
3773 p.println(" sessionRequested=" + ci.sessionRequested);
3774 p.println(" curSession=" + ci.curSession);
3775 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003776 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003778 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3779 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3781 + " mBoundToMethod=" + mBoundToMethod);
3782 p.println(" mCurToken=" + mCurToken);
3783 p.println(" mCurIntent=" + mCurIntent);
3784 method = mCurMethod;
3785 p.println(" mCurMethod=" + mCurMethod);
3786 p.println(" mEnabledSession=" + mEnabledSession);
3787 p.println(" mShowRequested=" + mShowRequested
3788 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3789 + " mShowForced=" + mShowForced
3790 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003791 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003793
Jeff Brownb88102f2010-09-08 11:49:43 -07003794 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 pw.flush();
3797 try {
3798 client.client.asBinder().dump(fd, args);
3799 } catch (RemoteException e) {
3800 p.println("Input method client dead: " + e);
3801 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003802 } else {
3803 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003805
Jeff Brownb88102f2010-09-08 11:49:43 -07003806 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 pw.flush();
3809 try {
3810 method.asBinder().dump(fd, args);
3811 } catch (RemoteException e) {
3812 p.println("Input method service dead: " + e);
3813 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003814 } else {
3815 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
3817 }
3818}