blob: 8eb61dd1ad0c83b7eaf0e2e8f93722ba0c1ad9d8 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070020import com.android.internal.os.SomeArgs;
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;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090037import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090040import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090041import android.app.Notification;
42import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070043import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090044import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.ComponentName;
46import android.content.ContentResolver;
47import android.content.Context;
48import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.DialogInterface.OnCancelListener;
50import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090051import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070053import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090054import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.ResolveInfo;
58import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070059import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.res.Resources;
61import android.content.res.TypedArray;
62import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080063import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090065import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
68import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090069import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Parcel;
73import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080074import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.ServiceManager;
76import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090080import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090082import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070083import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090085import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090086import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.PrintWriterPrinter;
88import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090089import android.util.Slog;
90import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090092import android.view.LayoutInflater;
93import android.view.View;
94import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090096import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.inputmethod.InputBinding;
98import android.view.inputmethod.InputMethod;
99import android.view.inputmethod.InputMethodInfo;
100import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900101import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900102import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900103import android.widget.CompoundButton;
104import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900106import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
satoke7c6998e2011-06-03 17:57:59 +0900109import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900111import java.io.FileInputStream;
112import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.io.IOException;
114import java.io.PrintWriter;
115import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900116import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900117import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900119import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900121import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900122import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124/**
125 * This class provides a system service that manages input methods.
126 */
127public class InputMethodManagerService extends IInputMethodManager.Stub
128 implements ServiceConnection, Handler.Callback {
129 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700130 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900133 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900134 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900135 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_UNBIND_INPUT = 1000;
138 static final int MSG_BIND_INPUT = 1010;
139 static final int MSG_SHOW_SOFT_INPUT = 1020;
140 static final int MSG_HIDE_SOFT_INPUT = 1030;
141 static final int MSG_ATTACH_TOKEN = 1040;
142 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final int MSG_START_INPUT = 2000;
145 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final int MSG_UNBIND_METHOD = 3000;
148 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700149 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
satok01038492012-04-09 21:08:27 +0900151 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
satokf9f01002011-05-19 21:31:50 +0900155 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
156
satokab751aa2010-09-14 19:17:36 +0900157 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900158 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900159 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
160 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900161 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900162 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
163 "EnabledWhenDefaultIsNotAsciiCapable";
164 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900165 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800168 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900170 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final IWindowManager mIWindowManager;
173 final HandlerCaller mCaller;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900174 private InputMethodFileManager mFileManager;
175 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900176 private final HardKeyboardListener mHardKeyboardListener;
177 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // All known input methods. mMethodMap also serves as the global
182 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900183 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
184 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900185 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
186 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700188 // Used to bring IME service up to visible adjustment while it is being shown.
189 final ServiceConnection mVisibleConnection = new ServiceConnection() {
190 @Override public void onServiceConnected(ComponentName name, IBinder service) {
191 }
192
193 @Override public void onServiceDisconnected(ComponentName name) {
194 }
195 };
196 boolean mVisibleBound = false;
197
satok7cfc0ed2011-06-20 21:29:36 +0900198 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700199 private NotificationManager mNotificationManager;
200 private KeyguardManager mKeyguardManager;
201 private StatusBarManagerService mStatusBar;
202 private Notification mImeSwitcherNotification;
203 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900204 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900205 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900206 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 class SessionState {
209 final ClientState client;
210 final IInputMethod method;
211 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 @Override
214 public String toString() {
215 return "SessionState{uid " + client.uid + " pid " + client.pid
216 + " method " + Integer.toHexString(
217 System.identityHashCode(method))
218 + " session " + Integer.toHexString(
219 System.identityHashCode(session))
220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
224 IInputMethodSession _session) {
225 client = _client;
226 method = _method;
227 session = _session;
228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 class ClientState {
232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * Set to true if our ServiceConnection is currently actively bound to
315 * a service (whether or not we have gotten its IBinder back yet).
316 */
317 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * Set if the client has asked for the input method to be shown.
321 */
322 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set if we were explicitly told to show the input method.
326 */
327 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Set if we were forced to be shown.
331 */
332 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Set if we last told the input method to show itself.
336 */
337 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The Intent used to connect to the current input method.
341 */
342 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The token we have made for the currently active input method, to
346 * identify it in the future.
347 */
348 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * If non-null, this is the input method service we are currently connected
352 * to.
353 */
354 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Time that we last initiated a bind to the input method, to determine
358 * if we should try to disconnect and reconnect to it.
359 */
360 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * Have we called mCurMethod.bindInput()?
364 */
365 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Currently enabled session. Only touched by service thread, not
369 * protected by a lock.
370 */
371 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * True if the screen is on. The value is true initially.
375 */
376 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
Joe Onorato857fd9b2011-01-27 15:08:35 -0800378 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
379 int mImeWindowVis;
380
Ken Wakasa05dbb652011-08-22 15:22:43 +0900381 private AlertDialog.Builder mDialogBuilder;
382 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900383 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private InputMethodInfo[] mIms;
385 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900386 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900387 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
388 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500389 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 class SettingsObserver extends ContentObserver {
392 SettingsObserver(Handler handler) {
393 super(handler);
394 ContentResolver resolver = mContext.getContentResolver();
395 resolver.registerContentObserver(Settings.Secure.getUriFor(
396 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900397 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900398 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900400 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 @Override public void onChange(boolean selfChange) {
404 synchronized (mMethodMap) {
405 updateFromSettingsLocked();
406 }
407 }
408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
411 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700413 if (mCurClient != null && mCurClient.client != null) {
414 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
415 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900418
419 @Override
420 public void onReceive(Context context, Intent intent) {
421 final String action = intent.getAction();
422 if (Intent.ACTION_SCREEN_ON.equals(action)) {
423 mScreenOn = true;
424 refreshImeWindowVisibilityLocked();
425 updateActive();
426 return;
427 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
428 mScreenOn = false;
429 setImeWindowVisibilityStatusHiddenLocked();
430 updateActive();
431 return;
432 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
433 hideInputMethodMenu();
434 // No need to updateActive
435 return;
436 } else {
437 Slog.w(TAG, "Unexpected intent " + intent);
438 }
439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443 private boolean isChangingPackagesOfCurrentUser() {
444 final int userId = getChangingUserId();
445 final boolean retval = userId == mSettings.getCurrentUserId();
446 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900447 if (!retval) {
448 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
449 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900450 }
451 return retval;
452 }
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800455 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456 if (!isChangingPackagesOfCurrentUser()) {
457 return false;
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 final int N = mMethodList.size();
462 if (curInputMethodId != null) {
463 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 InputMethodInfo imi = mMethodList.get(i);
465 if (imi.getId().equals(curInputMethodId)) {
466 for (String pkg : packages) {
467 if (imi.getPackageName().equals(pkg)) {
468 if (!doit) {
469 return true;
470 }
satok723a27e2010-11-11 14:58:11 +0900471 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 chooseNewDefaultIMELocked();
473 return true;
474 }
475 }
476 }
477 }
478 }
479 }
480 return false;
481 }
482
483 @Override
484 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 if (!isChangingPackagesOfCurrentUser()) {
486 return;
487 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 synchronized (mMethodMap) {
489 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800491 final int N = mMethodList.size();
492 if (curInputMethodId != null) {
493 for (int i=0; i<N; i++) {
494 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900495 final String imiId = imi.getId();
496 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 curIm = imi;
498 }
satoke7c6998e2011-06-03 17:57:59 +0900499
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800500 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900501 if (isPackageModified(imi.getPackageName())) {
502 mFileManager.deleteAllInputMethodSubtypes(imiId);
503 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800504 if (change == PACKAGE_TEMPORARY_CHANGE
505 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800506 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800507 + imi.getComponent());
508 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 }
511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800512
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800513 buildInputMethodListLocked(mMethodList, mMethodMap);
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800517 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 int change = isPackageDisappearing(curIm.getPackageName());
519 if (change == PACKAGE_TEMPORARY_CHANGE
520 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800521 ServiceInfo si = null;
522 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900523 si = mIPackageManager.getServiceInfo(
524 curIm.getComponent(), 0, mSettings.getCurrentUserId());
525 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800526 }
527 if (si == null) {
528 // Uh oh, current input method is no longer around!
529 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900531 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800532 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 changed = true;
534 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900536 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 }
538 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 }
satokab751aa2010-09-14 19:17:36 +0900541
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 if (curIm == null) {
543 // We currently don't have a default input method... is
544 // one now available?
545 changed = chooseNewDefaultIMELocked();
546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 if (changed) {
549 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551 }
552 }
553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
Jean Chalarde0d32a62011-10-20 20:36:07 +0900555 private static class MethodCallback extends IInputMethodCallback.Stub {
556 private final IInputMethod mMethod;
557 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558
Jean Chalarde0d32a62011-10-20 20:36:07 +0900559 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900561 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
satoke7c6998e2011-06-03 17:57:59 +0900564 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public void finishedEvent(int seq, boolean handled) throws RemoteException {
566 }
567
satoke7c6998e2011-06-03 17:57:59 +0900568 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900570 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
satok01038492012-04-09 21:08:27 +0900574 private class HardKeyboardListener
575 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
576 @Override
577 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
578 mHandler.sendMessage(mHandler.obtainMessage(
579 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
580 }
581
582 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
583 if (DEBUG) {
584 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
585 + enabled);
586 }
587 synchronized(mMethodMap) {
588 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
589 && mSwitchingDialog.isShowing()) {
590 mSwitchingDialogTitleView.findViewById(
591 com.android.internal.R.id.hard_keyboard_section).setVisibility(
592 available ? View.VISIBLE : View.GONE);
593 }
594 }
595 }
596 }
597
598 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900599 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800601 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mHandler = new Handler(this);
603 mIWindowManager = IWindowManager.Stub.asInterface(
604 ServiceManager.getService(Context.WINDOW_SERVICE));
605 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900606 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public void executeMessage(Message msg) {
608 handleMessage(msg);
609 }
610 });
satok01038492012-04-09 21:08:27 +0900611 mWindowManagerService = windowManager;
612 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900613
satok7cfc0ed2011-06-20 21:29:36 +0900614 mImeSwitcherNotification = new Notification();
615 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
616 mImeSwitcherNotification.when = 0;
617 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
618 mImeSwitcherNotification.tickerText = null;
619 mImeSwitcherNotification.defaults = 0; // please be quiet
620 mImeSwitcherNotification.sound = null;
621 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400622
623 // Tag this notification specially so SystemUI knows it's important
624 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
625
satok7cfc0ed2011-06-20 21:29:36 +0900626 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900627 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900628
629 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900630
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900631 final IntentFilter broadcastFilter = new IntentFilter();
632 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
633 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
634 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
635 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
satok7cfc0ed2011-06-20 21:29:36 +0900637 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 int userId = 0;
639 try {
640 ActivityManagerNative.getDefault().registerUserSwitchObserver(
641 new IUserSwitchObserver.Stub() {
642 @Override
643 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900644 synchronized(mMethodMap) {
645 switchUserLocked(newUserId);
646 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 if (reply != null) {
648 try {
649 reply.sendResult(null);
650 } catch (RemoteException e) {
651 }
652 }
653 }
654
655 @Override
656 public void onUserSwitchComplete(int newUserId) throws RemoteException {
657 }
658 });
659 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
660 } catch (RemoteException e) {
661 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
662 }
satok81f8b7c2012-12-04 20:42:56 +0900663 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900664
satokd87c2592010-09-29 11:52:06 +0900665 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900666 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900668 mFileManager = new InputMethodFileManager(mMethodMap, userId);
669 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900670
671 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900673 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900676 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
satok0a1bcf42012-05-16 19:26:31 +0900678 if (!mImeSelectedOnBoot) {
679 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900680 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 mSettingsObserver = new SettingsObserver(mHandler);
684 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900685
686 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
687 // according to the new system locale.
688 final IntentFilter filter = new IntentFilter();
689 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
690 mContext.registerReceiver(
691 new BroadcastReceiver() {
692 @Override
693 public void onReceive(Context context, Intent intent) {
694 synchronized(mMethodMap) {
695 checkCurrentLocaleChangedLocked();
696 }
697 }
698 }, filter);
699 }
700
satok5b927c432012-05-01 20:09:34 +0900701 private void resetDefaultImeLocked(Context context) {
702 // Do not reset the default (current) IME when it is a 3rd-party IME
703 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
704 return;
705 }
706
707 InputMethodInfo defIm = null;
708 for (InputMethodInfo imi : mMethodList) {
709 if (defIm == null) {
710 if (isValidSystemDefaultIme(imi, context)) {
711 defIm = imi;
712 Slog.i(TAG, "Selected default: " + imi.getId());
713 }
714 }
715 }
716 if (defIm == null && mMethodList.size() > 0) {
717 defIm = getMostApplicableDefaultIMELocked();
718 Slog.i(TAG, "No default found, using " + defIm.getId());
719 }
720 if (defIm != null) {
721 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
722 }
723 }
724
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900725 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
726 if (!mSystemReady) {
727 // not system ready
728 return;
729 }
730 final Locale newLocale = mRes.getConfiguration().locale;
731 if (!updateOnlyWhenLocaleChanged
732 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
733 if (!updateOnlyWhenLocaleChanged) {
734 hideCurrentInputLocked(0, null);
735 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700736 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900737 }
738 if (DEBUG) {
739 Slog.i(TAG, "Locale has been changed to " + newLocale);
740 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900741 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
742 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900743 buildInputMethodListLocked(mMethodList, mMethodMap);
744 if (!updateOnlyWhenLocaleChanged) {
745 final String selectedImiId = mSettings.getSelectedInputMethod();
746 if (TextUtils.isEmpty(selectedImiId)) {
747 // This is the first time of the user switch and
748 // set the current ime to the proper one.
749 resetDefaultImeLocked(mContext);
750 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900751 } else {
752 // If the locale is changed, needs to reset the default ime
753 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 }
755 updateFromSettingsLocked();
756 mLastSystemLocale = newLocale;
757 if (!updateOnlyWhenLocaleChanged) {
758 try {
759 startInputInnerLocked();
760 } catch (RuntimeException e) {
761 Slog.w(TAG, "Unexpected exception", e);
762 }
763 }
764 }
765 }
766
767 private void checkCurrentLocaleChangedLocked() {
768 resetAllInternalStateLocked(true);
769 }
770
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900771 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900773 // InputMethodFileManager should be reset when the user is changed
774 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900775 resetAllInternalStateLocked(false);
776 }
777
satok0a1bcf42012-05-16 19:26:31 +0900778 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
779 if (!mSystemReady) {
780 return false;
781 }
satok5b927c432012-05-01 20:09:34 +0900782 if (!isSystemIme(imi)) {
783 return false;
784 }
satok32b81202012-12-19 21:06:57 +0900785 if (imi.isAuxiliaryIme()) {
786 return false;
787 }
satok5b927c432012-05-01 20:09:34 +0900788 if (imi.getIsDefaultResourceId() != 0) {
789 try {
790 Resources res = context.createPackageContext(
791 imi.getPackageName(), 0).getResources();
792 if (res.getBoolean(imi.getIsDefaultResourceId())
793 && containsSubtypeOf(imi, context.getResources().getConfiguration().
794 locale.getLanguage())) {
795 return true;
796 }
797 } catch (PackageManager.NameNotFoundException ex) {
798 } catch (Resources.NotFoundException ex) {
799 }
800 }
801 if (imi.getSubtypeCount() == 0) {
802 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
803 }
804 return false;
805 }
806
807 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
808 if (!isSystemIme(imi)) {
809 return false;
810 }
satok32b81202012-12-19 21:06:57 +0900811 if (imi.isAuxiliaryIme()) {
812 return false;
813 }
satok5b927c432012-05-01 20:09:34 +0900814 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
815 }
816
817 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
818 final int N = imi.getSubtypeCount();
819 for (int i = 0; i < N; ++i) {
820 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
821 return true;
822 }
823 }
824 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
827 @Override
828 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
829 throws RemoteException {
830 try {
831 return super.onTransact(code, data, reply, flags);
832 } catch (RuntimeException e) {
833 // The input method manager only throws security exceptions, so let's
834 // log all others.
835 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838 throw e;
839 }
840 }
841
Dianne Hackborn661cd522011-08-22 00:26:20 -0700842 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700843 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900844 if (DEBUG) {
845 Slog.d(TAG, "--- systemReady");
846 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700847 if (!mSystemReady) {
848 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900849 mKeyguardManager =
850 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700851 mNotificationManager = (NotificationManager)
852 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
853 mStatusBar = statusBar;
854 statusBar.setIconVisibility("ime", false);
855 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900856 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
857 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900858 if (mShowOngoingImeSwitcherForPhones) {
859 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
860 mHardKeyboardListener);
861 }
satok0a1bcf42012-05-16 19:26:31 +0900862 buildInputMethodListLocked(mMethodList, mMethodMap);
863 if (!mImeSelectedOnBoot) {
864 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
865 checkCurrentLocaleChangedLocked();
866 }
867 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700868 try {
869 startInputInnerLocked();
870 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800871 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700872 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700873 }
874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800876
satok15452a42011-10-28 17:58:28 +0900877 private void setImeWindowVisibilityStatusHiddenLocked() {
878 mImeWindowVis = 0;
879 updateImeWindowStatusLocked();
880 }
881
satok3afd6c02011-11-18 08:38:19 +0900882 private void refreshImeWindowVisibilityLocked() {
883 final Configuration conf = mRes.getConfiguration();
884 final boolean haveHardKeyboard = conf.keyboard
885 != Configuration.KEYBOARD_NOKEYS;
886 final boolean hardKeyShown = haveHardKeyboard
887 && conf.hardKeyboardHidden
888 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500889 final boolean isScreenLocked =
890 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
891 final boolean isScreenSecurelyLocked =
892 isScreenLocked && mKeyguardManager.isKeyguardSecure();
893 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
894 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900895 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
896 updateImeWindowStatusLocked();
897 }
898
satok15452a42011-10-28 17:58:28 +0900899 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900900 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700901 }
902
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 // ---------------------------------------------------------------------------------------
904 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
905 // 1) it comes from the system process
906 // 2) the calling process' user id is identical to the current user id IMMS thinks.
907 private boolean calledFromValidUser() {
908 final int uid = Binder.getCallingUid();
909 final int userId = UserHandle.getUserId(uid);
910 if (DEBUG) {
911 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
912 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
913 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900914 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900915 }
916 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
917 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900918 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900919
920 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
921 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
922 // must not manage background users' states in any functions.
923 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
924 // by a token.
925 if (mContext.checkCallingOrSelfPermission(
926 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
927 == PackageManager.PERMISSION_GRANTED) {
928 if (DEBUG) {
929 Slog.d(TAG, "--- Access granted because the calling process has "
930 + "the INTERACT_ACROSS_USERS_FULL permission");
931 }
932 return true;
933 }
934 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
935 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900936 }
937
938 private boolean bindCurrentInputMethodService(
939 Intent service, ServiceConnection conn, int flags) {
940 if (service == null || conn == null) {
941 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
942 return false;
943 }
944 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
945 }
946
satoke7c6998e2011-06-03 17:57:59 +0900947 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 // TODO: Make this work even for non-current users?
950 if (!calledFromValidUser()) {
951 return Collections.emptyList();
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 synchronized (mMethodMap) {
954 return new ArrayList<InputMethodInfo>(mMethodList);
955 }
956 }
957
satoke7c6998e2011-06-03 17:57:59 +0900958 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900960 // TODO: Make this work even for non-current users?
961 if (!calledFromValidUser()) {
962 return Collections.emptyList();
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900965 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
967 }
968
satokbb4aa062011-01-19 21:40:27 +0900969 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
970 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
971 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
972 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900974 enabledInputMethodAndSubtypes.put(
975 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
976 }
977 return enabledInputMethodAndSubtypes;
978 }
979
980 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
981 boolean allowsImplicitlySelectedSubtypes) {
982 if (imi == null && mCurMethodId != null) {
983 imi = mMethodMap.get(mCurMethodId);
984 }
satok7265d9b2011-02-14 15:47:30 +0900985 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900986 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900987 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900988 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900989 }
satok7265d9b2011-02-14 15:47:30 +0900990 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900991 }
992
satoke7c6998e2011-06-03 17:57:59 +0900993 @Override
satok16331c82010-12-20 23:48:46 +0900994 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
995 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 // TODO: Make this work even for non-current users?
997 if (!calledFromValidUser()) {
998 return Collections.emptyList();
999 }
satok67ddf9c2010-11-17 09:45:54 +09001000 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09001001 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001002 }
1003 }
1004
satoke7c6998e2011-06-03 17:57:59 +09001005 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 public void addClient(IInputMethodClient client,
1007 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001008 if (!calledFromValidUser()) {
1009 return;
1010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 synchronized (mMethodMap) {
1012 mClients.put(client.asBinder(), new ClientState(client,
1013 inputContext, uid, pid));
1014 }
1015 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001016
satoke7c6998e2011-06-03 17:57:59 +09001017 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001019 if (!calledFromValidUser()) {
1020 return;
1021 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 synchronized (mMethodMap) {
1023 mClients.remove(client.asBinder());
1024 }
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 void executeOrSendMessage(IInterface target, Message msg) {
1028 if (target.asBinder() instanceof Binder) {
1029 mCaller.sendMessage(msg);
1030 } else {
1031 handleMessage(msg);
1032 msg.recycle();
1033 }
1034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001035
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001036 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001038 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 + mCurClient.client.asBinder());
1040 if (mBoundToMethod) {
1041 mBoundToMethod = false;
1042 if (mCurMethod != null) {
1043 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1044 MSG_UNBIND_INPUT, mCurMethod));
1045 }
1046 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001047
1048 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1049 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1051 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1052 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001054
The Android Open Source Project10592532009-03-18 17:39:46 -07001055 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
1057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 private int getImeShowFlags() {
1060 int flags = 0;
1061 if (mShowForced) {
1062 flags |= InputMethod.SHOW_FORCED
1063 | InputMethod.SHOW_EXPLICIT;
1064 } else if (mShowExplicitlyRequested) {
1065 flags |= InputMethod.SHOW_EXPLICIT;
1066 }
1067 return flags;
1068 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 private int getAppShowFlags() {
1071 int flags = 0;
1072 if (mShowForced) {
1073 flags |= InputMethodManager.SHOW_FORCED;
1074 } else if (!mShowExplicitlyRequested) {
1075 flags |= InputMethodManager.SHOW_IMPLICIT;
1076 }
1077 return flags;
1078 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079
Dianne Hackborn7663d802012-02-24 13:08:49 -08001080 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 if (!mBoundToMethod) {
1082 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1083 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1084 mBoundToMethod = true;
1085 }
1086 final SessionState session = mCurClient.curSession;
1087 if (initial) {
1088 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1089 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1090 } else {
1091 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1092 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1093 }
1094 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001095 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001096 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001098 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001102 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 // If no method is currently selected, do nothing.
1104 if (mCurMethodId == null) {
1105 return mNoBinding;
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 ClientState cs = mClients.get(client.asBinder());
1109 if (cs == null) {
1110 throw new IllegalArgumentException("unknown client "
1111 + client.asBinder());
1112 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 try {
1115 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1116 // Check with the window manager to make sure this client actually
1117 // has a window with focus. If not, reject. This is thread safe
1118 // because if the focus changes some time before or after, the
1119 // next client receiving focus that has any interest in input will
1120 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001121 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1123 return null;
1124 }
1125 } catch (RemoteException e) {
1126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001127
Dianne Hackborn7663d802012-02-24 13:08:49 -08001128 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1129 }
1130
1131 InputBindResult startInputUncheckedLocked(ClientState cs,
1132 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1133 // If no method is currently selected, do nothing.
1134 if (mCurMethodId == null) {
1135 return mNoBinding;
1136 }
1137
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001138 if (mCurClient == null) {
1139 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1140 if (DEBUG) {
1141 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1142 }
1143 }
1144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 if (mCurClient != cs) {
1146 // If the client is changing, we need to switch over to the new
1147 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001148 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001149 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 + cs.client.asBinder());
1151
1152 // If the screen is on, inform the new client it is active
1153 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001154 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1155 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 // Bump up the sequence for this client and attach it.
1160 mCurSeq++;
1161 if (mCurSeq <= 0) mCurSeq = 1;
1162 mCurClient = cs;
1163 mCurInputContext = inputContext;
1164 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 // Check if the input method is changing.
1167 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1168 if (cs.curSession != null) {
1169 // Fast case: if we are already connected to the input method,
1170 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001171 return attachNewInputLocked(
1172 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 if (mHaveConnection) {
1175 if (mCurMethod != null) {
1176 if (!cs.sessionRequested) {
1177 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001178 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1180 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001181 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 // Return to client, and we will get back with it when
1184 // we have had a session made for it.
1185 return new InputBindResult(null, mCurId, mCurSeq);
1186 } else if (SystemClock.uptimeMillis()
1187 < (mLastBindTime+TIME_TO_RECONNECT)) {
1188 // In this case we have connected to the service, but
1189 // don't yet have its interface. If it hasn't been too
1190 // long since we did the connection, we'll return to
1191 // the client and wait to get the service interface so
1192 // we can report back. If it has been too long, we want
1193 // to fall through so we can try a disconnect/reconnect
1194 // to see if we can get back in touch with the service.
1195 return new InputBindResult(null, mCurId, mCurSeq);
1196 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1198 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001203 return startInputInnerLocked();
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001206 InputBindResult startInputInnerLocked() {
1207 if (mCurMethodId == null) {
1208 return mNoBinding;
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001211 if (!mSystemReady) {
1212 // If the system is not yet ready, we shouldn't be running third
1213 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001214 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1218 if (info == null) {
1219 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001222 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1225 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001226 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1227 com.android.internal.R.string.input_method_binding_label);
1228 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1229 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001230 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001231 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 mLastBindTime = SystemClock.uptimeMillis();
1233 mHaveConnection = true;
1234 mCurId = info.getId();
1235 mCurToken = new Binder();
1236 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 mIWindowManager.addWindowToken(mCurToken,
1239 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1240 } catch (RemoteException e) {
1241 }
1242 return new InputBindResult(null, mCurId, mCurSeq);
1243 } else {
1244 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001245 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 + mCurIntent);
1247 }
1248 return null;
1249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001250
satoke7c6998e2011-06-03 17:57:59 +09001251 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001253 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001254 if (!calledFromValidUser()) {
1255 return null;
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 synchronized (mMethodMap) {
1258 final long ident = Binder.clearCallingIdentity();
1259 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001260 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 } finally {
1262 Binder.restoreCallingIdentity(ident);
1263 }
1264 }
1265 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266
satoke7c6998e2011-06-03 17:57:59 +09001267 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 public void finishInput(IInputMethodClient client) {
1269 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001270
satoke7c6998e2011-06-03 17:57:59 +09001271 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 public void onServiceConnected(ComponentName name, IBinder service) {
1273 synchronized (mMethodMap) {
1274 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1275 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001276 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001278 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001279 return;
1280 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001281 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001282 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1283 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001285 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001286 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001288 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001289 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 }
1292 }
1293 }
1294
1295 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1296 synchronized (mMethodMap) {
1297 if (mCurMethod != null && method != null
1298 && mCurMethod.asBinder() == method.asBinder()) {
1299 if (mCurClient != null) {
1300 mCurClient.curSession = new SessionState(mCurClient,
1301 method, session);
1302 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001303 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 if (res.method != null) {
1305 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1306 MSG_BIND_METHOD, mCurClient.client, res));
1307 }
1308 }
1309 }
1310 }
1311 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001312
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001313 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001314 if (mVisibleBound) {
1315 mContext.unbindService(mVisibleConnection);
1316 mVisibleBound = false;
1317 }
1318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 if (mHaveConnection) {
1320 mContext.unbindService(this);
1321 mHaveConnection = false;
1322 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001324 if (mCurToken != null) {
1325 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001326 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001327 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001328 // The current IME is shown. Hence an IME switch (transition) is happening.
1329 mWindowManagerService.saveLastInputMethodWindowForTransition();
1330 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001331 mIWindowManager.removeWindowToken(mCurToken);
1332 } catch (RemoteException e) {
1333 }
1334 mCurToken = null;
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
The Android Open Source Project10592532009-03-18 17:39:46 -07001337 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001338 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001339
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001340 if (reportToClient && mCurClient != null) {
1341 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1342 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1343 }
1344 }
1345
Devin Taylor0c33ed22010-02-23 13:26:46 -06001346 private void finishSession(SessionState sessionState) {
1347 if (sessionState != null && sessionState.session != null) {
1348 try {
1349 sessionState.session.finishSession();
1350 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001351 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001352 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001353 }
1354 }
1355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001356
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001357 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 if (mCurMethod != null) {
1359 for (ClientState cs : mClients.values()) {
1360 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001361 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 cs.curSession = null;
1363 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001364
1365 finishSession(mEnabledSession);
1366 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 mCurMethod = null;
1368 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001369 if (mStatusBar != null) {
1370 mStatusBar.setIconVisibility("ime", false);
1371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001373
satoke7c6998e2011-06-03 17:57:59 +09001374 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 public void onServiceDisconnected(ComponentName name) {
1376 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001377 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 + " mCurIntent=" + mCurIntent);
1379 if (mCurMethod != null && mCurIntent != null
1380 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001381 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 // We consider this to be a new bind attempt, since the system
1383 // should now try to restart the service for us.
1384 mLastBindTime = SystemClock.uptimeMillis();
1385 mShowRequested = mInputShown;
1386 mInputShown = false;
1387 if (mCurClient != null) {
1388 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1389 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1390 }
1391 }
1392 }
1393 }
1394
satokf9f01002011-05-19 21:31:50 +09001395 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001397 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 long ident = Binder.clearCallingIdentity();
1399 try {
1400 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001401 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 return;
1403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 synchronized (mMethodMap) {
1406 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001407 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001408 if (mStatusBar != null) {
1409 mStatusBar.setIconVisibility("ime", false);
1410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001412 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001413 CharSequence contentDescription = null;
1414 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001415 // Use PackageManager to load label
1416 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001417 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001418 mIPackageManager.getApplicationInfo(packageName, 0,
1419 mSettings.getCurrentUserId()));
1420 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001421 /* ignore */
1422 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001423 if (mStatusBar != null) {
1424 mStatusBar.setIcon("ime", packageName, iconId, 0,
1425 contentDescription != null
1426 ? contentDescription.toString() : null);
1427 mStatusBar.setIconVisibility("ime", true);
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 }
1431 } finally {
1432 Binder.restoreCallingIdentity(ident);
1433 }
1434 }
1435
satok7cfc0ed2011-06-20 21:29:36 +09001436 private boolean needsToShowImeSwitchOngoingNotification() {
1437 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001438 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001439 synchronized (mMethodMap) {
1440 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1441 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001442 if (N > 2) return true;
1443 if (N < 1) return false;
1444 int nonAuxCount = 0;
1445 int auxCount = 0;
1446 InputMethodSubtype nonAuxSubtype = null;
1447 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001448 for(int i = 0; i < N; ++i) {
1449 final InputMethodInfo imi = imis.get(i);
1450 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1451 imi, true);
1452 final int subtypeCount = subtypes.size();
1453 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001454 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001455 } else {
1456 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001457 final InputMethodSubtype subtype = subtypes.get(j);
1458 if (!subtype.isAuxiliary()) {
1459 ++nonAuxCount;
1460 nonAuxSubtype = subtype;
1461 } else {
1462 ++auxCount;
1463 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001464 }
1465 }
1466 }
satok7cfc0ed2011-06-20 21:29:36 +09001467 }
satokb6359412011-06-28 17:47:41 +09001468 if (nonAuxCount > 1 || auxCount > 1) {
1469 return true;
1470 } else if (nonAuxCount == 1 && auxCount == 1) {
1471 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001472 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1473 || auxSubtype.overridesImplicitlyEnabledSubtype()
1474 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001475 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1476 return false;
1477 }
1478 return true;
1479 }
1480 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001481 }
satok7cfc0ed2011-06-20 21:29:36 +09001482 }
1483
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001484 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001485 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001486 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001487 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001488 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001489 try {
1490 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001491 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001492 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001493 return;
1494 }
1495
1496 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001497 mImeWindowVis = vis;
1498 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001499 if (mStatusBar != null) {
1500 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1501 }
satok7cfc0ed2011-06-20 21:29:36 +09001502 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001503 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1504 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001505 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001506 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001507 final CharSequence title = mRes.getText(
1508 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001509 final CharSequence imiLabel = imi.loadLabel(pm);
1510 final CharSequence summary = mCurrentSubtype != null
1511 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1512 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1513 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001514 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001515 : imiLabel;
1516
satok7cfc0ed2011-06-20 21:29:36 +09001517 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001518 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001519 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001520 if (DEBUG) {
1521 Slog.d(TAG, "--- show notification: label = " + imiLabel
1522 + ", summary = " + summary);
1523 }
1524 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001525 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001526 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001527 mNotificationShown = true;
1528 }
satok7cfc0ed2011-06-20 21:29:36 +09001529 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001530 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001531 if (DEBUG) {
1532 Slog.d(TAG, "--- hide notification");
1533 }
1534 mNotificationManager.cancelAsUser(null,
1535 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001536 mNotificationShown = false;
1537 }
1538 }
satok06487a52010-10-29 11:37:18 +09001539 }
1540 } finally {
1541 Binder.restoreCallingIdentity(ident);
1542 }
1543 }
1544
satoke7c6998e2011-06-03 17:57:59 +09001545 @Override
satokf9f01002011-05-19 21:31:50 +09001546 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001547 if (!calledFromValidUser()) {
1548 return;
1549 }
satokf9f01002011-05-19 21:31:50 +09001550 synchronized (mMethodMap) {
1551 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1552 for (int i = 0; i < spans.length; ++i) {
1553 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001554 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001555 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001556 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001557 }
1558 }
1559 }
1560 }
1561
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
satokf9f01002011-05-19 21:31:50 +09001563 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 if (!calledFromValidUser()) {
1565 return false;
1566 }
satokf9f01002011-05-19 21:31:50 +09001567 synchronized (mMethodMap) {
1568 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1569 // TODO: Do not send the intent if the process of the targetImi is already dead.
1570 if (targetImi != null) {
1571 final String[] suggestions = span.getSuggestions();
1572 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001573 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001574 final Intent intent = new Intent();
1575 // Ensures that only a class in the original IME package will receive the
1576 // notification.
satok42c5a162011-05-26 16:46:14 +09001577 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001578 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1579 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1580 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001582 final long ident = Binder.clearCallingIdentity();
1583 try {
1584 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1585 } finally {
1586 Binder.restoreCallingIdentity(ident);
1587 }
satokf9f01002011-05-19 21:31:50 +09001588 return true;
1589 }
1590 }
1591 return false;
1592 }
1593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001595 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1596 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1597 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1598 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001599 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001600 // There is no input method selected, try to choose new applicable input method.
1601 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001602 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001603 }
1604 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 try {
satokab751aa2010-09-14 19:17:36 +09001606 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001608 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001609 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001610 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
satokf3db1af2010-11-23 13:34:33 +09001612 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001613 } else {
1614 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001615 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001616 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
1618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
satokab751aa2010-09-14 19:17:36 +09001620 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 InputMethodInfo info = mMethodMap.get(id);
1622 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001623 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001625
satokd81e9502012-05-21 12:58:45 +09001626 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001628 final int subtypeCount = info.getSubtypeCount();
1629 if (subtypeCount <= 0) {
1630 return;
satokcd7cd292010-11-20 15:46:23 +09001631 }
satokd81e9502012-05-21 12:58:45 +09001632 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1633 final InputMethodSubtype newSubtype;
1634 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1635 newSubtype = info.getSubtypeAt(subtypeId);
1636 } else {
1637 // If subtype is null, try to find the most applicable one from
1638 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001639 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001640 }
1641 if (newSubtype == null || oldSubtype == null) {
1642 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1643 + ", new subtype = " + newSubtype);
1644 return;
1645 }
1646 if (newSubtype != oldSubtype) {
1647 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1648 if (mCurMethod != null) {
1649 try {
1650 refreshImeWindowVisibilityLocked();
1651 mCurMethod.changeInputMethodSubtype(newSubtype);
1652 } catch (RemoteException e) {
1653 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001654 }
1655 }
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 return;
1658 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001659
satokd81e9502012-05-21 12:58:45 +09001660 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 final long ident = Binder.clearCallingIdentity();
1662 try {
satokab751aa2010-09-14 19:17:36 +09001663 // Set a subtype to this input method.
1664 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001665 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1666 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1667 // because mCurMethodId is stored as a history in
1668 // setSelectedInputMethodAndSubtypeLocked().
1669 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670
1671 if (ActivityManagerNative.isSystemReady()) {
1672 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001673 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001675 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001677 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 } finally {
1679 Binder.restoreCallingIdentity(ident);
1680 }
1681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001682
satok42c5a162011-05-26 16:46:14 +09001683 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001684 public boolean showSoftInput(IInputMethodClient client, int flags,
1685 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001686 if (!calledFromValidUser()) {
1687 return false;
1688 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001689 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 long ident = Binder.clearCallingIdentity();
1691 try {
1692 synchronized (mMethodMap) {
1693 if (mCurClient == null || client == null
1694 || mCurClient.client.asBinder() != client.asBinder()) {
1695 try {
1696 // We need to check if this is the current client with
1697 // focus in the window manager, to allow this call to
1698 // be made before input is started in it.
1699 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001700 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001701 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001704 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
Joe Onorato8a9b2202010-02-26 18:56:32 -08001708 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001709 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
1711 } finally {
1712 Binder.restoreCallingIdentity(ident);
1713 }
1714 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001715
The Android Open Source Project4df24232009-03-05 14:34:35 -08001716 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 mShowRequested = true;
1718 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1719 mShowExplicitlyRequested = true;
1720 }
1721 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1722 mShowExplicitlyRequested = true;
1723 mShowForced = true;
1724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
Dianne Hackborncc278702009-09-02 23:07:23 -07001726 if (!mSystemReady) {
1727 return false;
1728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001729
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001732 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1733 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1734 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001736 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001737 bindCurrentInputMethodService(
1738 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001739 mVisibleBound = true;
1740 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001741 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001743 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 // The client has asked to have the input method shown, but
1745 // we have been sitting here too long with a connection to the
1746 // service and no interface received, so let's disconnect/connect
1747 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001748 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001750 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001752 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001753 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001754 } else {
1755 if (DEBUG) {
1756 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1757 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001760
The Android Open Source Project4df24232009-03-05 14:34:35 -08001761 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001763
satok42c5a162011-05-26 16:46:14 +09001764 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001765 public boolean hideSoftInput(IInputMethodClient client, int flags,
1766 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001767 if (!calledFromValidUser()) {
1768 return false;
1769 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001770 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 long ident = Binder.clearCallingIdentity();
1772 try {
1773 synchronized (mMethodMap) {
1774 if (mCurClient == null || client == null
1775 || mCurClient.client.asBinder() != client.asBinder()) {
1776 try {
1777 // We need to check if this is the current client with
1778 // focus in the window manager, to allow this call to
1779 // be made before input is started in it.
1780 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001781 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1782 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001783 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001784 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001787 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
Joe Onorato8a9b2202010-02-26 18:56:32 -08001792 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001793 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 }
1795 } finally {
1796 Binder.restoreCallingIdentity(ident);
1797 }
1798 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001799
The Android Open Source Project4df24232009-03-05 14:34:35 -08001800 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1802 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001803 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001805 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
1807 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001808 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001812 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001814 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1815 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1816 res = true;
1817 } else {
1818 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001820 if (mHaveConnection && mVisibleBound) {
1821 mContext.unbindService(mVisibleConnection);
1822 mVisibleBound = false;
1823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 mInputShown = false;
1825 mShowRequested = false;
1826 mShowExplicitlyRequested = false;
1827 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001828 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001830
satok42c5a162011-05-26 16:46:14 +09001831 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001832 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1833 int controlFlags, int softInputMode, int windowFlags,
1834 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001835 // Needs to check the validity before clearing calling identity
1836 final boolean calledFromValidUser = calledFromValidUser();
1837
Dianne Hackborn7663d802012-02-24 13:08:49 -08001838 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 long ident = Binder.clearCallingIdentity();
1840 try {
1841 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001842 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001843 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001845 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001846
Dianne Hackborn7663d802012-02-24 13:08:49 -08001847 ClientState cs = mClients.get(client.asBinder());
1848 if (cs == null) {
1849 throw new IllegalArgumentException("unknown client "
1850 + client.asBinder());
1851 }
1852
1853 try {
1854 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1855 // Check with the window manager to make sure this client actually
1856 // has a window with focus. If not, reject. This is thread safe
1857 // because if the focus changes some time before or after, the
1858 // next client receiving focus that has any interest in input will
1859 // be calling through here after that change happens.
1860 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1861 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1862 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001864 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001866
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001867 if (!calledFromValidUser) {
1868 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1869 Slog.w(TAG, "If you want to interect with IME, you need "
1870 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1871 hideCurrentInputLocked(0, null);
1872 return null;
1873 }
1874
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001875 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001876 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001877 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001878 if (attribute != null) {
1879 return startInputUncheckedLocked(cs, inputContext, attribute,
1880 controlFlags);
1881 }
1882 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001883 }
1884 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001885
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001886 // Should we auto-show the IME even if the caller has not
1887 // specified what should be done with it?
1888 // We only do this automatically if the window can resize
1889 // to accommodate the IME (so what the user sees will give
1890 // them good context without input information being obscured
1891 // by the IME) or if running on a large screen where there
1892 // is more room for the target window + IME.
1893 final boolean doAutoShow =
1894 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1895 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1896 || mRes.getConfiguration().isLayoutSizeAtLeast(
1897 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001898 final boolean isTextEditor =
1899 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1900
1901 // We want to start input before showing the IME, but after closing
1902 // it. We want to do this after closing it to help the IME disappear
1903 // more quickly (not get stuck behind it initializing itself for the
1904 // new focused input, even if its window wants to hide the IME).
1905 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1908 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001909 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1911 // There is no focus view, and this window will
1912 // be behind any soft input window, so hide the
1913 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001914 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001915 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001917 } else if (isTextEditor && doAutoShow && (softInputMode &
1918 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 // There is a focus view, and we are navigating forward
1920 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001921 // We only do this automatically if the window can resize
1922 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001923 // them good context without input information being obscured
1924 // by the IME) or if running on a large screen where there
1925 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001926 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001927 if (attribute != null) {
1928 res = startInputUncheckedLocked(cs, inputContext, attribute,
1929 controlFlags);
1930 didStart = true;
1931 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001932 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 break;
1935 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1936 // Do nothing.
1937 break;
1938 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1939 if ((softInputMode &
1940 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001942 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 break;
1945 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001947 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 break;
1949 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1950 if ((softInputMode &
1951 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001953 if (attribute != null) {
1954 res = startInputUncheckedLocked(cs, inputContext, attribute,
1955 controlFlags);
1956 didStart = true;
1957 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001958 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
1960 break;
1961 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001962 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001963 if (attribute != null) {
1964 res = startInputUncheckedLocked(cs, inputContext, attribute,
1965 controlFlags);
1966 didStart = true;
1967 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001968 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 break;
1970 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001971
1972 if (!didStart && attribute != null) {
1973 res = startInputUncheckedLocked(cs, inputContext, attribute,
1974 controlFlags);
1975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
1977 } finally {
1978 Binder.restoreCallingIdentity(ident);
1979 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001980
1981 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001983
satok42c5a162011-05-26 16:46:14 +09001984 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001986 if (!calledFromValidUser()) {
1987 return;
1988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 synchronized (mMethodMap) {
1990 if (mCurClient == null || client == null
1991 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001992 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001993 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
1995
satok440aab52010-11-25 09:43:11 +09001996 // Always call subtype picker, because subtype picker is a superset of input method
1997 // picker.
satokab751aa2010-09-14 19:17:36 +09001998 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1999 }
2000 }
2001
satok42c5a162011-05-26 16:46:14 +09002002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002004 if (!calledFromValidUser()) {
2005 return;
2006 }
satok28203512010-11-24 11:06:49 +09002007 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2008 }
2009
satok42c5a162011-05-26 16:46:14 +09002010 @Override
satok28203512010-11-24 11:06:49 +09002011 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002012 if (!calledFromValidUser()) {
2013 return;
2014 }
satok28203512010-11-24 11:06:49 +09002015 synchronized (mMethodMap) {
2016 if (subtype != null) {
2017 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
2018 mMethodMap.get(id), subtype.hashCode()));
2019 } else {
2020 setInputMethod(token, id);
2021 }
2022 }
satokab751aa2010-09-14 19:17:36 +09002023 }
2024
satok42c5a162011-05-26 16:46:14 +09002025 @Override
satokb416a712010-11-25 20:42:14 +09002026 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002027 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002028 if (!calledFromValidUser()) {
2029 return;
2030 }
satokb416a712010-11-25 20:42:14 +09002031 synchronized (mMethodMap) {
2032 if (mCurClient == null || client == null
2033 || mCurClient.client.asBinder() != client.asBinder()) {
2034 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2035 }
satok7fee71f2010-12-17 18:54:26 +09002036 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2037 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002038 }
2039 }
2040
satok4fc87d62011-05-20 16:13:43 +09002041 @Override
satok735cf382010-11-11 20:40:09 +09002042 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002043 if (!calledFromValidUser()) {
2044 return false;
2045 }
satok735cf382010-11-11 20:40:09 +09002046 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002047 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002048 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002049 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002050 lastImi = mMethodMap.get(lastIme.first);
2051 } else {
2052 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002053 }
satok4fc87d62011-05-20 16:13:43 +09002054 String targetLastImiId = null;
2055 int subtypeId = NOT_A_SUBTYPE_ID;
2056 if (lastIme != null && lastImi != null) {
2057 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2058 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2059 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2060 : mCurrentSubtype.hashCode();
2061 // If the last IME is the same as the current IME and the last subtype is not
2062 // defined, there is no need to switch to the last IME.
2063 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2064 targetLastImiId = lastIme.first;
2065 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2066 }
2067 }
2068
2069 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2070 // This is a safety net. If the currentSubtype can't be added to the history
2071 // and the framework couldn't find the last ime, we will make the last ime be
2072 // the most applicable enabled keyboard subtype of the system imes.
2073 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2074 if (enabled != null) {
2075 final int N = enabled.size();
2076 final String locale = mCurrentSubtype == null
2077 ? mRes.getConfiguration().locale.toString()
2078 : mCurrentSubtype.getLocale();
2079 for (int i = 0; i < N; ++i) {
2080 final InputMethodInfo imi = enabled.get(i);
2081 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2082 InputMethodSubtype keyboardSubtype =
2083 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2084 SUBTYPE_MODE_KEYBOARD, locale, true);
2085 if (keyboardSubtype != null) {
2086 targetLastImiId = imi.getId();
2087 subtypeId = getSubtypeIdFromHashCode(
2088 imi, keyboardSubtype.hashCode());
2089 if(keyboardSubtype.getLocale().equals(locale)) {
2090 break;
2091 }
2092 }
2093 }
2094 }
2095 }
2096 }
2097
2098 if (!TextUtils.isEmpty(targetLastImiId)) {
2099 if (DEBUG) {
2100 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2101 + ", from: " + mCurMethodId + ", " + subtypeId);
2102 }
2103 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2104 return true;
2105 } else {
2106 return false;
2107 }
satok735cf382010-11-11 20:40:09 +09002108 }
2109 }
2110
satoke7c6998e2011-06-03 17:57:59 +09002111 @Override
satok688bd472012-02-09 20:09:17 +09002112 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002113 if (!calledFromValidUser()) {
2114 return false;
2115 }
satok688bd472012-02-09 20:09:17 +09002116 synchronized (mMethodMap) {
2117 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2118 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2119 if (nextSubtype == null) {
2120 return false;
2121 }
2122 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2123 return true;
2124 }
2125 }
2126
2127 @Override
satok68f1b782011-04-11 14:26:04 +09002128 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002129 if (!calledFromValidUser()) {
2130 return null;
2131 }
satok68f1b782011-04-11 14:26:04 +09002132 synchronized (mMethodMap) {
2133 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2134 // TODO: Handle the case of the last IME with no subtypes
2135 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2136 || TextUtils.isEmpty(lastIme.second)) return null;
2137 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2138 if (lastImi == null) return null;
2139 try {
2140 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002141 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2142 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2143 return null;
2144 }
2145 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002146 } catch (NumberFormatException e) {
2147 return null;
2148 }
2149 }
2150 }
2151
satoke7c6998e2011-06-03 17:57:59 +09002152 @Override
satokee5e77c2011-09-02 18:50:15 +09002153 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002154 if (!calledFromValidUser()) {
2155 return;
2156 }
satok91e88122011-07-18 11:11:42 +09002157 // By this IPC call, only a process which shares the same uid with the IME can add
2158 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002159 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002160 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002161 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002162 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002163 final String[] packageInfos;
2164 try {
2165 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2166 } catch (RemoteException e) {
2167 Slog.e(TAG, "Failed to get package infos");
2168 return;
2169 }
satok91e88122011-07-18 11:11:42 +09002170 if (packageInfos != null) {
2171 final int packageNum = packageInfos.length;
2172 for (int i = 0; i < packageNum; ++i) {
2173 if (packageInfos[i].equals(imi.getPackageName())) {
2174 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002175 final long ident = Binder.clearCallingIdentity();
2176 try {
2177 buildInputMethodListLocked(mMethodList, mMethodMap);
2178 } finally {
2179 Binder.restoreCallingIdentity(ident);
2180 }
satokee5e77c2011-09-02 18:50:15 +09002181 return;
satok91e88122011-07-18 11:11:42 +09002182 }
2183 }
2184 }
satoke7c6998e2011-06-03 17:57:59 +09002185 }
satokee5e77c2011-09-02 18:50:15 +09002186 return;
satoke7c6998e2011-06-03 17:57:59 +09002187 }
2188
satok28203512010-11-24 11:06:49 +09002189 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 synchronized (mMethodMap) {
2191 if (token == null) {
2192 if (mContext.checkCallingOrSelfPermission(
2193 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2194 != PackageManager.PERMISSION_GRANTED) {
2195 throw new SecurityException(
2196 "Using null token requires permission "
2197 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2198 }
2199 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002200 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2201 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 return;
2203 }
2204
satokc5933802011-08-31 21:26:04 +09002205 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 try {
satokab751aa2010-09-14 19:17:36 +09002207 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 } finally {
2209 Binder.restoreCallingIdentity(ident);
2210 }
2211 }
2212 }
2213
satok42c5a162011-05-26 16:46:14 +09002214 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002216 if (!calledFromValidUser()) {
2217 return;
2218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 synchronized (mMethodMap) {
2220 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002221 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2222 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 return;
2224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 long ident = Binder.clearCallingIdentity();
2226 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227 hideCurrentInputLocked(flags, null);
2228 } finally {
2229 Binder.restoreCallingIdentity(ident);
2230 }
2231 }
2232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002233
satok42c5a162011-05-26 16:46:14 +09002234 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002236 if (!calledFromValidUser()) {
2237 return;
2238 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002239 synchronized (mMethodMap) {
2240 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002241 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2242 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002243 return;
2244 }
2245 long ident = Binder.clearCallingIdentity();
2246 try {
2247 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 } finally {
2249 Binder.restoreCallingIdentity(ident);
2250 }
2251 }
2252 }
2253
2254 void setEnabledSessionInMainThread(SessionState session) {
2255 if (mEnabledSession != session) {
2256 if (mEnabledSession != null) {
2257 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 mEnabledSession.method.setSessionEnabled(
2260 mEnabledSession.session, false);
2261 } catch (RemoteException e) {
2262 }
2263 }
2264 mEnabledSession = session;
2265 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002266 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 session.method.setSessionEnabled(
2268 session.session, true);
2269 } catch (RemoteException e) {
2270 }
2271 }
2272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002273
satok42c5a162011-05-26 16:46:14 +09002274 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002276 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 switch (msg.what) {
2278 case MSG_SHOW_IM_PICKER:
2279 showInputMethodMenu();
2280 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002281
satokab751aa2010-09-14 19:17:36 +09002282 case MSG_SHOW_IM_SUBTYPE_PICKER:
2283 showInputMethodSubtypeMenu();
2284 return true;
2285
satok47a44912010-10-06 16:03:58 +09002286 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002287 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002288 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002289 args.recycle();
satok217f5482010-12-15 05:19:19 +09002290 return true;
2291
2292 case MSG_SHOW_IM_CONFIG:
2293 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002294 return true;
2295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 case MSG_UNBIND_INPUT:
2299 try {
2300 ((IInputMethod)msg.obj).unbindInput();
2301 } catch (RemoteException e) {
2302 // There is nothing interesting about the method dying.
2303 }
2304 return true;
2305 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002306 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 try {
2308 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2309 } catch (RemoteException e) {
2310 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002311 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 return true;
2313 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002314 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002316 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2317 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 } catch (RemoteException e) {
2319 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002320 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 return true;
2322 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002323 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002325 ((IInputMethod)args.arg1).hideSoftInput(0,
2326 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 } catch (RemoteException e) {
2328 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002329 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 return true;
2331 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002332 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002334 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2336 } catch (RemoteException e) {
2337 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002338 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 return true;
2340 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002341 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 try {
2343 ((IInputMethod)args.arg1).createSession(
2344 (IInputMethodCallback)args.arg2);
2345 } catch (RemoteException e) {
2346 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002347 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002352 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 try {
2354 SessionState session = (SessionState)args.arg1;
2355 setEnabledSessionInMainThread(session);
2356 session.method.startInput((IInputContext)args.arg2,
2357 (EditorInfo)args.arg3);
2358 } catch (RemoteException e) {
2359 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002360 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 return true;
2362 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002363 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 try {
2365 SessionState session = (SessionState)args.arg1;
2366 setEnabledSessionInMainThread(session);
2367 session.method.restartInput((IInputContext)args.arg2,
2368 (EditorInfo)args.arg3);
2369 } catch (RemoteException e) {
2370 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002371 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 case MSG_UNBIND_METHOD:
2377 try {
2378 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2379 } catch (RemoteException e) {
2380 // There is nothing interesting about the last client dying.
2381 }
2382 return true;
2383 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002384 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 try {
2386 ((IInputMethodClient)args.arg1).onBindMethod(
2387 (InputBindResult)args.arg2);
2388 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002389 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002391 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002393 case MSG_SET_ACTIVE:
2394 try {
2395 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2396 } catch (RemoteException e) {
2397 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2398 + ((ClientState)msg.obj).pid + " uid "
2399 + ((ClientState)msg.obj).uid);
2400 }
2401 return true;
satok01038492012-04-09 21:08:27 +09002402
2403 // --------------------------------------------------------------
2404 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2405 mHardKeyboardListener.handleHardKeyboardStatusChange(
2406 msg.arg1 == 1, msg.arg2 == 1);
2407 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 }
2409 return false;
2410 }
2411
satok5b927c432012-05-01 20:09:34 +09002412 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002413 return (inputMethod.getServiceInfo().applicationInfo.flags
2414 & ApplicationInfo.FLAG_SYSTEM) != 0;
2415 }
2416
Ken Wakasa586f0512011-01-20 22:31:01 +09002417 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2418 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2419 final int subtypeCount = imi.getSubtypeCount();
2420 for (int i = 0; i < subtypeCount; ++i) {
2421 subtypes.add(imi.getSubtypeAt(i));
2422 }
2423 return subtypes;
2424 }
2425
satoka86f5e42011-09-02 17:12:42 +09002426 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2427 InputMethodInfo imi, String mode) {
2428 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2429 final int subtypeCount = imi.getSubtypeCount();
2430 for (int i = 0; i < subtypeCount; ++i) {
2431 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2432 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2433 subtypes.add(subtype);
2434 }
2435 }
2436 return subtypes;
2437 }
2438
satokdc9ddae2011-10-06 12:22:36 +09002439 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002440 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002441 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002442 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002443 int i = enabled.size();
2444 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002445 while (i > 0) {
2446 i--;
satokdc9ddae2011-10-06 12:22:36 +09002447 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002448 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2449 return imi;
2450 }
2451 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2452 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002453 }
2454 }
satok0a1bcf42012-05-16 19:26:31 +09002455 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002456 }
2457 return null;
2458 }
2459
2460 private boolean chooseNewDefaultIMELocked() {
2461 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2462 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002463 if (DEBUG) {
2464 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2465 }
satok723a27e2010-11-11 14:58:11 +09002466 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002467 return true;
2468 }
2469
2470 return false;
2471 }
2472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2474 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 if (DEBUG) {
2476 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 list.clear();
2479 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002480
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002481 // Use for queryIntentServicesAsUser
2482 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002483 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002484 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002485 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002486 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002488 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002490 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
satoke7c6998e2011-06-03 17:57:59 +09002492 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2493 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 for (int i = 0; i < services.size(); ++i) {
2495 ResolveInfo ri = services.get(i);
2496 ServiceInfo si = ri.serviceInfo;
2497 ComponentName compName = new ComponentName(si.packageName, si.name);
2498 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2499 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002500 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 + ": it does not require the permission "
2502 + android.Manifest.permission.BIND_INPUT_METHOD);
2503 continue;
2504 }
2505
Joe Onorato8a9b2202010-02-26 18:56:32 -08002506 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
2508 try {
satoke7c6998e2011-06-03 17:57:59 +09002509 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002511 final String id = p.getId();
2512 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513
satok5b927c432012-05-01 20:09:34 +09002514 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002515 // by default
2516 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002517 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002518 }
2519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002521 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002525 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002527 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
2529 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002530
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002531 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002532 if (!TextUtils.isEmpty(defaultImiId)) {
2533 if (!map.containsKey(defaultImiId)) {
2534 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2535 if (chooseNewDefaultIMELocked()) {
2536 updateFromSettingsLocked();
2537 }
2538 } else {
2539 // Double check that the default IME is certainly enabled.
2540 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002541 }
2542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002546
satokab751aa2010-09-14 19:17:36 +09002547 private void showInputMethodMenu() {
2548 showInputMethodMenuInternal(false);
2549 }
2550
2551 private void showInputMethodSubtypeMenu() {
2552 showInputMethodMenuInternal(true);
2553 }
2554
satok217f5482010-12-15 05:19:19 +09002555 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002556 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002557 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002558 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2559 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002560 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002561 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002562 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002563 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002564 }
2565
2566 private void showConfigureInputMethods() {
2567 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2568 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2569 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2570 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002571 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002572 }
2573
satok2c93efc2012-04-02 19:33:47 +09002574 private boolean isScreenLocked() {
2575 return mKeyguardManager != null
2576 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2577 }
satokab751aa2010-09-14 19:17:36 +09002578 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002579 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002582 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002583
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002584 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002585 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002586 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002587
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002588 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002589 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2590 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002591 if (immis == null || immis.size() == 0) {
2592 return;
2593 }
2594
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002595 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596
satok688bd472012-02-09 20:09:17 +09002597 final List<ImeSubtypeListItem> imList =
2598 mImListManager.getSortedInputMethodAndSubtypeList(
2599 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002600
satokc3690562012-01-10 20:14:43 +09002601 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002602 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002603 if (currentSubtype != null) {
2604 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2605 lastInputMethodSubtypeId =
2606 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2607 }
2608 }
2609
Ken Wakasa761eb372011-03-04 19:06:18 +09002610 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002611 mIms = new InputMethodInfo[N];
2612 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002613 int checkedItem = 0;
2614 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002615 final ImeSubtypeListItem item = imList.get(i);
2616 mIms[i] = item.mImi;
2617 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002618 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002619 int subtypeId = mSubtypeIds[i];
2620 if ((subtypeId == NOT_A_SUBTYPE_ID)
2621 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2622 || (subtypeId == lastInputMethodSubtypeId)) {
2623 checkedItem = i;
2624 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002627 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002628 com.android.internal.R.styleable.DialogPreference,
2629 com.android.internal.R.attr.alertDialogStyle, 0);
2630 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002631 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002632 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002633 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002635 }
2636 })
2637 .setIcon(a.getDrawable(
2638 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2639 a.recycle();
satok01038492012-04-09 21:08:27 +09002640 final LayoutInflater inflater =
2641 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2642 final View tv = inflater.inflate(
2643 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2644 mDialogBuilder.setCustomTitle(tv);
2645
2646 // Setup layout for a toggle switch of the hardware keyboard
2647 mSwitchingDialogTitleView = tv;
2648 mSwitchingDialogTitleView.findViewById(
2649 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2650 mWindowManagerService.isHardKeyboardAvailable() ?
2651 View.VISIBLE : View.GONE);
2652 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2653 com.android.internal.R.id.hard_keyboard_switch));
2654 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2655 hardKeySwitch.setOnCheckedChangeListener(
2656 new OnCheckedChangeListener() {
2657 @Override
2658 public void onCheckedChanged(
2659 CompoundButton buttonView, boolean isChecked) {
2660 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2661 }
2662 });
satokd87c2592010-09-29 11:52:06 +09002663
Ken Wakasa05dbb652011-08-22 15:22:43 +09002664 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2665 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2666 checkedItem);
2667
2668 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002669 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002670 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002671 public void onClick(DialogInterface dialog, int which) {
2672 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002673 if (mIms == null || mIms.length <= which
2674 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002675 return;
2676 }
2677 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002678 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002679 hideInputMethodMenu();
2680 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002681 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002682 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002683 subtypeId = NOT_A_SUBTYPE_ID;
2684 }
2685 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002686 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002689 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690
satokbc81b692011-08-26 16:22:22 +09002691 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002692 mDialogBuilder.setPositiveButton(
2693 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002694 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002695 @Override
satok7f35c8c2010-10-07 21:13:11 +09002696 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002697 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002698 }
2699 });
2700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002702 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 mSwitchingDialog.getWindow().setType(
2704 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002705 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2706 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002707 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 mSwitchingDialog.show();
2709 }
2710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002711
satok93d744d2012-05-09 17:14:08 +09002712 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002713 public final CharSequence mImeName;
2714 public final CharSequence mSubtypeName;
2715 public final InputMethodInfo mImi;
2716 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002717 private final boolean mIsSystemLocale;
2718 private final boolean mIsSystemLanguage;
2719
Ken Wakasa05dbb652011-08-22 15:22:43 +09002720 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002721 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002722 mImeName = imeName;
2723 mSubtypeName = subtypeName;
2724 mImi = imi;
2725 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002726 if (TextUtils.isEmpty(subtypeLocale)) {
2727 mIsSystemLocale = false;
2728 mIsSystemLanguage = false;
2729 } else {
2730 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2731 mIsSystemLanguage = mIsSystemLocale
2732 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2733 }
2734 }
2735
2736 @Override
2737 public int compareTo(ImeSubtypeListItem other) {
2738 if (TextUtils.isEmpty(mImeName)) {
2739 return 1;
2740 }
2741 if (TextUtils.isEmpty(other.mImeName)) {
2742 return -1;
2743 }
2744 if (!TextUtils.equals(mImeName, other.mImeName)) {
2745 return mImeName.toString().compareTo(other.mImeName.toString());
2746 }
2747 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2748 return 0;
2749 }
2750 if (mIsSystemLocale) {
2751 return -1;
2752 }
2753 if (other.mIsSystemLocale) {
2754 return 1;
2755 }
2756 if (mIsSystemLanguage) {
2757 return -1;
2758 }
2759 if (other.mIsSystemLanguage) {
2760 return 1;
2761 }
2762 if (TextUtils.isEmpty(mSubtypeName)) {
2763 return 1;
2764 }
2765 if (TextUtils.isEmpty(other.mSubtypeName)) {
2766 return -1;
2767 }
2768 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002769 }
2770 }
2771
2772 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2773 private final LayoutInflater mInflater;
2774 private final int mTextViewResourceId;
2775 private final List<ImeSubtypeListItem> mItemsList;
2776 private final int mCheckedItem;
2777 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2778 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2779 super(context, textViewResourceId, itemsList);
2780 mTextViewResourceId = textViewResourceId;
2781 mItemsList = itemsList;
2782 mCheckedItem = checkedItem;
2783 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2784 }
2785
2786 @Override
2787 public View getView(int position, View convertView, ViewGroup parent) {
2788 final View view = convertView != null ? convertView
2789 : mInflater.inflate(mTextViewResourceId, null);
2790 if (position < 0 || position >= mItemsList.size()) return view;
2791 final ImeSubtypeListItem item = mItemsList.get(position);
2792 final CharSequence imeName = item.mImeName;
2793 final CharSequence subtypeName = item.mSubtypeName;
2794 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2795 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2796 if (TextUtils.isEmpty(subtypeName)) {
2797 firstTextView.setText(imeName);
2798 secondTextView.setVisibility(View.GONE);
2799 } else {
2800 firstTextView.setText(subtypeName);
2801 secondTextView.setText(imeName);
2802 secondTextView.setVisibility(View.VISIBLE);
2803 }
2804 final RadioButton radioButton =
2805 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2806 radioButton.setChecked(position == mCheckedItem);
2807 return view;
2808 }
2809 }
2810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002812 synchronized (mMethodMap) {
2813 hideInputMethodMenuLocked();
2814 }
2815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
The Android Open Source Project10592532009-03-18 17:39:46 -07002817 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002818 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819
The Android Open Source Project10592532009-03-18 17:39:46 -07002820 if (mSwitchingDialog != null) {
2821 mSwitchingDialog.dismiss();
2822 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002824
The Android Open Source Project10592532009-03-18 17:39:46 -07002825 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002826 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002830
satok42c5a162011-05-26 16:46:14 +09002831 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002833 // TODO: Make this work even for non-current users?
2834 if (!calledFromValidUser()) {
2835 return false;
2836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 synchronized (mMethodMap) {
2838 if (mContext.checkCallingOrSelfPermission(
2839 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2840 != PackageManager.PERMISSION_GRANTED) {
2841 throw new SecurityException(
2842 "Requires permission "
2843 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2844 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 long ident = Binder.clearCallingIdentity();
2847 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 } finally {
2850 Binder.restoreCallingIdentity(ident);
2851 }
2852 }
2853 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002854
2855 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2856 // Make sure this is a valid input method.
2857 InputMethodInfo imm = mMethodMap.get(id);
2858 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002859 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002860 }
2861
satokd87c2592010-09-29 11:52:06 +09002862 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2863 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002864
satok32b81202012-12-19 21:06:57 +09002865 if (DEBUG) {
2866 Slog.d(TAG, (enabled ? "Enable " : "Disable ") + id);
2867 }
satokd87c2592010-09-29 11:52:06 +09002868 if (enabled) {
2869 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2870 if (pair.first.equals(id)) {
2871 // We are enabling this input method, but it is already enabled.
2872 // Nothing to do. The previous state was enabled.
2873 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002874 }
2875 }
satokd87c2592010-09-29 11:52:06 +09002876 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2877 // Previous state was disabled.
2878 return false;
2879 } else {
2880 StringBuilder builder = new StringBuilder();
2881 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2882 builder, enabledInputMethodsList, id)) {
2883 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002884 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002885 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2886 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2887 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002888 }
2889 // Previous state was enabled.
2890 return true;
2891 } else {
2892 // We are disabling the input method but it is already disabled.
2893 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002894 return false;
2895 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002896 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002897 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002898
satok57ffc002011-01-25 00:11:47 +09002899 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2900 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002901 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002902 }
2903
satok723a27e2010-11-11 14:58:11 +09002904 private void saveCurrentInputMethodAndSubtypeToHistory() {
2905 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2906 if (mCurrentSubtype != null) {
2907 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2908 }
satok57ffc002011-01-25 00:11:47 +09002909 if (canAddToLastInputMethod(mCurrentSubtype)) {
2910 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2911 }
satokab751aa2010-09-14 19:17:36 +09002912 }
2913
satok723a27e2010-11-11 14:58:11 +09002914 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2915 boolean setSubtypeOnly) {
2916 // Update the history of InputMethod and Subtype
2917 saveCurrentInputMethodAndSubtypeToHistory();
2918
2919 // Set Subtype here
2920 if (imi == null || subtypeId < 0) {
2921 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002922 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002923 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002924 if (subtypeId < imi.getSubtypeCount()) {
2925 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2926 mSettings.putSelectedSubtype(subtype.hashCode());
2927 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002928 } else {
2929 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002930 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002931 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002932 }
satokab751aa2010-09-14 19:17:36 +09002933 }
satok723a27e2010-11-11 14:58:11 +09002934
satok4c0e7152012-06-20 20:08:44 +09002935 // Workaround.
2936 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2937 // IMEs are not recognized and considered uninstalled.
2938 // Actually, we can't move everything after SystemReady because
2939 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2940 // the default IME here and try cheking the default IME again in systemReady().
2941 // TODO: Do nothing before system ready and implement a separated logic for
2942 // the encryption lock screen.
2943 // TODO: ASEC should be ready before IMMS is instantiated.
2944 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002945 // Set InputMethod here
2946 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2947 }
2948 }
2949
2950 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2951 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2952 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2953 // newDefaultIme is empty when there is no candidate for the selected IME.
2954 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2955 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2956 if (subtypeHashCode != null) {
2957 try {
2958 lastSubtypeId = getSubtypeIdFromHashCode(
2959 imi, Integer.valueOf(subtypeHashCode));
2960 } catch (NumberFormatException e) {
2961 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2962 }
2963 }
2964 }
2965 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002966 }
2967
2968 private int getSelectedInputMethodSubtypeId(String id) {
2969 InputMethodInfo imi = mMethodMap.get(id);
2970 if (imi == null) {
2971 return NOT_A_SUBTYPE_ID;
2972 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002973 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2974 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002975 }
2976
satokfdf419e2012-05-08 16:52:08 +09002977 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2978 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2979 }
2980
2981 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002982 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002983 final int subtypeCount = imi.getSubtypeCount();
2984 for (int i = 0; i < subtypeCount; ++i) {
2985 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002986 if (subtypeHashCode == ims.hashCode()) {
2987 return i;
2988 }
satokab751aa2010-09-14 19:17:36 +09002989 }
2990 }
2991 return NOT_A_SUBTYPE_ID;
2992 }
2993
satoka86f5e42011-09-02 17:12:42 +09002994 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2995 Resources res, InputMethodInfo imi) {
2996 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002997 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002998 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002999 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09003000 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09003001 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09003002 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09003003 // scan overriding implicitly enabled subtypes.
3004 InputMethodSubtype subtype = subtypes.get(i);
3005 if (subtype.overridesImplicitlyEnabledSubtype()) {
3006 final String mode = subtype.getMode();
3007 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
3008 applicableModeAndSubtypesMap.put(mode, subtype);
3009 }
3010 }
3011 }
3012 if (applicableModeAndSubtypesMap.size() > 0) {
3013 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
3014 }
3015 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09003016 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09003017 final String locale = subtype.getLocale();
3018 final String mode = subtype.getMode();
3019 // When system locale starts with subtype's locale, that subtype will be applicable
3020 // for system locale
3021 // For instance, it's clearly applicable for cases like system locale = en_US and
3022 // subtype = en, but it is not necessarily considered applicable for cases like system
3023 // locale = en and subtype = en_US.
3024 // We just call systemLocale.startsWith(locale) in this function because there is no
3025 // need to find applicable subtypes aggressively unlike
3026 // findLastResortApplicableSubtypeLocked.
3027 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003028 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003029 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003030 if (applicableSubtype != null) {
3031 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3032 if (!systemLocale.equals(locale)) continue;
3033 }
satok3da92232011-01-11 22:46:30 +09003034 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003035 }
3036 }
3037 final InputMethodSubtype keyboardSubtype
3038 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3039 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3040 applicableModeAndSubtypesMap.values());
3041 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3042 for (int i = 0; i < N; ++i) {
3043 final InputMethodSubtype subtype = subtypes.get(i);
3044 final String mode = subtype.getMode();
3045 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3046 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3047 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003048 }
3049 }
3050 }
satokc3690562012-01-10 20:14:43 +09003051 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003052 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003053 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003054 if (lastResortKeyboardSubtype != null) {
3055 applicableSubtypes.add(lastResortKeyboardSubtype);
3056 }
3057 }
3058 return applicableSubtypes;
3059 }
3060
satok4e4569d2010-11-19 18:45:53 +09003061 /**
3062 * If there are no selected subtypes, tries finding the most applicable one according to the
3063 * given locale.
3064 * @param subtypes this function will search the most applicable subtype in subtypes
3065 * @param mode subtypes will be filtered by mode
3066 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003067 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3068 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003069 * @return the most applicable subtypeId
3070 */
satokdf31ae62011-01-15 06:19:44 +09003071 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3072 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003073 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003074 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003075 return null;
satok8fbb1e82010-11-02 23:15:58 +09003076 }
satok4e4569d2010-11-19 18:45:53 +09003077 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003078 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003079 }
satok8fbb1e82010-11-02 23:15:58 +09003080 final String language = locale.substring(0, 2);
3081 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003082 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003083 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003084 final int N = subtypes.size();
3085 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003086 InputMethodSubtype subtype = subtypes.get(i);
3087 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003088 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3089 // and all subtypes with all modes can be candidates.
3090 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003091 if (firstMatchedModeSubtype == null) {
3092 firstMatchedModeSubtype = subtype;
3093 }
satok9ef02832010-11-04 21:17:48 +09003094 if (locale.equals(subtypeLocale)) {
3095 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003096 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003097 break;
3098 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3099 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003100 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003101 partialMatchFound = true;
3102 }
satok8fbb1e82010-11-02 23:15:58 +09003103 }
3104 }
3105
satok7599a7f2010-12-22 13:45:23 +09003106 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3107 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003108 }
3109
satok8fbb1e82010-11-02 23:15:58 +09003110 // The first subtype applicable to the system locale will be defined as the most applicable
3111 // subtype.
3112 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003113 if (applicableSubtype != null) {
3114 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3115 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3116 }
satok8fbb1e82010-11-02 23:15:58 +09003117 }
satokcd7cd292010-11-20 15:46:23 +09003118 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003119 }
3120
satok4e4569d2010-11-19 18:45:53 +09003121 // If there are no selected shortcuts, tries finding the most applicable ones.
3122 private Pair<InputMethodInfo, InputMethodSubtype>
3123 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3124 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3125 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003126 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003127 boolean foundInSystemIME = false;
3128
3129 // Search applicable subtype for each InputMethodInfo
3130 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003131 final String imiId = imi.getId();
3132 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3133 continue;
3134 }
satokcd7cd292010-11-20 15:46:23 +09003135 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003136 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003137 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003138 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003139 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003140 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003141 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003142 }
satokdf31ae62011-01-15 06:19:44 +09003143 // 2. Search by the system locale from enabledSubtypes.
3144 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003145 if (subtype == null) {
3146 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003147 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003148 }
satoka86f5e42011-09-02 17:12:42 +09003149 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3150 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3151 final ArrayList<InputMethodSubtype> subtypesForSearch =
3152 overridingImplicitlyEnabledSubtypes.isEmpty()
3153 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003154 // 4. Search by the current subtype's locale from all subtypes.
3155 if (subtype == null && mCurrentSubtype != null) {
3156 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003157 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003158 }
3159 // 5. Search by the system locale from all subtypes.
3160 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003161 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003162 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003163 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003164 }
satokcd7cd292010-11-20 15:46:23 +09003165 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003166 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003167 // The current input method is the most applicable IME.
3168 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003169 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003170 break;
satok7599a7f2010-12-22 13:45:23 +09003171 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003172 // The system input method is 2nd applicable IME.
3173 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003174 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003175 if ((imi.getServiceInfo().applicationInfo.flags
3176 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3177 foundInSystemIME = true;
3178 }
satok4e4569d2010-11-19 18:45:53 +09003179 }
3180 }
3181 }
3182 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003183 if (mostApplicableIMI != null) {
3184 Slog.w(TAG, "Most applicable shortcut input method was:"
3185 + mostApplicableIMI.getId());
3186 if (mostApplicableSubtype != null) {
3187 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3188 + "," + mostApplicableSubtype.getMode() + ","
3189 + mostApplicableSubtype.getLocale());
3190 }
3191 }
satok4e4569d2010-11-19 18:45:53 +09003192 }
satokcd7cd292010-11-20 15:46:23 +09003193 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003194 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003195 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003196 } else {
3197 return null;
3198 }
3199 }
3200
satokab751aa2010-09-14 19:17:36 +09003201 /**
3202 * @return Return the current subtype of this input method.
3203 */
satok42c5a162011-05-26 16:46:14 +09003204 @Override
satokab751aa2010-09-14 19:17:36 +09003205 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003206 // TODO: Make this work even for non-current users?
3207 if (!calledFromValidUser()) {
3208 return null;
3209 }
3210 synchronized (mMethodMap) {
3211 return getCurrentInputMethodSubtypeLocked();
3212 }
3213 }
3214
3215 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003216 if (mCurMethodId == null) {
3217 return null;
3218 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003219 final boolean subtypeIsSelected =
3220 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3221 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3222 if (imi == null || imi.getSubtypeCount() == 0) {
3223 return null;
satok4e4569d2010-11-19 18:45:53 +09003224 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003225 if (!subtypeIsSelected || mCurrentSubtype == null
3226 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3227 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3228 if (subtypeId == NOT_A_SUBTYPE_ID) {
3229 // If there are no selected subtypes, the framework will try to find
3230 // the most applicable subtype from explicitly or implicitly enabled
3231 // subtypes.
3232 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3233 getEnabledInputMethodSubtypeListLocked(imi, true);
3234 // If there is only one explicitly or implicitly enabled subtype,
3235 // just returns it.
3236 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3237 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3238 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3239 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3240 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3241 SUBTYPE_MODE_KEYBOARD, null, true);
3242 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003243 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003244 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3245 true);
satok4e4569d2010-11-19 18:45:53 +09003246 }
satok3ef8b292010-11-23 06:06:29 +09003247 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003248 } else {
3249 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003250 }
3251 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003252 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003253 }
3254
satokf3db1af2010-11-23 13:34:33 +09003255 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3256 InputMethodSubtype subtype) {
3257 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3258 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3259 } else {
3260 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3261 subtypes.add(subtype);
3262 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3263 }
3264 }
3265
satok4e4569d2010-11-19 18:45:53 +09003266 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003267 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003268 @Override
satok4e4569d2010-11-19 18:45:53 +09003269 public List getShortcutInputMethodsAndSubtypes() {
3270 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003271 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003272 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003273 // If there are no selected shortcut subtypes, the framework will try to find
3274 // the most applicable subtype from all subtypes whose mode is
3275 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003276 Pair<InputMethodInfo, InputMethodSubtype> info =
3277 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3278 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003279 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003280 ret.add(info.first);
3281 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003282 }
satok3da92232011-01-11 22:46:30 +09003283 return ret;
satokf3db1af2010-11-23 13:34:33 +09003284 }
satokf3db1af2010-11-23 13:34:33 +09003285 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3286 ret.add(imi);
3287 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3288 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003289 }
3290 }
satokf3db1af2010-11-23 13:34:33 +09003291 return ret;
satok4e4569d2010-11-19 18:45:53 +09003292 }
3293 }
3294
satok42c5a162011-05-26 16:46:14 +09003295 @Override
satokb66d2872010-11-10 01:04:04 +09003296 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003297 // TODO: Make this work even for non-current users?
3298 if (!calledFromValidUser()) {
3299 return false;
3300 }
satokb66d2872010-11-10 01:04:04 +09003301 synchronized (mMethodMap) {
3302 if (subtype != null && mCurMethodId != null) {
3303 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3304 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3305 if (subtypeId != NOT_A_SUBTYPE_ID) {
3306 setInputMethodLocked(mCurMethodId, subtypeId);
3307 return true;
3308 }
3309 }
3310 return false;
3311 }
3312 }
3313
satok688bd472012-02-09 20:09:17 +09003314 private static class InputMethodAndSubtypeListManager {
3315 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003316 // Used to load label
satok688bd472012-02-09 20:09:17 +09003317 private final PackageManager mPm;
3318 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003319 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003320 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3321 mContext = context;
3322 mPm = context.getPackageManager();
3323 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003324 final Locale locale = context.getResources().getConfiguration().locale;
3325 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003326 }
3327
3328 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3329 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3330 new Comparator<InputMethodInfo>() {
3331 @Override
3332 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3333 if (imi2 == null) return 0;
3334 if (imi1 == null) return 1;
3335 if (mPm == null) {
3336 return imi1.getId().compareTo(imi2.getId());
3337 }
3338 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3339 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3340 return imiId1.toString().compareTo(imiId2.toString());
3341 }
3342 });
3343
3344 public ImeSubtypeListItem getNextInputMethod(
3345 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3346 if (imi == null) {
3347 return null;
3348 }
3349 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3350 if (imList.size() <= 1) {
3351 return null;
3352 }
3353 final int N = imList.size();
3354 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003355 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003356 : NOT_A_SUBTYPE_ID;
3357 for (int i = 0; i < N; ++i) {
3358 final ImeSubtypeListItem isli = imList.get(i);
3359 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3360 if (!onlyCurrentIme) {
3361 return imList.get((i + 1) % N);
3362 }
3363 for (int j = 0; j < N - 1; ++j) {
3364 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3365 if (candidate.mImi.equals(imi)) {
3366 return candidate;
3367 }
3368 }
3369 return null;
3370 }
3371 }
3372 return null;
3373 }
3374
3375 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3376 return getSortedInputMethodAndSubtypeList(true, false, false);
3377 }
3378
3379 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3380 boolean inputShown, boolean isScreenLocked) {
3381 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3382 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3383 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3384 if (immis == null || immis.size() == 0) {
3385 return Collections.emptyList();
3386 }
3387 mSortedImmis.clear();
3388 mSortedImmis.putAll(immis);
3389 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3390 if (imi == null) continue;
3391 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3392 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3393 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3394 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3395 }
3396 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3397 final CharSequence imeLabel = imi.loadLabel(mPm);
3398 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3399 final int subtypeCount = imi.getSubtypeCount();
3400 if (DEBUG) {
3401 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3402 }
3403 for (int j = 0; j < subtypeCount; ++j) {
3404 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3405 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3406 // We show all enabled IMEs and subtypes when an IME is shown.
3407 if (enabledSubtypeSet.contains(subtypeHashCode)
3408 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3409 final CharSequence subtypeLabel =
3410 subtype.overridesImplicitlyEnabledSubtype() ? null
3411 : subtype.getDisplayName(mContext, imi.getPackageName(),
3412 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003413 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3414 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003415
3416 // Removing this subtype from enabledSubtypeSet because we no longer
3417 // need to add an entry of this subtype to imList to avoid duplicated
3418 // entries.
3419 enabledSubtypeSet.remove(subtypeHashCode);
3420 }
3421 }
3422 } else {
satok93d744d2012-05-09 17:14:08 +09003423 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3424 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003425 }
3426 }
satok93d744d2012-05-09 17:14:08 +09003427 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003428 return imList;
3429 }
3430 }
3431
satokd87c2592010-09-29 11:52:06 +09003432 /**
3433 * Utility class for putting and getting settings for InputMethod
3434 * TODO: Move all putters and getters of settings to this class.
3435 */
3436 private static class InputMethodSettings {
3437 // The string for enabled input method is saved as follows:
3438 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3439 private static final char INPUT_METHOD_SEPARATER = ':';
3440 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003441 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003442 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3443
satok723a27e2010-11-11 14:58:11 +09003444 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003445 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3446
satokdf31ae62011-01-15 06:19:44 +09003447 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003448 private final ContentResolver mResolver;
3449 private final HashMap<String, InputMethodInfo> mMethodMap;
3450 private final ArrayList<InputMethodInfo> mMethodList;
3451
3452 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003454
3455 private static void buildEnabledInputMethodsSettingString(
3456 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3457 String id = pair.first;
3458 ArrayList<String> subtypes = pair.second;
3459 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003460 // Inputmethod and subtypes are saved in the settings as follows:
3461 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3462 for (String subtypeId: subtypes) {
3463 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003464 }
3465 }
3466
3467 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003468 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003469 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3470 int userId) {
3471 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003472 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003473 mResolver = resolver;
3474 mMethodMap = methodMap;
3475 mMethodList = methodList;
3476 }
3477
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003478 public void setCurrentUserId(int userId) {
3479 if (DEBUG) {
3480 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3481 + userId + ", new ime = " + getSelectedInputMethod());
3482 }
3483 // IMMS settings are kept per user, so keep track of current user
3484 mCurrentUserId = userId;
3485 }
3486
satokd87c2592010-09-29 11:52:06 +09003487 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3488 return createEnabledInputMethodListLocked(
3489 getEnabledInputMethodsAndSubtypeListLocked());
3490 }
3491
satok7f35c8c2010-10-07 21:13:11 +09003492 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003493 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3494 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003495 getEnabledInputMethodsAndSubtypeListLocked());
3496 }
3497
satok67ddf9c2010-11-17 09:45:54 +09003498 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3499 InputMethodInfo imi) {
3500 List<Pair<String, ArrayList<String>>> imsList =
3501 getEnabledInputMethodsAndSubtypeListLocked();
3502 ArrayList<InputMethodSubtype> enabledSubtypes =
3503 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003504 if (imi != null) {
3505 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3506 InputMethodInfo info = mMethodMap.get(imsPair.first);
3507 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003508 final int subtypeCount = info.getSubtypeCount();
3509 for (int i = 0; i < subtypeCount; ++i) {
3510 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003511 for (String s: imsPair.second) {
3512 if (String.valueOf(ims.hashCode()).equals(s)) {
3513 enabledSubtypes.add(ims);
3514 }
satok67ddf9c2010-11-17 09:45:54 +09003515 }
3516 }
satok884ef9a2010-11-18 10:39:46 +09003517 break;
satok67ddf9c2010-11-17 09:45:54 +09003518 }
satok67ddf9c2010-11-17 09:45:54 +09003519 }
3520 }
3521 return enabledSubtypes;
3522 }
3523
satokd87c2592010-09-29 11:52:06 +09003524 // At the initial boot, the settings for input methods are not set,
3525 // so we need to enable IME in that case.
3526 public void enableAllIMEsIfThereIsNoEnabledIME() {
3527 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3528 StringBuilder sb = new StringBuilder();
3529 final int N = mMethodList.size();
3530 for (int i = 0; i < N; i++) {
3531 InputMethodInfo imi = mMethodList.get(i);
3532 Slog.i(TAG, "Adding: " + imi.getId());
3533 if (i > 0) sb.append(':');
3534 sb.append(imi.getId());
3535 }
3536 putEnabledInputMethodsStr(sb.toString());
3537 }
3538 }
3539
satokbb4aa062011-01-19 21:40:27 +09003540 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003541 ArrayList<Pair<String, ArrayList<String>>> imsList
3542 = new ArrayList<Pair<String, ArrayList<String>>>();
3543 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3544 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3545 return imsList;
3546 }
satok723a27e2010-11-11 14:58:11 +09003547 mInputMethodSplitter.setString(enabledInputMethodsStr);
3548 while (mInputMethodSplitter.hasNext()) {
3549 String nextImsStr = mInputMethodSplitter.next();
3550 mSubtypeSplitter.setString(nextImsStr);
3551 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003552 ArrayList<String> subtypeHashes = new ArrayList<String>();
3553 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003554 String imeId = mSubtypeSplitter.next();
3555 while (mSubtypeSplitter.hasNext()) {
3556 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003557 }
3558 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3559 }
3560 }
3561 return imsList;
3562 }
3563
3564 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3565 if (reloadInputMethodStr) {
3566 getEnabledInputMethodsStr();
3567 }
3568 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3569 // Add in the newly enabled input method.
3570 putEnabledInputMethodsStr(id);
3571 } else {
3572 putEnabledInputMethodsStr(
3573 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3574 }
3575 }
3576
3577 /**
3578 * Build and put a string of EnabledInputMethods with removing specified Id.
3579 * @return the specified id was removed or not.
3580 */
3581 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3582 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3583 boolean isRemoved = false;
3584 boolean needsAppendSeparator = false;
3585 for (Pair<String, ArrayList<String>> ims: imsList) {
3586 String curId = ims.first;
3587 if (curId.equals(id)) {
3588 // We are disabling this input method, and it is
3589 // currently enabled. Skip it to remove from the
3590 // new list.
3591 isRemoved = true;
3592 } else {
3593 if (needsAppendSeparator) {
3594 builder.append(INPUT_METHOD_SEPARATER);
3595 } else {
3596 needsAppendSeparator = true;
3597 }
3598 buildEnabledInputMethodsSettingString(builder, ims);
3599 }
3600 }
3601 if (isRemoved) {
3602 // Update the setting with the new list of input methods.
3603 putEnabledInputMethodsStr(builder.toString());
3604 }
3605 return isRemoved;
3606 }
3607
3608 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3609 List<Pair<String, ArrayList<String>>> imsList) {
3610 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3611 for (Pair<String, ArrayList<String>> ims: imsList) {
3612 InputMethodInfo info = mMethodMap.get(ims.first);
3613 if (info != null) {
3614 res.add(info);
3615 }
3616 }
3617 return res;
3618 }
3619
satok7f35c8c2010-10-07 21:13:11 +09003620 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003621 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003622 List<Pair<String, ArrayList<String>>> imsList) {
3623 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3624 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3625 for (Pair<String, ArrayList<String>> ims : imsList) {
3626 InputMethodInfo info = mMethodMap.get(ims.first);
3627 if (info != null) {
3628 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3629 }
3630 }
3631 return res;
3632 }
3633
satokd87c2592010-09-29 11:52:06 +09003634 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003635 Settings.Secure.putStringForUser(
3636 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003637 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003638 if (DEBUG) {
3639 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3640 }
satokd87c2592010-09-29 11:52:06 +09003641 }
3642
3643 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003644 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3645 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003646 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003647 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3648 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003649 }
satokd87c2592010-09-29 11:52:06 +09003650 return mEnabledInputMethodsStrCache;
3651 }
satok723a27e2010-11-11 14:58:11 +09003652
3653 private void saveSubtypeHistory(
3654 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3655 StringBuilder builder = new StringBuilder();
3656 boolean isImeAdded = false;
3657 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3658 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3659 newSubtypeId);
3660 isImeAdded = true;
3661 }
3662 for (Pair<String, String> ime: savedImes) {
3663 String imeId = ime.first;
3664 String subtypeId = ime.second;
3665 if (TextUtils.isEmpty(subtypeId)) {
3666 subtypeId = NOT_A_SUBTYPE_ID_STR;
3667 }
3668 if (isImeAdded) {
3669 builder.append(INPUT_METHOD_SEPARATER);
3670 } else {
3671 isImeAdded = true;
3672 }
3673 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3674 subtypeId);
3675 }
3676 // Remove the last INPUT_METHOD_SEPARATER
3677 putSubtypeHistoryStr(builder.toString());
3678 }
3679
3680 public void addSubtypeToHistory(String imeId, String subtypeId) {
3681 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3682 for (Pair<String, String> ime: subtypeHistory) {
3683 if (ime.first.equals(imeId)) {
3684 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003685 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003686 + ime.second);
3687 }
3688 // We should break here
3689 subtypeHistory.remove(ime);
3690 break;
3691 }
3692 }
satokbb4aa062011-01-19 21:40:27 +09003693 if (DEBUG) {
3694 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3695 }
satok723a27e2010-11-11 14:58:11 +09003696 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3697 }
3698
3699 private void putSubtypeHistoryStr(String str) {
3700 if (DEBUG) {
3701 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3702 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003703 Settings.Secure.putStringForUser(
3704 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003705 }
3706
3707 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3708 // Gets the first one from the history
3709 return getLastSubtypeForInputMethodLockedInternal(null);
3710 }
3711
3712 public String getLastSubtypeForInputMethodLocked(String imeId) {
3713 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3714 if (ime != null) {
3715 return ime.second;
3716 } else {
3717 return null;
3718 }
3719 }
3720
3721 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3722 List<Pair<String, ArrayList<String>>> enabledImes =
3723 getEnabledInputMethodsAndSubtypeListLocked();
3724 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003725 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003726 final String imeInTheHistory = imeAndSubtype.first;
3727 // If imeId is empty, returns the first IME and subtype in the history
3728 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3729 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003730 final String subtypeHashCode =
3731 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3732 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003733 if (!TextUtils.isEmpty(subtypeHashCode)) {
3734 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003735 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003736 }
3737 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3738 }
3739 }
3740 }
3741 if (DEBUG) {
3742 Slog.d(TAG, "No enabled IME found in the history");
3743 }
3744 return null;
3745 }
3746
satokdf31ae62011-01-15 06:19:44 +09003747 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003748 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3749 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3750 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003751 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003752 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003753 if (explicitlyEnabledSubtypes.size() == 0) {
3754 // If there are no explicitly enabled subtypes, applicable subtypes are
3755 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003756 // If IME is enabled and no subtypes are enabled, applicable subtypes
3757 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003758 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003759 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003760 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003761 if (implicitlySelectedSubtypes != null) {
3762 final int N = implicitlySelectedSubtypes.size();
3763 for (int i = 0; i < N; ++i) {
3764 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3765 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3766 return subtypeHashCode;
3767 }
3768 }
3769 }
3770 }
3771 } else {
satokf6cafb62011-01-17 16:29:02 +09003772 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003773 if (s.equals(subtypeHashCode)) {
3774 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003775 try {
3776 final int hashCode = Integer.valueOf(subtypeHashCode);
3777 // Check whether the subtype id is valid or not
3778 if (isValidSubtypeId(imi, hashCode)) {
3779 return s;
3780 } else {
3781 return NOT_A_SUBTYPE_ID_STR;
3782 }
3783 } catch (NumberFormatException e) {
3784 return NOT_A_SUBTYPE_ID_STR;
3785 }
satokdf31ae62011-01-15 06:19:44 +09003786 }
satok723a27e2010-11-11 14:58:11 +09003787 }
3788 }
3789 // If imeId was enabled but subtypeId was disabled.
3790 return NOT_A_SUBTYPE_ID_STR;
3791 }
3792 }
3793 // If both imeId and subtypeId are disabled, return null
3794 return null;
3795 }
3796
3797 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3798 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3799 final String subtypeHistoryStr = getSubtypeHistoryStr();
3800 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3801 return imsList;
3802 }
3803 mInputMethodSplitter.setString(subtypeHistoryStr);
3804 while (mInputMethodSplitter.hasNext()) {
3805 String nextImsStr = mInputMethodSplitter.next();
3806 mSubtypeSplitter.setString(nextImsStr);
3807 if (mSubtypeSplitter.hasNext()) {
3808 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3809 // The first element is ime id.
3810 String imeId = mSubtypeSplitter.next();
3811 while (mSubtypeSplitter.hasNext()) {
3812 subtypeId = mSubtypeSplitter.next();
3813 break;
3814 }
3815 imsList.add(new Pair<String, String>(imeId, subtypeId));
3816 }
3817 }
3818 return imsList;
3819 }
3820
3821 private String getSubtypeHistoryStr() {
3822 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003823 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3824 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003825 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003826 return Settings.Secure.getStringForUser(
3827 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003828 }
3829
3830 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003831 if (DEBUG) {
3832 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3833 + mCurrentUserId);
3834 }
3835 Settings.Secure.putStringForUser(
3836 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003837 }
3838
3839 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003840 if (DEBUG) {
3841 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3842 + mCurrentUserId);
3843 }
3844 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3845 subtypeId, mCurrentUserId);
3846 }
3847
3848 public String getDisabledSystemInputMethods() {
3849 return Settings.Secure.getStringForUser(
3850 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3851 }
3852
3853 public String getSelectedInputMethod() {
3854 if (DEBUG) {
3855 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3856 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3857 + ", " + mCurrentUserId);
3858 }
3859 return Settings.Secure.getStringForUser(
3860 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3861 }
3862
3863 public int getSelectedInputMethodSubtypeHashCode() {
3864 try {
3865 return Settings.Secure.getIntForUser(
3866 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3867 } catch (SettingNotFoundException e) {
3868 return NOT_A_SUBTYPE_ID;
3869 }
3870 }
3871
3872 public int getCurrentUserId() {
3873 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003874 }
satokd87c2592010-09-29 11:52:06 +09003875 }
3876
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003877 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003878 private static class InputMethodFileManager {
3879 private static final String SYSTEM_PATH = "system";
3880 private static final String INPUT_METHOD_PATH = "inputmethod";
3881 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3882 private static final String NODE_SUBTYPES = "subtypes";
3883 private static final String NODE_SUBTYPE = "subtype";
3884 private static final String NODE_IMI = "imi";
3885 private static final String ATTR_ID = "id";
3886 private static final String ATTR_LABEL = "label";
3887 private static final String ATTR_ICON = "icon";
3888 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3889 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3890 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3891 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3892 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3893 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003894 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003895 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003896 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003897 if (methodMap == null) {
3898 throw new NullPointerException("methodMap is null");
3899 }
3900 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003901 final File systemDir = userId == UserHandle.USER_OWNER
3902 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3903 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003904 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3905 if (!inputMethodDir.mkdirs()) {
3906 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3907 }
3908 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3909 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3910 if (!subtypeFile.exists()) {
3911 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003912 writeAdditionalInputMethodSubtypes(
3913 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003914 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003915 readAdditionalInputMethodSubtypes(
3916 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003917 }
3918 }
3919
3920 private void deleteAllInputMethodSubtypes(String imiId) {
3921 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003922 mAdditionalSubtypesMap.remove(imiId);
3923 writeAdditionalInputMethodSubtypes(
3924 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003925 }
3926 }
3927
3928 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003929 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003930 synchronized (mMethodMap) {
3931 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3932 final int N = additionalSubtypes.length;
3933 for (int i = 0; i < N; ++i) {
3934 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003935 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003936 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003937 } else {
3938 Slog.w(TAG, "Duplicated subtype definition found: "
3939 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003940 }
3941 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003942 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3943 writeAdditionalInputMethodSubtypes(
3944 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003945 }
3946 }
3947
3948 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3949 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003950 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003951 }
3952 }
3953
3954 private static void writeAdditionalInputMethodSubtypes(
3955 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3956 HashMap<String, InputMethodInfo> methodMap) {
3957 // Safety net for the case that this function is called before methodMap is set.
3958 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3959 FileOutputStream fos = null;
3960 try {
3961 fos = subtypesFile.startWrite();
3962 final XmlSerializer out = new FastXmlSerializer();
3963 out.setOutput(fos, "utf-8");
3964 out.startDocument(null, true);
3965 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3966 out.startTag(null, NODE_SUBTYPES);
3967 for (String imiId : allSubtypes.keySet()) {
3968 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3969 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3970 continue;
3971 }
3972 out.startTag(null, NODE_IMI);
3973 out.attribute(null, ATTR_ID, imiId);
3974 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3975 final int N = subtypesList.size();
3976 for (int i = 0; i < N; ++i) {
3977 final InputMethodSubtype subtype = subtypesList.get(i);
3978 out.startTag(null, NODE_SUBTYPE);
3979 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3980 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3981 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3982 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3983 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3984 out.attribute(null, ATTR_IS_AUXILIARY,
3985 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3986 out.endTag(null, NODE_SUBTYPE);
3987 }
3988 out.endTag(null, NODE_IMI);
3989 }
3990 out.endTag(null, NODE_SUBTYPES);
3991 out.endDocument();
3992 subtypesFile.finishWrite(fos);
3993 } catch (java.io.IOException e) {
3994 Slog.w(TAG, "Error writing subtypes", e);
3995 if (fos != null) {
3996 subtypesFile.failWrite(fos);
3997 }
3998 }
3999 }
4000
4001 private static void readAdditionalInputMethodSubtypes(
4002 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
4003 if (allSubtypes == null || subtypesFile == null) return;
4004 allSubtypes.clear();
4005 FileInputStream fis = null;
4006 try {
4007 fis = subtypesFile.openRead();
4008 final XmlPullParser parser = Xml.newPullParser();
4009 parser.setInput(fis, null);
4010 int type = parser.getEventType();
4011 // Skip parsing until START_TAG
4012 while ((type = parser.next()) != XmlPullParser.START_TAG
4013 && type != XmlPullParser.END_DOCUMENT) {}
4014 String firstNodeName = parser.getName();
4015 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4016 throw new XmlPullParserException("Xml doesn't start with subtypes");
4017 }
4018 final int depth =parser.getDepth();
4019 String currentImiId = null;
4020 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4021 while (((type = parser.next()) != XmlPullParser.END_TAG
4022 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4023 if (type != XmlPullParser.START_TAG)
4024 continue;
4025 final String nodeName = parser.getName();
4026 if (NODE_IMI.equals(nodeName)) {
4027 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4028 if (TextUtils.isEmpty(currentImiId)) {
4029 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4030 continue;
4031 }
4032 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4033 allSubtypes.put(currentImiId, tempSubtypesArray);
4034 } else if (NODE_SUBTYPE.equals(nodeName)) {
4035 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4036 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4037 continue;
4038 }
4039 final int icon = Integer.valueOf(
4040 parser.getAttributeValue(null, ATTR_ICON));
4041 final int label = Integer.valueOf(
4042 parser.getAttributeValue(null, ATTR_LABEL));
4043 final String imeSubtypeLocale =
4044 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4045 final String imeSubtypeMode =
4046 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4047 final String imeSubtypeExtraValue =
4048 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004049 final boolean isAuxiliary = "1".equals(String.valueOf(
4050 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004051 final InputMethodSubtype subtype =
4052 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4053 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4054 tempSubtypesArray.add(subtype);
4055 }
4056 }
4057 } catch (XmlPullParserException e) {
4058 Slog.w(TAG, "Error reading subtypes: " + e);
4059 return;
4060 } catch (java.io.IOException e) {
4061 Slog.w(TAG, "Error reading subtypes: " + e);
4062 return;
4063 } catch (NumberFormatException e) {
4064 Slog.w(TAG, "Error reading subtypes: " + e);
4065 return;
4066 } finally {
4067 if (fis != null) {
4068 try {
4069 fis.close();
4070 } catch (java.io.IOException e1) {
4071 Slog.w(TAG, "Failed to close.");
4072 }
4073 }
4074 }
4075 }
4076 }
4077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004079 // Utilities for debug
4080 private static String getStackTrace() {
4081 final StringBuilder sb = new StringBuilder();
4082 try {
4083 throw new RuntimeException();
4084 } catch (RuntimeException e) {
4085 final StackTraceElement[] frames = e.getStackTrace();
4086 // Start at 1 because the first frame is here and we don't care about it
4087 for (int j = 1; j < frames.length; ++j) {
4088 sb.append(frames[j].toString() + "\n");
4089 }
4090 }
4091 return sb.toString();
4092 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 @Override
4095 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4096 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4097 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4100 + Binder.getCallingPid()
4101 + ", uid=" + Binder.getCallingUid());
4102 return;
4103 }
4104
4105 IInputMethod method;
4106 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 synchronized (mMethodMap) {
4111 p.println("Current Input Method Manager state:");
4112 int N = mMethodList.size();
4113 p.println(" Input Methods:");
4114 for (int i=0; i<N; i++) {
4115 InputMethodInfo info = mMethodList.get(i);
4116 p.println(" InputMethod #" + i + ":");
4117 info.dump(p, " ");
4118 }
4119 p.println(" Clients:");
4120 for (ClientState ci : mClients.values()) {
4121 p.println(" Client " + ci + ":");
4122 p.println(" client=" + ci.client);
4123 p.println(" inputContext=" + ci.inputContext);
4124 p.println(" sessionRequested=" + ci.sessionRequested);
4125 p.println(" curSession=" + ci.curSession);
4126 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004127 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004129 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4130 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4132 + " mBoundToMethod=" + mBoundToMethod);
4133 p.println(" mCurToken=" + mCurToken);
4134 p.println(" mCurIntent=" + mCurIntent);
4135 method = mCurMethod;
4136 p.println(" mCurMethod=" + mCurMethod);
4137 p.println(" mEnabledSession=" + mEnabledSession);
4138 p.println(" mShowRequested=" + mShowRequested
4139 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4140 + " mShowForced=" + mShowForced
4141 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004142 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004144
Jeff Brownb88102f2010-09-08 11:49:43 -07004145 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 pw.flush();
4148 try {
4149 client.client.asBinder().dump(fd, args);
4150 } catch (RemoteException e) {
4151 p.println("Input method client dead: " + e);
4152 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004153 } else {
4154 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004156
Jeff Brownb88102f2010-09-08 11:49:43 -07004157 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 pw.flush();
4160 try {
4161 method.asBinder().dump(fd, args);
4162 } catch (RemoteException e) {
4163 p.println("Input method service dead: " + e);
4164 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004165 } else {
4166 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
4168 }
4169}