blob: 8eb532d2de1f6785578d346b999f648cae6b3612 [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) {
447 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
448 }
449 return retval;
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900454 if (!isChangingPackagesOfCurrentUser()) {
455 return false;
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 final int N = mMethodList.size();
460 if (curInputMethodId != null) {
461 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800462 InputMethodInfo imi = mMethodList.get(i);
463 if (imi.getId().equals(curInputMethodId)) {
464 for (String pkg : packages) {
465 if (imi.getPackageName().equals(pkg)) {
466 if (!doit) {
467 return true;
468 }
satok723a27e2010-11-11 14:58:11 +0900469 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 chooseNewDefaultIMELocked();
471 return true;
472 }
473 }
474 }
475 }
476 }
477 }
478 return false;
479 }
480
481 @Override
482 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900483 if (!isChangingPackagesOfCurrentUser()) {
484 return;
485 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 synchronized (mMethodMap) {
487 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900488 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 final int N = mMethodList.size();
490 if (curInputMethodId != null) {
491 for (int i=0; i<N; i++) {
492 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900493 final String imiId = imi.getId();
494 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800495 curIm = imi;
496 }
satoke7c6998e2011-06-03 17:57:59 +0900497
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900499 if (isPackageModified(imi.getPackageName())) {
500 mFileManager.deleteAllInputMethodSubtypes(imiId);
501 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 if (change == PACKAGE_TEMPORARY_CHANGE
503 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800504 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 + imi.getComponent());
506 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 }
509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800510
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 buildInputMethodListLocked(mMethodList, mMethodMap);
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800515 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516 int change = isPackageDisappearing(curIm.getPackageName());
517 if (change == PACKAGE_TEMPORARY_CHANGE
518 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 ServiceInfo si = null;
520 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900521 si = mIPackageManager.getServiceInfo(
522 curIm.getComponent(), 0, mSettings.getCurrentUserId());
523 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 }
525 if (si == null) {
526 // Uh oh, current input method is no longer around!
527 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900529 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800531 changed = true;
532 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800533 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900534 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 }
536 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800538 }
satokab751aa2010-09-14 19:17:36 +0900539
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 if (curIm == null) {
541 // We currently don't have a default input method... is
542 // one now available?
543 changed = chooseNewDefaultIMELocked();
544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800546 if (changed) {
547 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 }
550 }
551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800552
Jean Chalarde0d32a62011-10-20 20:36:07 +0900553 private static class MethodCallback extends IInputMethodCallback.Stub {
554 private final IInputMethod mMethod;
555 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Jean Chalarde0d32a62011-10-20 20:36:07 +0900557 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900559 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
satoke7c6998e2011-06-03 17:57:59 +0900562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 public void finishedEvent(int seq, boolean handled) throws RemoteException {
564 }
565
satoke7c6998e2011-06-03 17:57:59 +0900566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900568 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
satok01038492012-04-09 21:08:27 +0900572 private class HardKeyboardListener
573 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
574 @Override
575 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
576 mHandler.sendMessage(mHandler.obtainMessage(
577 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
578 }
579
580 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
581 if (DEBUG) {
582 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
583 + enabled);
584 }
585 synchronized(mMethodMap) {
586 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
587 && mSwitchingDialog.isShowing()) {
588 mSwitchingDialogTitleView.findViewById(
589 com.android.internal.R.id.hard_keyboard_section).setVisibility(
590 available ? View.VISIBLE : View.GONE);
591 }
592 }
593 }
594 }
595
596 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900597 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800599 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mHandler = new Handler(this);
601 mIWindowManager = IWindowManager.Stub.asInterface(
602 ServiceManager.getService(Context.WINDOW_SERVICE));
603 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900604 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public void executeMessage(Message msg) {
606 handleMessage(msg);
607 }
608 });
satok01038492012-04-09 21:08:27 +0900609 mWindowManagerService = windowManager;
610 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900611
satok7cfc0ed2011-06-20 21:29:36 +0900612 mImeSwitcherNotification = new Notification();
613 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
614 mImeSwitcherNotification.when = 0;
615 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
616 mImeSwitcherNotification.tickerText = null;
617 mImeSwitcherNotification.defaults = 0; // please be quiet
618 mImeSwitcherNotification.sound = null;
619 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400620
621 // Tag this notification specially so SystemUI knows it's important
622 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
623
satok7cfc0ed2011-06-20 21:29:36 +0900624 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900625 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900626
627 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900628
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900629 final IntentFilter broadcastFilter = new IntentFilter();
630 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
631 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
632 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
633 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
satok7cfc0ed2011-06-20 21:29:36 +0900635 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900636 int userId = 0;
637 try {
638 ActivityManagerNative.getDefault().registerUserSwitchObserver(
639 new IUserSwitchObserver.Stub() {
640 @Override
641 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900642 synchronized(mMethodMap) {
643 switchUserLocked(newUserId);
644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 if (reply != null) {
646 try {
647 reply.sendResult(null);
648 } catch (RemoteException e) {
649 }
650 }
651 }
652
653 @Override
654 public void onUserSwitchComplete(int newUserId) throws RemoteException {
655 }
656 });
657 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
658 } catch (RemoteException e) {
659 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
660 }
661 mMyPackageMonitor.register(mContext, null, true);
satok913a8922010-08-26 21:53:41 +0900662
satokd87c2592010-09-29 11:52:06 +0900663 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900664 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900666 mFileManager = new InputMethodFileManager(mMethodMap, userId);
667 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900668
669 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900670 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900671 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900674 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
satok0a1bcf42012-05-16 19:26:31 +0900676 if (!mImeSelectedOnBoot) {
677 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900678 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 mSettingsObserver = new SettingsObserver(mHandler);
682 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900683
684 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
685 // according to the new system locale.
686 final IntentFilter filter = new IntentFilter();
687 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
688 mContext.registerReceiver(
689 new BroadcastReceiver() {
690 @Override
691 public void onReceive(Context context, Intent intent) {
692 synchronized(mMethodMap) {
693 checkCurrentLocaleChangedLocked();
694 }
695 }
696 }, filter);
697 }
698
satok5b927c432012-05-01 20:09:34 +0900699 private void resetDefaultImeLocked(Context context) {
700 // Do not reset the default (current) IME when it is a 3rd-party IME
701 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
702 return;
703 }
704
705 InputMethodInfo defIm = null;
706 for (InputMethodInfo imi : mMethodList) {
707 if (defIm == null) {
708 if (isValidSystemDefaultIme(imi, context)) {
709 defIm = imi;
710 Slog.i(TAG, "Selected default: " + imi.getId());
711 }
712 }
713 }
714 if (defIm == null && mMethodList.size() > 0) {
715 defIm = getMostApplicableDefaultIMELocked();
716 Slog.i(TAG, "No default found, using " + defIm.getId());
717 }
718 if (defIm != null) {
719 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
720 }
721 }
722
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900723 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
724 if (!mSystemReady) {
725 // not system ready
726 return;
727 }
728 final Locale newLocale = mRes.getConfiguration().locale;
729 if (!updateOnlyWhenLocaleChanged
730 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
731 if (!updateOnlyWhenLocaleChanged) {
732 hideCurrentInputLocked(0, null);
733 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700734 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900735 }
736 if (DEBUG) {
737 Slog.i(TAG, "Locale has been changed to " + newLocale);
738 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900739 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
740 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900741 buildInputMethodListLocked(mMethodList, mMethodMap);
742 if (!updateOnlyWhenLocaleChanged) {
743 final String selectedImiId = mSettings.getSelectedInputMethod();
744 if (TextUtils.isEmpty(selectedImiId)) {
745 // This is the first time of the user switch and
746 // set the current ime to the proper one.
747 resetDefaultImeLocked(mContext);
748 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900749 } else {
750 // If the locale is changed, needs to reset the default ime
751 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900752 }
753 updateFromSettingsLocked();
754 mLastSystemLocale = newLocale;
755 if (!updateOnlyWhenLocaleChanged) {
756 try {
757 startInputInnerLocked();
758 } catch (RuntimeException e) {
759 Slog.w(TAG, "Unexpected exception", e);
760 }
761 }
762 }
763 }
764
765 private void checkCurrentLocaleChangedLocked() {
766 resetAllInternalStateLocked(true);
767 }
768
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900769 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900770 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900771 // InputMethodFileManager should be reset when the user is changed
772 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 resetAllInternalStateLocked(false);
774 }
775
satok0a1bcf42012-05-16 19:26:31 +0900776 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
777 if (!mSystemReady) {
778 return false;
779 }
satok5b927c432012-05-01 20:09:34 +0900780 if (!isSystemIme(imi)) {
781 return false;
782 }
783 if (imi.getIsDefaultResourceId() != 0) {
784 try {
785 Resources res = context.createPackageContext(
786 imi.getPackageName(), 0).getResources();
787 if (res.getBoolean(imi.getIsDefaultResourceId())
788 && containsSubtypeOf(imi, context.getResources().getConfiguration().
789 locale.getLanguage())) {
790 return true;
791 }
792 } catch (PackageManager.NameNotFoundException ex) {
793 } catch (Resources.NotFoundException ex) {
794 }
795 }
796 if (imi.getSubtypeCount() == 0) {
797 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
798 }
799 return false;
800 }
801
802 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
803 if (!isSystemIme(imi)) {
804 return false;
805 }
806 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
807 }
808
809 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
810 final int N = imi.getSubtypeCount();
811 for (int i = 0; i < N; ++i) {
812 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
813 return true;
814 }
815 }
816 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
819 @Override
820 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
821 throws RemoteException {
822 try {
823 return super.onTransact(code, data, reply, flags);
824 } catch (RuntimeException e) {
825 // The input method manager only throws security exceptions, so let's
826 // log all others.
827 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830 throw e;
831 }
832 }
833
Dianne Hackborn661cd522011-08-22 00:26:20 -0700834 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700835 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 if (DEBUG) {
837 Slog.d(TAG, "--- systemReady");
838 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700839 if (!mSystemReady) {
840 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900841 mKeyguardManager =
842 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700843 mNotificationManager = (NotificationManager)
844 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
845 mStatusBar = statusBar;
846 statusBar.setIconVisibility("ime", false);
847 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900848 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
849 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900850 if (mShowOngoingImeSwitcherForPhones) {
851 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
852 mHardKeyboardListener);
853 }
satok0a1bcf42012-05-16 19:26:31 +0900854 buildInputMethodListLocked(mMethodList, mMethodMap);
855 if (!mImeSelectedOnBoot) {
856 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
857 checkCurrentLocaleChangedLocked();
858 }
859 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700860 try {
861 startInputInnerLocked();
862 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800863 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 }
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
satok15452a42011-10-28 17:58:28 +0900869 private void setImeWindowVisibilityStatusHiddenLocked() {
870 mImeWindowVis = 0;
871 updateImeWindowStatusLocked();
872 }
873
satok3afd6c02011-11-18 08:38:19 +0900874 private void refreshImeWindowVisibilityLocked() {
875 final Configuration conf = mRes.getConfiguration();
876 final boolean haveHardKeyboard = conf.keyboard
877 != Configuration.KEYBOARD_NOKEYS;
878 final boolean hardKeyShown = haveHardKeyboard
879 && conf.hardKeyboardHidden
880 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500881 final boolean isScreenLocked =
882 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
883 final boolean isScreenSecurelyLocked =
884 isScreenLocked && mKeyguardManager.isKeyguardSecure();
885 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
886 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900887 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
888 updateImeWindowStatusLocked();
889 }
890
satok15452a42011-10-28 17:58:28 +0900891 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900892 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700893 }
894
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900895 // ---------------------------------------------------------------------------------------
896 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
897 // 1) it comes from the system process
898 // 2) the calling process' user id is identical to the current user id IMMS thinks.
899 private boolean calledFromValidUser() {
900 final int uid = Binder.getCallingUid();
901 final int userId = UserHandle.getUserId(uid);
902 if (DEBUG) {
903 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
904 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
905 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900906 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
908 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
909 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900910 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900911
912 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
913 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
914 // must not manage background users' states in any functions.
915 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
916 // by a token.
917 if (mContext.checkCallingOrSelfPermission(
918 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
919 == PackageManager.PERMISSION_GRANTED) {
920 if (DEBUG) {
921 Slog.d(TAG, "--- Access granted because the calling process has "
922 + "the INTERACT_ACROSS_USERS_FULL permission");
923 }
924 return true;
925 }
926 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
927 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 }
929
930 private boolean bindCurrentInputMethodService(
931 Intent service, ServiceConnection conn, int flags) {
932 if (service == null || conn == null) {
933 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
934 return false;
935 }
936 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
937 }
938
satoke7c6998e2011-06-03 17:57:59 +0900939 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900941 // TODO: Make this work even for non-current users?
942 if (!calledFromValidUser()) {
943 return Collections.emptyList();
944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 synchronized (mMethodMap) {
946 return new ArrayList<InputMethodInfo>(mMethodList);
947 }
948 }
949
satoke7c6998e2011-06-03 17:57:59 +0900950 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900952 // TODO: Make this work even for non-current users?
953 if (!calledFromValidUser()) {
954 return Collections.emptyList();
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900957 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959 }
960
satokbb4aa062011-01-19 21:40:27 +0900961 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
962 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
963 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
964 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900966 enabledInputMethodAndSubtypes.put(
967 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
968 }
969 return enabledInputMethodAndSubtypes;
970 }
971
972 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
973 boolean allowsImplicitlySelectedSubtypes) {
974 if (imi == null && mCurMethodId != null) {
975 imi = mMethodMap.get(mCurMethodId);
976 }
satok7265d9b2011-02-14 15:47:30 +0900977 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900978 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900979 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900980 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900981 }
satok7265d9b2011-02-14 15:47:30 +0900982 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900983 }
984
satoke7c6998e2011-06-03 17:57:59 +0900985 @Override
satok16331c82010-12-20 23:48:46 +0900986 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
987 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 // TODO: Make this work even for non-current users?
989 if (!calledFromValidUser()) {
990 return Collections.emptyList();
991 }
satok67ddf9c2010-11-17 09:45:54 +0900992 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900993 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900994 }
995 }
996
satoke7c6998e2011-06-03 17:57:59 +0900997 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 public void addClient(IInputMethodClient client,
999 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 if (!calledFromValidUser()) {
1001 return;
1002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 synchronized (mMethodMap) {
1004 mClients.put(client.asBinder(), new ClientState(client,
1005 inputContext, uid, pid));
1006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 if (!calledFromValidUser()) {
1012 return;
1013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 synchronized (mMethodMap) {
1015 mClients.remove(client.asBinder());
1016 }
1017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void executeOrSendMessage(IInterface target, Message msg) {
1020 if (target.asBinder() instanceof Binder) {
1021 mCaller.sendMessage(msg);
1022 } else {
1023 handleMessage(msg);
1024 msg.recycle();
1025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001028 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001030 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 + mCurClient.client.asBinder());
1032 if (mBoundToMethod) {
1033 mBoundToMethod = false;
1034 if (mCurMethod != null) {
1035 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1036 MSG_UNBIND_INPUT, mCurMethod));
1037 }
1038 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001039
1040 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1041 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1043 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1044 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project10592532009-03-18 17:39:46 -07001047 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 private int getImeShowFlags() {
1052 int flags = 0;
1053 if (mShowForced) {
1054 flags |= InputMethod.SHOW_FORCED
1055 | InputMethod.SHOW_EXPLICIT;
1056 } else if (mShowExplicitlyRequested) {
1057 flags |= InputMethod.SHOW_EXPLICIT;
1058 }
1059 return flags;
1060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 private int getAppShowFlags() {
1063 int flags = 0;
1064 if (mShowForced) {
1065 flags |= InputMethodManager.SHOW_FORCED;
1066 } else if (!mShowExplicitlyRequested) {
1067 flags |= InputMethodManager.SHOW_IMPLICIT;
1068 }
1069 return flags;
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
Dianne Hackborn7663d802012-02-24 13:08:49 -08001072 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (!mBoundToMethod) {
1074 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1075 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1076 mBoundToMethod = true;
1077 }
1078 final SessionState session = mCurClient.curSession;
1079 if (initial) {
1080 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1081 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1082 } else {
1083 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1084 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1085 }
1086 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001087 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001088 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001090 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001094 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 // If no method is currently selected, do nothing.
1096 if (mCurMethodId == null) {
1097 return mNoBinding;
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 ClientState cs = mClients.get(client.asBinder());
1101 if (cs == null) {
1102 throw new IllegalArgumentException("unknown client "
1103 + client.asBinder());
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 try {
1107 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1108 // Check with the window manager to make sure this client actually
1109 // has a window with focus. If not, reject. This is thread safe
1110 // because if the focus changes some time before or after, the
1111 // next client receiving focus that has any interest in input will
1112 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1115 return null;
1116 }
1117 } catch (RemoteException e) {
1118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001119
Dianne Hackborn7663d802012-02-24 13:08:49 -08001120 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1121 }
1122
1123 InputBindResult startInputUncheckedLocked(ClientState cs,
1124 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1125 // If no method is currently selected, do nothing.
1126 if (mCurMethodId == null) {
1127 return mNoBinding;
1128 }
1129
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001130 if (mCurClient == null) {
1131 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1132 if (DEBUG) {
1133 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1134 }
1135 }
1136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 if (mCurClient != cs) {
1138 // If the client is changing, we need to switch over to the new
1139 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001140 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 + cs.client.asBinder());
1143
1144 // If the screen is on, inform the new client it is active
1145 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001146 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1147 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Bump up the sequence for this client and attach it.
1152 mCurSeq++;
1153 if (mCurSeq <= 0) mCurSeq = 1;
1154 mCurClient = cs;
1155 mCurInputContext = inputContext;
1156 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 // Check if the input method is changing.
1159 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1160 if (cs.curSession != null) {
1161 // Fast case: if we are already connected to the input method,
1162 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001163 return attachNewInputLocked(
1164 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 if (mHaveConnection) {
1167 if (mCurMethod != null) {
1168 if (!cs.sessionRequested) {
1169 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001170 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1172 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001173 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 // Return to client, and we will get back with it when
1176 // we have had a session made for it.
1177 return new InputBindResult(null, mCurId, mCurSeq);
1178 } else if (SystemClock.uptimeMillis()
1179 < (mLastBindTime+TIME_TO_RECONNECT)) {
1180 // In this case we have connected to the service, but
1181 // don't yet have its interface. If it hasn't been too
1182 // long since we did the connection, we'll return to
1183 // the client and wait to get the service interface so
1184 // we can report back. If it has been too long, we want
1185 // to fall through so we can try a disconnect/reconnect
1186 // to see if we can get back in touch with the service.
1187 return new InputBindResult(null, mCurId, mCurSeq);
1188 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1190 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001195 return startInputInnerLocked();
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001198 InputBindResult startInputInnerLocked() {
1199 if (mCurMethodId == null) {
1200 return mNoBinding;
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001203 if (!mSystemReady) {
1204 // If the system is not yet ready, we shouldn't be running third
1205 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001206 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1210 if (info == null) {
1211 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001214 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1217 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001218 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1219 com.android.internal.R.string.input_method_binding_label);
1220 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1221 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001222 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001223 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 mLastBindTime = SystemClock.uptimeMillis();
1225 mHaveConnection = true;
1226 mCurId = info.getId();
1227 mCurToken = new Binder();
1228 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001229 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 mIWindowManager.addWindowToken(mCurToken,
1231 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1232 } catch (RemoteException e) {
1233 }
1234 return new InputBindResult(null, mCurId, mCurSeq);
1235 } else {
1236 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 + mCurIntent);
1239 }
1240 return null;
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
satoke7c6998e2011-06-03 17:57:59 +09001243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001245 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001246 if (!calledFromValidUser()) {
1247 return null;
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 synchronized (mMethodMap) {
1250 final long ident = Binder.clearCallingIdentity();
1251 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001252 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 } finally {
1254 Binder.restoreCallingIdentity(ident);
1255 }
1256 }
1257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
satoke7c6998e2011-06-03 17:57:59 +09001259 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 public void finishInput(IInputMethodClient client) {
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
satoke7c6998e2011-06-03 17:57:59 +09001263 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 public void onServiceConnected(ComponentName name, IBinder service) {
1265 synchronized (mMethodMap) {
1266 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1267 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001268 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001270 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001271 return;
1272 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001274 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1275 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001278 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001280 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001281 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
1284 }
1285 }
1286
1287 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1288 synchronized (mMethodMap) {
1289 if (mCurMethod != null && method != null
1290 && mCurMethod.asBinder() == method.asBinder()) {
1291 if (mCurClient != null) {
1292 mCurClient.curSession = new SessionState(mCurClient,
1293 method, session);
1294 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001295 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 if (res.method != null) {
1297 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1298 MSG_BIND_METHOD, mCurClient.client, res));
1299 }
1300 }
1301 }
1302 }
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001305 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001306 if (mVisibleBound) {
1307 mContext.unbindService(mVisibleConnection);
1308 mVisibleBound = false;
1309 }
1310
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001311 if (mHaveConnection) {
1312 mContext.unbindService(this);
1313 mHaveConnection = false;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 if (mCurToken != null) {
1317 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001319 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001320 // The current IME is shown. Hence an IME switch (transition) is happening.
1321 mWindowManagerService.saveLastInputMethodWindowForTransition();
1322 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 mIWindowManager.removeWindowToken(mCurToken);
1324 } catch (RemoteException e) {
1325 }
1326 mCurToken = null;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Project10592532009-03-18 17:39:46 -07001329 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001330 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001332 if (reportToClient && mCurClient != null) {
1333 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1334 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1335 }
1336 }
1337
Devin Taylor0c33ed22010-02-23 13:26:46 -06001338 private void finishSession(SessionState sessionState) {
1339 if (sessionState != null && sessionState.session != null) {
1340 try {
1341 sessionState.session.finishSession();
1342 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001343 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001344 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001345 }
1346 }
1347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001349 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (mCurMethod != null) {
1351 for (ClientState cs : mClients.values()) {
1352 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001353 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 cs.curSession = null;
1355 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001356
1357 finishSession(mEnabledSession);
1358 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 mCurMethod = null;
1360 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001361 if (mStatusBar != null) {
1362 mStatusBar.setIconVisibility("ime", false);
1363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
satoke7c6998e2011-06-03 17:57:59 +09001366 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public void onServiceDisconnected(ComponentName name) {
1368 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001369 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 + " mCurIntent=" + mCurIntent);
1371 if (mCurMethod != null && mCurIntent != null
1372 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001373 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // We consider this to be a new bind attempt, since the system
1375 // should now try to restart the service for us.
1376 mLastBindTime = SystemClock.uptimeMillis();
1377 mShowRequested = mInputShown;
1378 mInputShown = false;
1379 if (mCurClient != null) {
1380 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1381 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1382 }
1383 }
1384 }
1385 }
1386
satokf9f01002011-05-19 21:31:50 +09001387 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001389 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 long ident = Binder.clearCallingIdentity();
1391 try {
1392 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001393 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 return;
1395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 synchronized (mMethodMap) {
1398 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001399 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001400 if (mStatusBar != null) {
1401 mStatusBar.setIconVisibility("ime", false);
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001405 CharSequence contentDescription = null;
1406 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001407 // Use PackageManager to load label
1408 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001409 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001410 mIPackageManager.getApplicationInfo(packageName, 0,
1411 mSettings.getCurrentUserId()));
1412 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001413 /* ignore */
1414 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001415 if (mStatusBar != null) {
1416 mStatusBar.setIcon("ime", packageName, iconId, 0,
1417 contentDescription != null
1418 ? contentDescription.toString() : null);
1419 mStatusBar.setIconVisibility("ime", true);
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422 }
1423 } finally {
1424 Binder.restoreCallingIdentity(ident);
1425 }
1426 }
1427
satok7cfc0ed2011-06-20 21:29:36 +09001428 private boolean needsToShowImeSwitchOngoingNotification() {
1429 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001430 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001431 synchronized (mMethodMap) {
1432 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1433 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001434 if (N > 2) return true;
1435 if (N < 1) return false;
1436 int nonAuxCount = 0;
1437 int auxCount = 0;
1438 InputMethodSubtype nonAuxSubtype = null;
1439 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001440 for(int i = 0; i < N; ++i) {
1441 final InputMethodInfo imi = imis.get(i);
1442 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1443 imi, true);
1444 final int subtypeCount = subtypes.size();
1445 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001446 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001447 } else {
1448 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001449 final InputMethodSubtype subtype = subtypes.get(j);
1450 if (!subtype.isAuxiliary()) {
1451 ++nonAuxCount;
1452 nonAuxSubtype = subtype;
1453 } else {
1454 ++auxCount;
1455 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001456 }
1457 }
1458 }
satok7cfc0ed2011-06-20 21:29:36 +09001459 }
satokb6359412011-06-28 17:47:41 +09001460 if (nonAuxCount > 1 || auxCount > 1) {
1461 return true;
1462 } else if (nonAuxCount == 1 && auxCount == 1) {
1463 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001464 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1465 || auxSubtype.overridesImplicitlyEnabledSubtype()
1466 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001467 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1468 return false;
1469 }
1470 return true;
1471 }
1472 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001473 }
satok7cfc0ed2011-06-20 21:29:36 +09001474 }
1475
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001477 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001478 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001479 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001480 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001481 try {
1482 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001484 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001485 return;
1486 }
1487
1488 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001489 mImeWindowVis = vis;
1490 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001491 if (mStatusBar != null) {
1492 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1493 }
satok7cfc0ed2011-06-20 21:29:36 +09001494 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001495 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1496 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001497 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001498 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001499 final CharSequence title = mRes.getText(
1500 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001501 final CharSequence imiLabel = imi.loadLabel(pm);
1502 final CharSequence summary = mCurrentSubtype != null
1503 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1504 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1505 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001506 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001507 : imiLabel;
1508
satok7cfc0ed2011-06-20 21:29:36 +09001509 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001510 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001511 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001512 if (DEBUG) {
1513 Slog.d(TAG, "--- show notification: label = " + imiLabel
1514 + ", summary = " + summary);
1515 }
1516 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001517 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001518 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001519 mNotificationShown = true;
1520 }
satok7cfc0ed2011-06-20 21:29:36 +09001521 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001522 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001523 if (DEBUG) {
1524 Slog.d(TAG, "--- hide notification");
1525 }
1526 mNotificationManager.cancelAsUser(null,
1527 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001528 mNotificationShown = false;
1529 }
1530 }
satok06487a52010-10-29 11:37:18 +09001531 }
1532 } finally {
1533 Binder.restoreCallingIdentity(ident);
1534 }
1535 }
1536
satoke7c6998e2011-06-03 17:57:59 +09001537 @Override
satokf9f01002011-05-19 21:31:50 +09001538 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001539 if (!calledFromValidUser()) {
1540 return;
1541 }
satokf9f01002011-05-19 21:31:50 +09001542 synchronized (mMethodMap) {
1543 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1544 for (int i = 0; i < spans.length; ++i) {
1545 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001546 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001547 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001548 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001549 }
1550 }
1551 }
1552 }
1553
satoke7c6998e2011-06-03 17:57:59 +09001554 @Override
satokf9f01002011-05-19 21:31:50 +09001555 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001556 if (!calledFromValidUser()) {
1557 return false;
1558 }
satokf9f01002011-05-19 21:31:50 +09001559 synchronized (mMethodMap) {
1560 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1561 // TODO: Do not send the intent if the process of the targetImi is already dead.
1562 if (targetImi != null) {
1563 final String[] suggestions = span.getSuggestions();
1564 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001565 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001566 final Intent intent = new Intent();
1567 // Ensures that only a class in the original IME package will receive the
1568 // notification.
satok42c5a162011-05-26 16:46:14 +09001569 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001570 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1571 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1572 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1573 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001574 final long ident = Binder.clearCallingIdentity();
1575 try {
1576 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1577 } finally {
1578 Binder.restoreCallingIdentity(ident);
1579 }
satokf9f01002011-05-19 21:31:50 +09001580 return true;
1581 }
1582 }
1583 return false;
1584 }
1585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001587 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1588 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1589 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1590 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001591 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001592 // There is no input method selected, try to choose new applicable input method.
1593 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001595 }
1596 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 try {
satokab751aa2010-09-14 19:17:36 +09001598 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001601 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001602 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
satokf3db1af2010-11-23 13:34:33 +09001604 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001605 } else {
1606 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001607 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001608 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
1610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
satokab751aa2010-09-14 19:17:36 +09001612 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 InputMethodInfo info = mMethodMap.get(id);
1614 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001615 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
satokd81e9502012-05-21 12:58:45 +09001618 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001620 final int subtypeCount = info.getSubtypeCount();
1621 if (subtypeCount <= 0) {
1622 return;
satokcd7cd292010-11-20 15:46:23 +09001623 }
satokd81e9502012-05-21 12:58:45 +09001624 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1625 final InputMethodSubtype newSubtype;
1626 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1627 newSubtype = info.getSubtypeAt(subtypeId);
1628 } else {
1629 // If subtype is null, try to find the most applicable one from
1630 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001631 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001632 }
1633 if (newSubtype == null || oldSubtype == null) {
1634 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1635 + ", new subtype = " + newSubtype);
1636 return;
1637 }
1638 if (newSubtype != oldSubtype) {
1639 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1640 if (mCurMethod != null) {
1641 try {
1642 refreshImeWindowVisibilityLocked();
1643 mCurMethod.changeInputMethodSubtype(newSubtype);
1644 } catch (RemoteException e) {
1645 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001646 }
1647 }
1648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 return;
1650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
satokd81e9502012-05-21 12:58:45 +09001652 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 final long ident = Binder.clearCallingIdentity();
1654 try {
satokab751aa2010-09-14 19:17:36 +09001655 // Set a subtype to this input method.
1656 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001657 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1658 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1659 // because mCurMethodId is stored as a history in
1660 // setSelectedInputMethodAndSubtypeLocked().
1661 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662
1663 if (ActivityManagerNative.isSystemReady()) {
1664 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001665 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001667 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001669 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 } finally {
1671 Binder.restoreCallingIdentity(ident);
1672 }
1673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001674
satok42c5a162011-05-26 16:46:14 +09001675 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001676 public boolean showSoftInput(IInputMethodClient client, int flags,
1677 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001678 if (!calledFromValidUser()) {
1679 return false;
1680 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001681 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 long ident = Binder.clearCallingIdentity();
1683 try {
1684 synchronized (mMethodMap) {
1685 if (mCurClient == null || client == null
1686 || mCurClient.client.asBinder() != client.asBinder()) {
1687 try {
1688 // We need to check if this is the current client with
1689 // focus in the window manager, to allow this call to
1690 // be made before input is started in it.
1691 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001692 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001693 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001701 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703 } finally {
1704 Binder.restoreCallingIdentity(ident);
1705 }
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
The Android Open Source Project4df24232009-03-05 14:34:35 -08001708 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 mShowRequested = true;
1710 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1711 mShowExplicitlyRequested = true;
1712 }
1713 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1714 mShowExplicitlyRequested = true;
1715 mShowForced = true;
1716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
Dianne Hackborncc278702009-09-02 23:07:23 -07001718 if (!mSystemReady) {
1719 return false;
1720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001721
The Android Open Source Project4df24232009-03-05 14:34:35 -08001722 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001724 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1725 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1726 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001728 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001729 bindCurrentInputMethodService(
1730 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001731 mVisibleBound = true;
1732 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001733 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001735 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 // The client has asked to have the input method shown, but
1737 // we have been sitting here too long with a connection to the
1738 // service and no interface received, so let's disconnect/connect
1739 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001742 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001744 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001745 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001746 } else {
1747 if (DEBUG) {
1748 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1749 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
The Android Open Source Project4df24232009-03-05 14:34:35 -08001753 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
satok42c5a162011-05-26 16:46:14 +09001756 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 public boolean hideSoftInput(IInputMethodClient client, int flags,
1758 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001759 if (!calledFromValidUser()) {
1760 return false;
1761 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001762 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 long ident = Binder.clearCallingIdentity();
1764 try {
1765 synchronized (mMethodMap) {
1766 if (mCurClient == null || client == null
1767 || mCurClient.client.asBinder() != client.asBinder()) {
1768 try {
1769 // We need to check if this is the current client with
1770 // focus in the window manager, to allow this call to
1771 // be made before input is started in it.
1772 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001773 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1774 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001775 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001776 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001779 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
1782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
Joe Onorato8a9b2202010-02-26 18:56:32 -08001784 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001785 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 } finally {
1788 Binder.restoreCallingIdentity(ident);
1789 }
1790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1794 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001795 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001797 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001800 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001804 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001806 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1807 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1808 res = true;
1809 } else {
1810 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001812 if (mHaveConnection && mVisibleBound) {
1813 mContext.unbindService(mVisibleConnection);
1814 mVisibleBound = false;
1815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 mInputShown = false;
1817 mShowRequested = false;
1818 mShowExplicitlyRequested = false;
1819 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001820 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
satok42c5a162011-05-26 16:46:14 +09001823 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001824 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1825 int controlFlags, int softInputMode, int windowFlags,
1826 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001827 // Needs to check the validity before clearing calling identity
1828 final boolean calledFromValidUser = calledFromValidUser();
1829
Dianne Hackborn7663d802012-02-24 13:08:49 -08001830 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 long ident = Binder.clearCallingIdentity();
1832 try {
1833 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001834 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001835 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001837 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
Dianne Hackborn7663d802012-02-24 13:08:49 -08001839 ClientState cs = mClients.get(client.asBinder());
1840 if (cs == null) {
1841 throw new IllegalArgumentException("unknown client "
1842 + client.asBinder());
1843 }
1844
1845 try {
1846 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1847 // Check with the window manager to make sure this client actually
1848 // has a window with focus. If not, reject. This is thread safe
1849 // because if the focus changes some time before or after, the
1850 // next client receiving focus that has any interest in input will
1851 // be calling through here after that change happens.
1852 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1853 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1854 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001856 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001859 if (!calledFromValidUser) {
1860 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1861 Slog.w(TAG, "If you want to interect with IME, you need "
1862 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1863 hideCurrentInputLocked(0, null);
1864 return null;
1865 }
1866
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001867 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001868 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001869 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001870 if (attribute != null) {
1871 return startInputUncheckedLocked(cs, inputContext, attribute,
1872 controlFlags);
1873 }
1874 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001875 }
1876 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001877
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001878 // Should we auto-show the IME even if the caller has not
1879 // specified what should be done with it?
1880 // We only do this automatically if the window can resize
1881 // to accommodate the IME (so what the user sees will give
1882 // them good context without input information being obscured
1883 // by the IME) or if running on a large screen where there
1884 // is more room for the target window + IME.
1885 final boolean doAutoShow =
1886 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1887 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1888 || mRes.getConfiguration().isLayoutSizeAtLeast(
1889 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001890 final boolean isTextEditor =
1891 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1892
1893 // We want to start input before showing the IME, but after closing
1894 // it. We want to do this after closing it to help the IME disappear
1895 // more quickly (not get stuck behind it initializing itself for the
1896 // new focused input, even if its window wants to hide the IME).
1897 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1900 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001901 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1903 // There is no focus view, and this window will
1904 // be behind any soft input window, so hide the
1905 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001906 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001907 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001909 } else if (isTextEditor && doAutoShow && (softInputMode &
1910 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 // There is a focus view, and we are navigating forward
1912 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001913 // We only do this automatically if the window can resize
1914 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001915 // them good context without input information being obscured
1916 // by the IME) or if running on a large screen where there
1917 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001918 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001919 if (attribute != null) {
1920 res = startInputUncheckedLocked(cs, inputContext, attribute,
1921 controlFlags);
1922 didStart = true;
1923 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001924 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
1926 break;
1927 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1928 // Do nothing.
1929 break;
1930 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1931 if ((softInputMode &
1932 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001933 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001934 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936 break;
1937 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001938 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001939 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 break;
1941 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1942 if ((softInputMode &
1943 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001944 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001945 if (attribute != null) {
1946 res = startInputUncheckedLocked(cs, inputContext, attribute,
1947 controlFlags);
1948 didStart = true;
1949 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001950 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952 break;
1953 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001954 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001955 if (attribute != null) {
1956 res = startInputUncheckedLocked(cs, inputContext, attribute,
1957 controlFlags);
1958 didStart = true;
1959 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001960 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 break;
1962 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001963
1964 if (!didStart && attribute != null) {
1965 res = startInputUncheckedLocked(cs, inputContext, attribute,
1966 controlFlags);
1967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969 } finally {
1970 Binder.restoreCallingIdentity(ident);
1971 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001972
1973 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001975
satok42c5a162011-05-26 16:46:14 +09001976 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001978 if (!calledFromValidUser()) {
1979 return;
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 synchronized (mMethodMap) {
1982 if (mCurClient == null || client == null
1983 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001984 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001985 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
1987
satok440aab52010-11-25 09:43:11 +09001988 // Always call subtype picker, because subtype picker is a superset of input method
1989 // picker.
satokab751aa2010-09-14 19:17:36 +09001990 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1991 }
1992 }
1993
satok42c5a162011-05-26 16:46:14 +09001994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001996 if (!calledFromValidUser()) {
1997 return;
1998 }
satok28203512010-11-24 11:06:49 +09001999 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2000 }
2001
satok42c5a162011-05-26 16:46:14 +09002002 @Override
satok28203512010-11-24 11:06:49 +09002003 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002004 if (!calledFromValidUser()) {
2005 return;
2006 }
satok28203512010-11-24 11:06:49 +09002007 synchronized (mMethodMap) {
2008 if (subtype != null) {
2009 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
2010 mMethodMap.get(id), subtype.hashCode()));
2011 } else {
2012 setInputMethod(token, id);
2013 }
2014 }
satokab751aa2010-09-14 19:17:36 +09002015 }
2016
satok42c5a162011-05-26 16:46:14 +09002017 @Override
satokb416a71e2010-11-25 20:42:14 +09002018 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002019 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002020 if (!calledFromValidUser()) {
2021 return;
2022 }
satokb416a71e2010-11-25 20:42:14 +09002023 synchronized (mMethodMap) {
2024 if (mCurClient == null || client == null
2025 || mCurClient.client.asBinder() != client.asBinder()) {
2026 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2027 }
satok7fee71f2010-12-17 18:54:26 +09002028 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2029 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09002030 }
2031 }
2032
satok4fc87d62011-05-20 16:13:43 +09002033 @Override
satok735cf382010-11-11 20:40:09 +09002034 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002035 if (!calledFromValidUser()) {
2036 return false;
2037 }
satok735cf382010-11-11 20:40:09 +09002038 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002039 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002040 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002041 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002042 lastImi = mMethodMap.get(lastIme.first);
2043 } else {
2044 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002045 }
satok4fc87d62011-05-20 16:13:43 +09002046 String targetLastImiId = null;
2047 int subtypeId = NOT_A_SUBTYPE_ID;
2048 if (lastIme != null && lastImi != null) {
2049 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2050 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2051 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2052 : mCurrentSubtype.hashCode();
2053 // If the last IME is the same as the current IME and the last subtype is not
2054 // defined, there is no need to switch to the last IME.
2055 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2056 targetLastImiId = lastIme.first;
2057 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2058 }
2059 }
2060
2061 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2062 // This is a safety net. If the currentSubtype can't be added to the history
2063 // and the framework couldn't find the last ime, we will make the last ime be
2064 // the most applicable enabled keyboard subtype of the system imes.
2065 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2066 if (enabled != null) {
2067 final int N = enabled.size();
2068 final String locale = mCurrentSubtype == null
2069 ? mRes.getConfiguration().locale.toString()
2070 : mCurrentSubtype.getLocale();
2071 for (int i = 0; i < N; ++i) {
2072 final InputMethodInfo imi = enabled.get(i);
2073 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2074 InputMethodSubtype keyboardSubtype =
2075 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2076 SUBTYPE_MODE_KEYBOARD, locale, true);
2077 if (keyboardSubtype != null) {
2078 targetLastImiId = imi.getId();
2079 subtypeId = getSubtypeIdFromHashCode(
2080 imi, keyboardSubtype.hashCode());
2081 if(keyboardSubtype.getLocale().equals(locale)) {
2082 break;
2083 }
2084 }
2085 }
2086 }
2087 }
2088 }
2089
2090 if (!TextUtils.isEmpty(targetLastImiId)) {
2091 if (DEBUG) {
2092 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2093 + ", from: " + mCurMethodId + ", " + subtypeId);
2094 }
2095 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2096 return true;
2097 } else {
2098 return false;
2099 }
satok735cf382010-11-11 20:40:09 +09002100 }
2101 }
2102
satoke7c6998e2011-06-03 17:57:59 +09002103 @Override
satok688bd472012-02-09 20:09:17 +09002104 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002105 if (!calledFromValidUser()) {
2106 return false;
2107 }
satok688bd472012-02-09 20:09:17 +09002108 synchronized (mMethodMap) {
2109 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2110 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2111 if (nextSubtype == null) {
2112 return false;
2113 }
2114 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2115 return true;
2116 }
2117 }
2118
2119 @Override
satok68f1b782011-04-11 14:26:04 +09002120 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002121 if (!calledFromValidUser()) {
2122 return null;
2123 }
satok68f1b782011-04-11 14:26:04 +09002124 synchronized (mMethodMap) {
2125 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2126 // TODO: Handle the case of the last IME with no subtypes
2127 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2128 || TextUtils.isEmpty(lastIme.second)) return null;
2129 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2130 if (lastImi == null) return null;
2131 try {
2132 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002133 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2134 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2135 return null;
2136 }
2137 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002138 } catch (NumberFormatException e) {
2139 return null;
2140 }
2141 }
2142 }
2143
satoke7c6998e2011-06-03 17:57:59 +09002144 @Override
satokee5e77c2011-09-02 18:50:15 +09002145 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002146 if (!calledFromValidUser()) {
2147 return;
2148 }
satok91e88122011-07-18 11:11:42 +09002149 // By this IPC call, only a process which shares the same uid with the IME can add
2150 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002151 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002152 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002153 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002154 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002155 final String[] packageInfos;
2156 try {
2157 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2158 } catch (RemoteException e) {
2159 Slog.e(TAG, "Failed to get package infos");
2160 return;
2161 }
satok91e88122011-07-18 11:11:42 +09002162 if (packageInfos != null) {
2163 final int packageNum = packageInfos.length;
2164 for (int i = 0; i < packageNum; ++i) {
2165 if (packageInfos[i].equals(imi.getPackageName())) {
2166 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002167 final long ident = Binder.clearCallingIdentity();
2168 try {
2169 buildInputMethodListLocked(mMethodList, mMethodMap);
2170 } finally {
2171 Binder.restoreCallingIdentity(ident);
2172 }
satokee5e77c2011-09-02 18:50:15 +09002173 return;
satok91e88122011-07-18 11:11:42 +09002174 }
2175 }
2176 }
satoke7c6998e2011-06-03 17:57:59 +09002177 }
satokee5e77c2011-09-02 18:50:15 +09002178 return;
satoke7c6998e2011-06-03 17:57:59 +09002179 }
2180
satok28203512010-11-24 11:06:49 +09002181 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 synchronized (mMethodMap) {
2183 if (token == null) {
2184 if (mContext.checkCallingOrSelfPermission(
2185 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2186 != PackageManager.PERMISSION_GRANTED) {
2187 throw new SecurityException(
2188 "Using null token requires permission "
2189 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2190 }
2191 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002192 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2193 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 return;
2195 }
2196
satokc5933802011-08-31 21:26:04 +09002197 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 try {
satokab751aa2010-09-14 19:17:36 +09002199 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 } finally {
2201 Binder.restoreCallingIdentity(ident);
2202 }
2203 }
2204 }
2205
satok42c5a162011-05-26 16:46:14 +09002206 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002208 if (!calledFromValidUser()) {
2209 return;
2210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 synchronized (mMethodMap) {
2212 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002213 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2214 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 return;
2216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 long ident = Binder.clearCallingIdentity();
2218 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002219 hideCurrentInputLocked(flags, null);
2220 } finally {
2221 Binder.restoreCallingIdentity(ident);
2222 }
2223 }
2224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002225
satok42c5a162011-05-26 16:46:14 +09002226 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002228 if (!calledFromValidUser()) {
2229 return;
2230 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002231 synchronized (mMethodMap) {
2232 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002233 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2234 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002235 return;
2236 }
2237 long ident = Binder.clearCallingIdentity();
2238 try {
2239 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 } finally {
2241 Binder.restoreCallingIdentity(ident);
2242 }
2243 }
2244 }
2245
2246 void setEnabledSessionInMainThread(SessionState session) {
2247 if (mEnabledSession != session) {
2248 if (mEnabledSession != null) {
2249 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002250 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 mEnabledSession.method.setSessionEnabled(
2252 mEnabledSession.session, false);
2253 } catch (RemoteException e) {
2254 }
2255 }
2256 mEnabledSession = session;
2257 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002258 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 session.method.setSessionEnabled(
2260 session.session, true);
2261 } catch (RemoteException e) {
2262 }
2263 }
2264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002265
satok42c5a162011-05-26 16:46:14 +09002266 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002268 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 switch (msg.what) {
2270 case MSG_SHOW_IM_PICKER:
2271 showInputMethodMenu();
2272 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002273
satokab751aa2010-09-14 19:17:36 +09002274 case MSG_SHOW_IM_SUBTYPE_PICKER:
2275 showInputMethodSubtypeMenu();
2276 return true;
2277
satok47a44912010-10-06 16:03:58 +09002278 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002279 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002280 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002281 args.recycle();
satok217f5482010-12-15 05:19:19 +09002282 return true;
2283
2284 case MSG_SHOW_IM_CONFIG:
2285 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002286 return true;
2287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 case MSG_UNBIND_INPUT:
2291 try {
2292 ((IInputMethod)msg.obj).unbindInput();
2293 } catch (RemoteException e) {
2294 // There is nothing interesting about the method dying.
2295 }
2296 return true;
2297 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002298 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 try {
2300 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2301 } catch (RemoteException e) {
2302 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002303 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 return true;
2305 case MSG_SHOW_SOFT_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 {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002308 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2309 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 } catch (RemoteException e) {
2311 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002312 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 return true;
2314 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002315 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002317 ((IInputMethod)args.arg1).hideSoftInput(0,
2318 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 } catch (RemoteException e) {
2320 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002321 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 return true;
2323 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002324 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002326 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2328 } catch (RemoteException e) {
2329 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002330 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 return true;
2332 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002333 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 try {
2335 ((IInputMethod)args.arg1).createSession(
2336 (IInputMethodCallback)args.arg2);
2337 } catch (RemoteException e) {
2338 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002339 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 try {
2346 SessionState session = (SessionState)args.arg1;
2347 setEnabledSessionInMainThread(session);
2348 session.method.startInput((IInputContext)args.arg2,
2349 (EditorInfo)args.arg3);
2350 } catch (RemoteException e) {
2351 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002352 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 return true;
2354 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002355 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 try {
2357 SessionState session = (SessionState)args.arg1;
2358 setEnabledSessionInMainThread(session);
2359 session.method.restartInput((IInputContext)args.arg2,
2360 (EditorInfo)args.arg3);
2361 } catch (RemoteException e) {
2362 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002363 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 case MSG_UNBIND_METHOD:
2369 try {
2370 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2371 } catch (RemoteException e) {
2372 // There is nothing interesting about the last client dying.
2373 }
2374 return true;
2375 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002376 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 try {
2378 ((IInputMethodClient)args.arg1).onBindMethod(
2379 (InputBindResult)args.arg2);
2380 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002381 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002383 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002385 case MSG_SET_ACTIVE:
2386 try {
2387 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2388 } catch (RemoteException e) {
2389 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2390 + ((ClientState)msg.obj).pid + " uid "
2391 + ((ClientState)msg.obj).uid);
2392 }
2393 return true;
satok01038492012-04-09 21:08:27 +09002394
2395 // --------------------------------------------------------------
2396 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2397 mHardKeyboardListener.handleHardKeyboardStatusChange(
2398 msg.arg1 == 1, msg.arg2 == 1);
2399 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 }
2401 return false;
2402 }
2403
satok5b927c432012-05-01 20:09:34 +09002404 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002405 return (inputMethod.getServiceInfo().applicationInfo.flags
2406 & ApplicationInfo.FLAG_SYSTEM) != 0;
2407 }
2408
Ken Wakasa586f0512011-01-20 22:31:01 +09002409 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2410 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2411 final int subtypeCount = imi.getSubtypeCount();
2412 for (int i = 0; i < subtypeCount; ++i) {
2413 subtypes.add(imi.getSubtypeAt(i));
2414 }
2415 return subtypes;
2416 }
2417
satoka86f5e42011-09-02 17:12:42 +09002418 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2419 InputMethodInfo imi, String mode) {
2420 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2421 final int subtypeCount = imi.getSubtypeCount();
2422 for (int i = 0; i < subtypeCount; ++i) {
2423 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2424 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2425 subtypes.add(subtype);
2426 }
2427 }
2428 return subtypes;
2429 }
2430
satokdc9ddae2011-10-06 12:22:36 +09002431 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002432 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002433 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002434 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002435 int i = enabled.size();
2436 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002437 while (i > 0) {
2438 i--;
satokdc9ddae2011-10-06 12:22:36 +09002439 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002440 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2441 return imi;
2442 }
2443 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2444 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002445 }
2446 }
satok0a1bcf42012-05-16 19:26:31 +09002447 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002448 }
2449 return null;
2450 }
2451
2452 private boolean chooseNewDefaultIMELocked() {
2453 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2454 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002455 if (DEBUG) {
2456 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2457 }
satok723a27e2010-11-11 14:58:11 +09002458 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002459 return true;
2460 }
2461
2462 return false;
2463 }
2464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2466 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002467 if (DEBUG) {
2468 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 list.clear();
2471 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002472
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002473 // Use for queryIntentServicesAsUser
2474 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002475 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002476 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002477 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002478 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002480 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002482 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002483
satoke7c6998e2011-06-03 17:57:59 +09002484 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2485 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 for (int i = 0; i < services.size(); ++i) {
2487 ResolveInfo ri = services.get(i);
2488 ServiceInfo si = ri.serviceInfo;
2489 ComponentName compName = new ComponentName(si.packageName, si.name);
2490 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2491 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002492 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 + ": it does not require the permission "
2494 + android.Manifest.permission.BIND_INPUT_METHOD);
2495 continue;
2496 }
2497
Joe Onorato8a9b2202010-02-26 18:56:32 -08002498 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499
2500 try {
satoke7c6998e2011-06-03 17:57:59 +09002501 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002503 final String id = p.getId();
2504 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505
satok5b927c432012-05-01 20:09:34 +09002506 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002507 // by default
2508 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002509 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002510 }
2511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002513 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002517 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002519 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 }
2521 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002522
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002523 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002524 if (!TextUtils.isEmpty(defaultImiId)) {
2525 if (!map.containsKey(defaultImiId)) {
2526 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2527 if (chooseNewDefaultIMELocked()) {
2528 updateFromSettingsLocked();
2529 }
2530 } else {
2531 // Double check that the default IME is certainly enabled.
2532 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002533 }
2534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002538
satokab751aa2010-09-14 19:17:36 +09002539 private void showInputMethodMenu() {
2540 showInputMethodMenuInternal(false);
2541 }
2542
2543 private void showInputMethodSubtypeMenu() {
2544 showInputMethodMenuInternal(true);
2545 }
2546
satok217f5482010-12-15 05:19:19 +09002547 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002548 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002549 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002550 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2551 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002552 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002553 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002554 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002555 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002556 }
2557
2558 private void showConfigureInputMethods() {
2559 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2560 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2561 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2562 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002563 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002564 }
2565
satok2c93efc2012-04-02 19:33:47 +09002566 private boolean isScreenLocked() {
2567 return mKeyguardManager != null
2568 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2569 }
satokab751aa2010-09-14 19:17:36 +09002570 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002571 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002574 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002575
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002576 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002577 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002578 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002579
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002580 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002581 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2582 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002583 if (immis == null || immis.size() == 0) {
2584 return;
2585 }
2586
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002587 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588
satok688bd472012-02-09 20:09:17 +09002589 final List<ImeSubtypeListItem> imList =
2590 mImListManager.getSortedInputMethodAndSubtypeList(
2591 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002592
satokc3690562012-01-10 20:14:43 +09002593 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002594 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002595 if (currentSubtype != null) {
2596 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2597 lastInputMethodSubtypeId =
2598 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2599 }
2600 }
2601
Ken Wakasa761eb372011-03-04 19:06:18 +09002602 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002603 mIms = new InputMethodInfo[N];
2604 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002605 int checkedItem = 0;
2606 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002607 final ImeSubtypeListItem item = imList.get(i);
2608 mIms[i] = item.mImi;
2609 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002610 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002611 int subtypeId = mSubtypeIds[i];
2612 if ((subtypeId == NOT_A_SUBTYPE_ID)
2613 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2614 || (subtypeId == lastInputMethodSubtypeId)) {
2615 checkedItem = i;
2616 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002619 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002620 com.android.internal.R.styleable.DialogPreference,
2621 com.android.internal.R.attr.alertDialogStyle, 0);
2622 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002623 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002624 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002627 }
2628 })
2629 .setIcon(a.getDrawable(
2630 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2631 a.recycle();
satok01038492012-04-09 21:08:27 +09002632 final LayoutInflater inflater =
2633 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2634 final View tv = inflater.inflate(
2635 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2636 mDialogBuilder.setCustomTitle(tv);
2637
2638 // Setup layout for a toggle switch of the hardware keyboard
2639 mSwitchingDialogTitleView = tv;
2640 mSwitchingDialogTitleView.findViewById(
2641 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2642 mWindowManagerService.isHardKeyboardAvailable() ?
2643 View.VISIBLE : View.GONE);
2644 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2645 com.android.internal.R.id.hard_keyboard_switch));
2646 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2647 hardKeySwitch.setOnCheckedChangeListener(
2648 new OnCheckedChangeListener() {
2649 @Override
2650 public void onCheckedChanged(
2651 CompoundButton buttonView, boolean isChecked) {
2652 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2653 }
2654 });
satokd87c2592010-09-29 11:52:06 +09002655
Ken Wakasa05dbb652011-08-22 15:22:43 +09002656 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2657 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2658 checkedItem);
2659
2660 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002661 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002662 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002663 public void onClick(DialogInterface dialog, int which) {
2664 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002665 if (mIms == null || mIms.length <= which
2666 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002667 return;
2668 }
2669 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002670 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002671 hideInputMethodMenu();
2672 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002673 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002674 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002675 subtypeId = NOT_A_SUBTYPE_ID;
2676 }
2677 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002678 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002681 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
satokbc81b692011-08-26 16:22:22 +09002683 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002684 mDialogBuilder.setPositiveButton(
2685 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002686 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002687 @Override
satok7f35c8c2010-10-07 21:13:11 +09002688 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002689 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002690 }
2691 });
2692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002694 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 mSwitchingDialog.getWindow().setType(
2696 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002697 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2698 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002699 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 mSwitchingDialog.show();
2701 }
2702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002703
satok93d744d2012-05-09 17:14:08 +09002704 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002705 public final CharSequence mImeName;
2706 public final CharSequence mSubtypeName;
2707 public final InputMethodInfo mImi;
2708 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002709 private final boolean mIsSystemLocale;
2710 private final boolean mIsSystemLanguage;
2711
Ken Wakasa05dbb652011-08-22 15:22:43 +09002712 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002713 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002714 mImeName = imeName;
2715 mSubtypeName = subtypeName;
2716 mImi = imi;
2717 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002718 if (TextUtils.isEmpty(subtypeLocale)) {
2719 mIsSystemLocale = false;
2720 mIsSystemLanguage = false;
2721 } else {
2722 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2723 mIsSystemLanguage = mIsSystemLocale
2724 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2725 }
2726 }
2727
2728 @Override
2729 public int compareTo(ImeSubtypeListItem other) {
2730 if (TextUtils.isEmpty(mImeName)) {
2731 return 1;
2732 }
2733 if (TextUtils.isEmpty(other.mImeName)) {
2734 return -1;
2735 }
2736 if (!TextUtils.equals(mImeName, other.mImeName)) {
2737 return mImeName.toString().compareTo(other.mImeName.toString());
2738 }
2739 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2740 return 0;
2741 }
2742 if (mIsSystemLocale) {
2743 return -1;
2744 }
2745 if (other.mIsSystemLocale) {
2746 return 1;
2747 }
2748 if (mIsSystemLanguage) {
2749 return -1;
2750 }
2751 if (other.mIsSystemLanguage) {
2752 return 1;
2753 }
2754 if (TextUtils.isEmpty(mSubtypeName)) {
2755 return 1;
2756 }
2757 if (TextUtils.isEmpty(other.mSubtypeName)) {
2758 return -1;
2759 }
2760 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002761 }
2762 }
2763
2764 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2765 private final LayoutInflater mInflater;
2766 private final int mTextViewResourceId;
2767 private final List<ImeSubtypeListItem> mItemsList;
2768 private final int mCheckedItem;
2769 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2770 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2771 super(context, textViewResourceId, itemsList);
2772 mTextViewResourceId = textViewResourceId;
2773 mItemsList = itemsList;
2774 mCheckedItem = checkedItem;
2775 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2776 }
2777
2778 @Override
2779 public View getView(int position, View convertView, ViewGroup parent) {
2780 final View view = convertView != null ? convertView
2781 : mInflater.inflate(mTextViewResourceId, null);
2782 if (position < 0 || position >= mItemsList.size()) return view;
2783 final ImeSubtypeListItem item = mItemsList.get(position);
2784 final CharSequence imeName = item.mImeName;
2785 final CharSequence subtypeName = item.mSubtypeName;
2786 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2787 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2788 if (TextUtils.isEmpty(subtypeName)) {
2789 firstTextView.setText(imeName);
2790 secondTextView.setVisibility(View.GONE);
2791 } else {
2792 firstTextView.setText(subtypeName);
2793 secondTextView.setText(imeName);
2794 secondTextView.setVisibility(View.VISIBLE);
2795 }
2796 final RadioButton radioButton =
2797 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2798 radioButton.setChecked(position == mCheckedItem);
2799 return view;
2800 }
2801 }
2802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002804 synchronized (mMethodMap) {
2805 hideInputMethodMenuLocked();
2806 }
2807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002808
The Android Open Source Project10592532009-03-18 17:39:46 -07002809 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002810 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811
The Android Open Source Project10592532009-03-18 17:39:46 -07002812 if (mSwitchingDialog != null) {
2813 mSwitchingDialog.dismiss();
2814 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
The Android Open Source Project10592532009-03-18 17:39:46 -07002817 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002818 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002822
satok42c5a162011-05-26 16:46:14 +09002823 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002825 // TODO: Make this work even for non-current users?
2826 if (!calledFromValidUser()) {
2827 return false;
2828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 synchronized (mMethodMap) {
2830 if (mContext.checkCallingOrSelfPermission(
2831 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2832 != PackageManager.PERMISSION_GRANTED) {
2833 throw new SecurityException(
2834 "Requires permission "
2835 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2836 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 long ident = Binder.clearCallingIdentity();
2839 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002840 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 } finally {
2842 Binder.restoreCallingIdentity(ident);
2843 }
2844 }
2845 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002846
2847 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2848 // Make sure this is a valid input method.
2849 InputMethodInfo imm = mMethodMap.get(id);
2850 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002851 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002852 }
2853
satokd87c2592010-09-29 11:52:06 +09002854 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2855 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002856
satokd87c2592010-09-29 11:52:06 +09002857 if (enabled) {
2858 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2859 if (pair.first.equals(id)) {
2860 // We are enabling this input method, but it is already enabled.
2861 // Nothing to do. The previous state was enabled.
2862 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002863 }
2864 }
satokd87c2592010-09-29 11:52:06 +09002865 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2866 // Previous state was disabled.
2867 return false;
2868 } else {
2869 StringBuilder builder = new StringBuilder();
2870 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2871 builder, enabledInputMethodsList, id)) {
2872 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002873 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002874 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2875 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2876 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002877 }
2878 // Previous state was enabled.
2879 return true;
2880 } else {
2881 // We are disabling the input method but it is already disabled.
2882 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002883 return false;
2884 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002885 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002886 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002887
satok57ffc002011-01-25 00:11:47 +09002888 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2889 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002890 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002891 }
2892
satok723a27e2010-11-11 14:58:11 +09002893 private void saveCurrentInputMethodAndSubtypeToHistory() {
2894 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2895 if (mCurrentSubtype != null) {
2896 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2897 }
satok57ffc002011-01-25 00:11:47 +09002898 if (canAddToLastInputMethod(mCurrentSubtype)) {
2899 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2900 }
satokab751aa2010-09-14 19:17:36 +09002901 }
2902
satok723a27e2010-11-11 14:58:11 +09002903 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2904 boolean setSubtypeOnly) {
2905 // Update the history of InputMethod and Subtype
2906 saveCurrentInputMethodAndSubtypeToHistory();
2907
2908 // Set Subtype here
2909 if (imi == null || subtypeId < 0) {
2910 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002911 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002912 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002913 if (subtypeId < imi.getSubtypeCount()) {
2914 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2915 mSettings.putSelectedSubtype(subtype.hashCode());
2916 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002917 } else {
2918 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002919 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002920 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002921 }
satokab751aa2010-09-14 19:17:36 +09002922 }
satok723a27e2010-11-11 14:58:11 +09002923
satok4c0e7152012-06-20 20:08:44 +09002924 // Workaround.
2925 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2926 // IMEs are not recognized and considered uninstalled.
2927 // Actually, we can't move everything after SystemReady because
2928 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2929 // the default IME here and try cheking the default IME again in systemReady().
2930 // TODO: Do nothing before system ready and implement a separated logic for
2931 // the encryption lock screen.
2932 // TODO: ASEC should be ready before IMMS is instantiated.
2933 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002934 // Set InputMethod here
2935 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2936 }
2937 }
2938
2939 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2940 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2941 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2942 // newDefaultIme is empty when there is no candidate for the selected IME.
2943 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2944 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2945 if (subtypeHashCode != null) {
2946 try {
2947 lastSubtypeId = getSubtypeIdFromHashCode(
2948 imi, Integer.valueOf(subtypeHashCode));
2949 } catch (NumberFormatException e) {
2950 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2951 }
2952 }
2953 }
2954 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002955 }
2956
2957 private int getSelectedInputMethodSubtypeId(String id) {
2958 InputMethodInfo imi = mMethodMap.get(id);
2959 if (imi == null) {
2960 return NOT_A_SUBTYPE_ID;
2961 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002962 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2963 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002964 }
2965
satokfdf419e2012-05-08 16:52:08 +09002966 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2967 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2968 }
2969
2970 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002971 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002972 final int subtypeCount = imi.getSubtypeCount();
2973 for (int i = 0; i < subtypeCount; ++i) {
2974 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002975 if (subtypeHashCode == ims.hashCode()) {
2976 return i;
2977 }
satokab751aa2010-09-14 19:17:36 +09002978 }
2979 }
2980 return NOT_A_SUBTYPE_ID;
2981 }
2982
satoka86f5e42011-09-02 17:12:42 +09002983 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2984 Resources res, InputMethodInfo imi) {
2985 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002986 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002987 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002988 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002989 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002990 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002991 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002992 // scan overriding implicitly enabled subtypes.
2993 InputMethodSubtype subtype = subtypes.get(i);
2994 if (subtype.overridesImplicitlyEnabledSubtype()) {
2995 final String mode = subtype.getMode();
2996 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2997 applicableModeAndSubtypesMap.put(mode, subtype);
2998 }
2999 }
3000 }
3001 if (applicableModeAndSubtypesMap.size() > 0) {
3002 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
3003 }
3004 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09003005 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09003006 final String locale = subtype.getLocale();
3007 final String mode = subtype.getMode();
3008 // When system locale starts with subtype's locale, that subtype will be applicable
3009 // for system locale
3010 // For instance, it's clearly applicable for cases like system locale = en_US and
3011 // subtype = en, but it is not necessarily considered applicable for cases like system
3012 // locale = en and subtype = en_US.
3013 // We just call systemLocale.startsWith(locale) in this function because there is no
3014 // need to find applicable subtypes aggressively unlike
3015 // findLastResortApplicableSubtypeLocked.
3016 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003017 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003018 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003019 if (applicableSubtype != null) {
3020 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3021 if (!systemLocale.equals(locale)) continue;
3022 }
satok3da92232011-01-11 22:46:30 +09003023 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003024 }
3025 }
3026 final InputMethodSubtype keyboardSubtype
3027 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3028 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3029 applicableModeAndSubtypesMap.values());
3030 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3031 for (int i = 0; i < N; ++i) {
3032 final InputMethodSubtype subtype = subtypes.get(i);
3033 final String mode = subtype.getMode();
3034 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3035 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3036 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003037 }
3038 }
3039 }
satokc3690562012-01-10 20:14:43 +09003040 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003041 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003042 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003043 if (lastResortKeyboardSubtype != null) {
3044 applicableSubtypes.add(lastResortKeyboardSubtype);
3045 }
3046 }
3047 return applicableSubtypes;
3048 }
3049
satok4e4569d2010-11-19 18:45:53 +09003050 /**
3051 * If there are no selected subtypes, tries finding the most applicable one according to the
3052 * given locale.
3053 * @param subtypes this function will search the most applicable subtype in subtypes
3054 * @param mode subtypes will be filtered by mode
3055 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09003056 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3057 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003058 * @return the most applicable subtypeId
3059 */
satokdf31ae62011-01-15 06:19:44 +09003060 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3061 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09003062 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003063 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003064 return null;
satok8fbb1e82010-11-02 23:15:58 +09003065 }
satok4e4569d2010-11-19 18:45:53 +09003066 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003067 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003068 }
satok8fbb1e82010-11-02 23:15:58 +09003069 final String language = locale.substring(0, 2);
3070 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003071 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09003072 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003073 final int N = subtypes.size();
3074 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003075 InputMethodSubtype subtype = subtypes.get(i);
3076 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003077 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3078 // and all subtypes with all modes can be candidates.
3079 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09003080 if (firstMatchedModeSubtype == null) {
3081 firstMatchedModeSubtype = subtype;
3082 }
satok9ef02832010-11-04 21:17:48 +09003083 if (locale.equals(subtypeLocale)) {
3084 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003085 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003086 break;
3087 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3088 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003089 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003090 partialMatchFound = true;
3091 }
satok8fbb1e82010-11-02 23:15:58 +09003092 }
3093 }
3094
satok7599a7fb2010-12-22 13:45:23 +09003095 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3096 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003097 }
3098
satok8fbb1e82010-11-02 23:15:58 +09003099 // The first subtype applicable to the system locale will be defined as the most applicable
3100 // subtype.
3101 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003102 if (applicableSubtype != null) {
3103 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3104 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3105 }
satok8fbb1e82010-11-02 23:15:58 +09003106 }
satokcd7cd292010-11-20 15:46:23 +09003107 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003108 }
3109
satok4e4569d2010-11-19 18:45:53 +09003110 // If there are no selected shortcuts, tries finding the most applicable ones.
3111 private Pair<InputMethodInfo, InputMethodSubtype>
3112 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3113 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3114 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003115 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003116 boolean foundInSystemIME = false;
3117
3118 // Search applicable subtype for each InputMethodInfo
3119 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09003120 final String imiId = imi.getId();
3121 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3122 continue;
3123 }
satokcd7cd292010-11-20 15:46:23 +09003124 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003125 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003126 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003127 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003128 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003129 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003130 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003131 }
satokdf31ae62011-01-15 06:19:44 +09003132 // 2. Search by the system locale from enabledSubtypes.
3133 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003134 if (subtype == null) {
3135 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003136 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003137 }
satoka86f5e42011-09-02 17:12:42 +09003138 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3139 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3140 final ArrayList<InputMethodSubtype> subtypesForSearch =
3141 overridingImplicitlyEnabledSubtypes.isEmpty()
3142 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09003143 // 4. Search by the current subtype's locale from all subtypes.
3144 if (subtype == null && mCurrentSubtype != null) {
3145 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003146 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09003147 }
3148 // 5. Search by the system locale from all subtypes.
3149 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003150 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09003151 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003152 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003153 }
satokcd7cd292010-11-20 15:46:23 +09003154 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09003155 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003156 // The current input method is the most applicable IME.
3157 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003158 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003159 break;
satok7599a7fb2010-12-22 13:45:23 +09003160 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003161 // The system input method is 2nd applicable IME.
3162 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003163 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09003164 if ((imi.getServiceInfo().applicationInfo.flags
3165 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3166 foundInSystemIME = true;
3167 }
satok4e4569d2010-11-19 18:45:53 +09003168 }
3169 }
3170 }
3171 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003172 if (mostApplicableIMI != null) {
3173 Slog.w(TAG, "Most applicable shortcut input method was:"
3174 + mostApplicableIMI.getId());
3175 if (mostApplicableSubtype != null) {
3176 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3177 + "," + mostApplicableSubtype.getMode() + ","
3178 + mostApplicableSubtype.getLocale());
3179 }
3180 }
satok4e4569d2010-11-19 18:45:53 +09003181 }
satokcd7cd292010-11-20 15:46:23 +09003182 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003183 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003184 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003185 } else {
3186 return null;
3187 }
3188 }
3189
satokab751aa2010-09-14 19:17:36 +09003190 /**
3191 * @return Return the current subtype of this input method.
3192 */
satok42c5a162011-05-26 16:46:14 +09003193 @Override
satokab751aa2010-09-14 19:17:36 +09003194 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003195 // TODO: Make this work even for non-current users?
3196 if (!calledFromValidUser()) {
3197 return null;
3198 }
3199 synchronized (mMethodMap) {
3200 return getCurrentInputMethodSubtypeLocked();
3201 }
3202 }
3203
3204 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003205 if (mCurMethodId == null) {
3206 return null;
3207 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003208 final boolean subtypeIsSelected =
3209 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3210 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3211 if (imi == null || imi.getSubtypeCount() == 0) {
3212 return null;
satok4e4569d2010-11-19 18:45:53 +09003213 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003214 if (!subtypeIsSelected || mCurrentSubtype == null
3215 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3216 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3217 if (subtypeId == NOT_A_SUBTYPE_ID) {
3218 // If there are no selected subtypes, the framework will try to find
3219 // the most applicable subtype from explicitly or implicitly enabled
3220 // subtypes.
3221 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3222 getEnabledInputMethodSubtypeListLocked(imi, true);
3223 // If there is only one explicitly or implicitly enabled subtype,
3224 // just returns it.
3225 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3226 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3227 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3228 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3229 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3230 SUBTYPE_MODE_KEYBOARD, null, true);
3231 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003232 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003233 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3234 true);
satok4e4569d2010-11-19 18:45:53 +09003235 }
satok3ef8b292010-11-23 06:06:29 +09003236 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003237 } else {
3238 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003239 }
3240 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003241 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003242 }
3243
satokf3db1af2010-11-23 13:34:33 +09003244 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3245 InputMethodSubtype subtype) {
3246 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3247 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3248 } else {
3249 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3250 subtypes.add(subtype);
3251 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3252 }
3253 }
3254
satok4e4569d2010-11-19 18:45:53 +09003255 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003256 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003257 @Override
satok4e4569d2010-11-19 18:45:53 +09003258 public List getShortcutInputMethodsAndSubtypes() {
3259 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003260 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003261 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003262 // If there are no selected shortcut subtypes, the framework will try to find
3263 // the most applicable subtype from all subtypes whose mode is
3264 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003265 Pair<InputMethodInfo, InputMethodSubtype> info =
3266 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3267 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003268 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003269 ret.add(info.first);
3270 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003271 }
satok3da92232011-01-11 22:46:30 +09003272 return ret;
satokf3db1af2010-11-23 13:34:33 +09003273 }
satokf3db1af2010-11-23 13:34:33 +09003274 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3275 ret.add(imi);
3276 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3277 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003278 }
3279 }
satokf3db1af2010-11-23 13:34:33 +09003280 return ret;
satok4e4569d2010-11-19 18:45:53 +09003281 }
3282 }
3283
satok42c5a162011-05-26 16:46:14 +09003284 @Override
satokb66d2872010-11-10 01:04:04 +09003285 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003286 // TODO: Make this work even for non-current users?
3287 if (!calledFromValidUser()) {
3288 return false;
3289 }
satokb66d2872010-11-10 01:04:04 +09003290 synchronized (mMethodMap) {
3291 if (subtype != null && mCurMethodId != null) {
3292 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3293 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3294 if (subtypeId != NOT_A_SUBTYPE_ID) {
3295 setInputMethodLocked(mCurMethodId, subtypeId);
3296 return true;
3297 }
3298 }
3299 return false;
3300 }
3301 }
3302
satok688bd472012-02-09 20:09:17 +09003303 private static class InputMethodAndSubtypeListManager {
3304 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003305 // Used to load label
satok688bd472012-02-09 20:09:17 +09003306 private final PackageManager mPm;
3307 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003308 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003309 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3310 mContext = context;
3311 mPm = context.getPackageManager();
3312 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003313 final Locale locale = context.getResources().getConfiguration().locale;
3314 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003315 }
3316
3317 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3318 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3319 new Comparator<InputMethodInfo>() {
3320 @Override
3321 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3322 if (imi2 == null) return 0;
3323 if (imi1 == null) return 1;
3324 if (mPm == null) {
3325 return imi1.getId().compareTo(imi2.getId());
3326 }
3327 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3328 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3329 return imiId1.toString().compareTo(imiId2.toString());
3330 }
3331 });
3332
3333 public ImeSubtypeListItem getNextInputMethod(
3334 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3335 if (imi == null) {
3336 return null;
3337 }
3338 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3339 if (imList.size() <= 1) {
3340 return null;
3341 }
3342 final int N = imList.size();
3343 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003344 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003345 : NOT_A_SUBTYPE_ID;
3346 for (int i = 0; i < N; ++i) {
3347 final ImeSubtypeListItem isli = imList.get(i);
3348 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3349 if (!onlyCurrentIme) {
3350 return imList.get((i + 1) % N);
3351 }
3352 for (int j = 0; j < N - 1; ++j) {
3353 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3354 if (candidate.mImi.equals(imi)) {
3355 return candidate;
3356 }
3357 }
3358 return null;
3359 }
3360 }
3361 return null;
3362 }
3363
3364 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3365 return getSortedInputMethodAndSubtypeList(true, false, false);
3366 }
3367
3368 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3369 boolean inputShown, boolean isScreenLocked) {
3370 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3371 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3372 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3373 if (immis == null || immis.size() == 0) {
3374 return Collections.emptyList();
3375 }
3376 mSortedImmis.clear();
3377 mSortedImmis.putAll(immis);
3378 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3379 if (imi == null) continue;
3380 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3381 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3382 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3383 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3384 }
3385 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3386 final CharSequence imeLabel = imi.loadLabel(mPm);
3387 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3388 final int subtypeCount = imi.getSubtypeCount();
3389 if (DEBUG) {
3390 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3391 }
3392 for (int j = 0; j < subtypeCount; ++j) {
3393 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3394 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3395 // We show all enabled IMEs and subtypes when an IME is shown.
3396 if (enabledSubtypeSet.contains(subtypeHashCode)
3397 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3398 final CharSequence subtypeLabel =
3399 subtype.overridesImplicitlyEnabledSubtype() ? null
3400 : subtype.getDisplayName(mContext, imi.getPackageName(),
3401 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003402 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3403 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003404
3405 // Removing this subtype from enabledSubtypeSet because we no longer
3406 // need to add an entry of this subtype to imList to avoid duplicated
3407 // entries.
3408 enabledSubtypeSet.remove(subtypeHashCode);
3409 }
3410 }
3411 } else {
satok93d744d2012-05-09 17:14:08 +09003412 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3413 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003414 }
3415 }
satok93d744d2012-05-09 17:14:08 +09003416 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003417 return imList;
3418 }
3419 }
3420
satokd87c2592010-09-29 11:52:06 +09003421 /**
3422 * Utility class for putting and getting settings for InputMethod
3423 * TODO: Move all putters and getters of settings to this class.
3424 */
3425 private static class InputMethodSettings {
3426 // The string for enabled input method is saved as follows:
3427 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3428 private static final char INPUT_METHOD_SEPARATER = ':';
3429 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003430 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003431 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3432
satok723a27e2010-11-11 14:58:11 +09003433 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003434 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3435
satokdf31ae62011-01-15 06:19:44 +09003436 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003437 private final ContentResolver mResolver;
3438 private final HashMap<String, InputMethodInfo> mMethodMap;
3439 private final ArrayList<InputMethodInfo> mMethodList;
3440
3441 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003442 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003443
3444 private static void buildEnabledInputMethodsSettingString(
3445 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3446 String id = pair.first;
3447 ArrayList<String> subtypes = pair.second;
3448 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003449 // Inputmethod and subtypes are saved in the settings as follows:
3450 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3451 for (String subtypeId: subtypes) {
3452 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003453 }
3454 }
3455
3456 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003457 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003458 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3459 int userId) {
3460 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003461 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003462 mResolver = resolver;
3463 mMethodMap = methodMap;
3464 mMethodList = methodList;
3465 }
3466
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003467 public void setCurrentUserId(int userId) {
3468 if (DEBUG) {
3469 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3470 + userId + ", new ime = " + getSelectedInputMethod());
3471 }
3472 // IMMS settings are kept per user, so keep track of current user
3473 mCurrentUserId = userId;
3474 }
3475
satokd87c2592010-09-29 11:52:06 +09003476 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3477 return createEnabledInputMethodListLocked(
3478 getEnabledInputMethodsAndSubtypeListLocked());
3479 }
3480
satok7f35c8c2010-10-07 21:13:11 +09003481 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003482 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3483 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003484 getEnabledInputMethodsAndSubtypeListLocked());
3485 }
3486
satok67ddf9c2010-11-17 09:45:54 +09003487 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3488 InputMethodInfo imi) {
3489 List<Pair<String, ArrayList<String>>> imsList =
3490 getEnabledInputMethodsAndSubtypeListLocked();
3491 ArrayList<InputMethodSubtype> enabledSubtypes =
3492 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003493 if (imi != null) {
3494 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3495 InputMethodInfo info = mMethodMap.get(imsPair.first);
3496 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003497 final int subtypeCount = info.getSubtypeCount();
3498 for (int i = 0; i < subtypeCount; ++i) {
3499 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003500 for (String s: imsPair.second) {
3501 if (String.valueOf(ims.hashCode()).equals(s)) {
3502 enabledSubtypes.add(ims);
3503 }
satok67ddf9c2010-11-17 09:45:54 +09003504 }
3505 }
satok884ef9a2010-11-18 10:39:46 +09003506 break;
satok67ddf9c2010-11-17 09:45:54 +09003507 }
satok67ddf9c2010-11-17 09:45:54 +09003508 }
3509 }
3510 return enabledSubtypes;
3511 }
3512
satokd87c2592010-09-29 11:52:06 +09003513 // At the initial boot, the settings for input methods are not set,
3514 // so we need to enable IME in that case.
3515 public void enableAllIMEsIfThereIsNoEnabledIME() {
3516 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3517 StringBuilder sb = new StringBuilder();
3518 final int N = mMethodList.size();
3519 for (int i = 0; i < N; i++) {
3520 InputMethodInfo imi = mMethodList.get(i);
3521 Slog.i(TAG, "Adding: " + imi.getId());
3522 if (i > 0) sb.append(':');
3523 sb.append(imi.getId());
3524 }
3525 putEnabledInputMethodsStr(sb.toString());
3526 }
3527 }
3528
satokbb4aa062011-01-19 21:40:27 +09003529 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003530 ArrayList<Pair<String, ArrayList<String>>> imsList
3531 = new ArrayList<Pair<String, ArrayList<String>>>();
3532 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3533 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3534 return imsList;
3535 }
satok723a27e2010-11-11 14:58:11 +09003536 mInputMethodSplitter.setString(enabledInputMethodsStr);
3537 while (mInputMethodSplitter.hasNext()) {
3538 String nextImsStr = mInputMethodSplitter.next();
3539 mSubtypeSplitter.setString(nextImsStr);
3540 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003541 ArrayList<String> subtypeHashes = new ArrayList<String>();
3542 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003543 String imeId = mSubtypeSplitter.next();
3544 while (mSubtypeSplitter.hasNext()) {
3545 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003546 }
3547 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3548 }
3549 }
3550 return imsList;
3551 }
3552
3553 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3554 if (reloadInputMethodStr) {
3555 getEnabledInputMethodsStr();
3556 }
3557 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3558 // Add in the newly enabled input method.
3559 putEnabledInputMethodsStr(id);
3560 } else {
3561 putEnabledInputMethodsStr(
3562 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3563 }
3564 }
3565
3566 /**
3567 * Build and put a string of EnabledInputMethods with removing specified Id.
3568 * @return the specified id was removed or not.
3569 */
3570 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3571 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3572 boolean isRemoved = false;
3573 boolean needsAppendSeparator = false;
3574 for (Pair<String, ArrayList<String>> ims: imsList) {
3575 String curId = ims.first;
3576 if (curId.equals(id)) {
3577 // We are disabling this input method, and it is
3578 // currently enabled. Skip it to remove from the
3579 // new list.
3580 isRemoved = true;
3581 } else {
3582 if (needsAppendSeparator) {
3583 builder.append(INPUT_METHOD_SEPARATER);
3584 } else {
3585 needsAppendSeparator = true;
3586 }
3587 buildEnabledInputMethodsSettingString(builder, ims);
3588 }
3589 }
3590 if (isRemoved) {
3591 // Update the setting with the new list of input methods.
3592 putEnabledInputMethodsStr(builder.toString());
3593 }
3594 return isRemoved;
3595 }
3596
3597 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3598 List<Pair<String, ArrayList<String>>> imsList) {
3599 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3600 for (Pair<String, ArrayList<String>> ims: imsList) {
3601 InputMethodInfo info = mMethodMap.get(ims.first);
3602 if (info != null) {
3603 res.add(info);
3604 }
3605 }
3606 return res;
3607 }
3608
satok7f35c8c2010-10-07 21:13:11 +09003609 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003610 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003611 List<Pair<String, ArrayList<String>>> imsList) {
3612 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3613 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3614 for (Pair<String, ArrayList<String>> ims : imsList) {
3615 InputMethodInfo info = mMethodMap.get(ims.first);
3616 if (info != null) {
3617 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3618 }
3619 }
3620 return res;
3621 }
3622
satokd87c2592010-09-29 11:52:06 +09003623 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003624 Settings.Secure.putStringForUser(
3625 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003626 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003627 if (DEBUG) {
3628 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3629 }
satokd87c2592010-09-29 11:52:06 +09003630 }
3631
3632 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003633 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3634 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003635 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003636 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3637 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003638 }
satokd87c2592010-09-29 11:52:06 +09003639 return mEnabledInputMethodsStrCache;
3640 }
satok723a27e2010-11-11 14:58:11 +09003641
3642 private void saveSubtypeHistory(
3643 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3644 StringBuilder builder = new StringBuilder();
3645 boolean isImeAdded = false;
3646 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3647 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3648 newSubtypeId);
3649 isImeAdded = true;
3650 }
3651 for (Pair<String, String> ime: savedImes) {
3652 String imeId = ime.first;
3653 String subtypeId = ime.second;
3654 if (TextUtils.isEmpty(subtypeId)) {
3655 subtypeId = NOT_A_SUBTYPE_ID_STR;
3656 }
3657 if (isImeAdded) {
3658 builder.append(INPUT_METHOD_SEPARATER);
3659 } else {
3660 isImeAdded = true;
3661 }
3662 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3663 subtypeId);
3664 }
3665 // Remove the last INPUT_METHOD_SEPARATER
3666 putSubtypeHistoryStr(builder.toString());
3667 }
3668
3669 public void addSubtypeToHistory(String imeId, String subtypeId) {
3670 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3671 for (Pair<String, String> ime: subtypeHistory) {
3672 if (ime.first.equals(imeId)) {
3673 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003674 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003675 + ime.second);
3676 }
3677 // We should break here
3678 subtypeHistory.remove(ime);
3679 break;
3680 }
3681 }
satokbb4aa062011-01-19 21:40:27 +09003682 if (DEBUG) {
3683 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3684 }
satok723a27e2010-11-11 14:58:11 +09003685 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3686 }
3687
3688 private void putSubtypeHistoryStr(String str) {
3689 if (DEBUG) {
3690 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3691 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003692 Settings.Secure.putStringForUser(
3693 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003694 }
3695
3696 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3697 // Gets the first one from the history
3698 return getLastSubtypeForInputMethodLockedInternal(null);
3699 }
3700
3701 public String getLastSubtypeForInputMethodLocked(String imeId) {
3702 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3703 if (ime != null) {
3704 return ime.second;
3705 } else {
3706 return null;
3707 }
3708 }
3709
3710 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3711 List<Pair<String, ArrayList<String>>> enabledImes =
3712 getEnabledInputMethodsAndSubtypeListLocked();
3713 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003714 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003715 final String imeInTheHistory = imeAndSubtype.first;
3716 // If imeId is empty, returns the first IME and subtype in the history
3717 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3718 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003719 final String subtypeHashCode =
3720 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3721 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003722 if (!TextUtils.isEmpty(subtypeHashCode)) {
3723 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003724 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003725 }
3726 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3727 }
3728 }
3729 }
3730 if (DEBUG) {
3731 Slog.d(TAG, "No enabled IME found in the history");
3732 }
3733 return null;
3734 }
3735
satokdf31ae62011-01-15 06:19:44 +09003736 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003737 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3738 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3739 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003740 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003741 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003742 if (explicitlyEnabledSubtypes.size() == 0) {
3743 // If there are no explicitly enabled subtypes, applicable subtypes are
3744 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003745 // If IME is enabled and no subtypes are enabled, applicable subtypes
3746 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003747 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003748 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003749 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003750 if (implicitlySelectedSubtypes != null) {
3751 final int N = implicitlySelectedSubtypes.size();
3752 for (int i = 0; i < N; ++i) {
3753 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3754 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3755 return subtypeHashCode;
3756 }
3757 }
3758 }
3759 }
3760 } else {
satokf6cafb62011-01-17 16:29:02 +09003761 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003762 if (s.equals(subtypeHashCode)) {
3763 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003764 try {
3765 final int hashCode = Integer.valueOf(subtypeHashCode);
3766 // Check whether the subtype id is valid or not
3767 if (isValidSubtypeId(imi, hashCode)) {
3768 return s;
3769 } else {
3770 return NOT_A_SUBTYPE_ID_STR;
3771 }
3772 } catch (NumberFormatException e) {
3773 return NOT_A_SUBTYPE_ID_STR;
3774 }
satokdf31ae62011-01-15 06:19:44 +09003775 }
satok723a27e2010-11-11 14:58:11 +09003776 }
3777 }
3778 // If imeId was enabled but subtypeId was disabled.
3779 return NOT_A_SUBTYPE_ID_STR;
3780 }
3781 }
3782 // If both imeId and subtypeId are disabled, return null
3783 return null;
3784 }
3785
3786 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3787 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3788 final String subtypeHistoryStr = getSubtypeHistoryStr();
3789 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3790 return imsList;
3791 }
3792 mInputMethodSplitter.setString(subtypeHistoryStr);
3793 while (mInputMethodSplitter.hasNext()) {
3794 String nextImsStr = mInputMethodSplitter.next();
3795 mSubtypeSplitter.setString(nextImsStr);
3796 if (mSubtypeSplitter.hasNext()) {
3797 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3798 // The first element is ime id.
3799 String imeId = mSubtypeSplitter.next();
3800 while (mSubtypeSplitter.hasNext()) {
3801 subtypeId = mSubtypeSplitter.next();
3802 break;
3803 }
3804 imsList.add(new Pair<String, String>(imeId, subtypeId));
3805 }
3806 }
3807 return imsList;
3808 }
3809
3810 private String getSubtypeHistoryStr() {
3811 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003812 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3813 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003814 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003815 return Settings.Secure.getStringForUser(
3816 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003817 }
3818
3819 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003820 if (DEBUG) {
3821 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3822 + mCurrentUserId);
3823 }
3824 Settings.Secure.putStringForUser(
3825 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003826 }
3827
3828 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003829 if (DEBUG) {
3830 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3831 + mCurrentUserId);
3832 }
3833 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3834 subtypeId, mCurrentUserId);
3835 }
3836
3837 public String getDisabledSystemInputMethods() {
3838 return Settings.Secure.getStringForUser(
3839 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3840 }
3841
3842 public String getSelectedInputMethod() {
3843 if (DEBUG) {
3844 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3845 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3846 + ", " + mCurrentUserId);
3847 }
3848 return Settings.Secure.getStringForUser(
3849 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3850 }
3851
3852 public int getSelectedInputMethodSubtypeHashCode() {
3853 try {
3854 return Settings.Secure.getIntForUser(
3855 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3856 } catch (SettingNotFoundException e) {
3857 return NOT_A_SUBTYPE_ID;
3858 }
3859 }
3860
3861 public int getCurrentUserId() {
3862 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003863 }
satokd87c2592010-09-29 11:52:06 +09003864 }
3865
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003866 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003867 private static class InputMethodFileManager {
3868 private static final String SYSTEM_PATH = "system";
3869 private static final String INPUT_METHOD_PATH = "inputmethod";
3870 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3871 private static final String NODE_SUBTYPES = "subtypes";
3872 private static final String NODE_SUBTYPE = "subtype";
3873 private static final String NODE_IMI = "imi";
3874 private static final String ATTR_ID = "id";
3875 private static final String ATTR_LABEL = "label";
3876 private static final String ATTR_ICON = "icon";
3877 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3878 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3879 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3880 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3881 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3882 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003883 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003884 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003885 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003886 if (methodMap == null) {
3887 throw new NullPointerException("methodMap is null");
3888 }
3889 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003890 final File systemDir = userId == UserHandle.USER_OWNER
3891 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3892 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003893 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3894 if (!inputMethodDir.mkdirs()) {
3895 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3896 }
3897 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3898 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3899 if (!subtypeFile.exists()) {
3900 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003901 writeAdditionalInputMethodSubtypes(
3902 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003903 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003904 readAdditionalInputMethodSubtypes(
3905 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003906 }
3907 }
3908
3909 private void deleteAllInputMethodSubtypes(String imiId) {
3910 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003911 mAdditionalSubtypesMap.remove(imiId);
3912 writeAdditionalInputMethodSubtypes(
3913 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003914 }
3915 }
3916
3917 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003918 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003919 synchronized (mMethodMap) {
3920 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3921 final int N = additionalSubtypes.length;
3922 for (int i = 0; i < N; ++i) {
3923 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003924 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003925 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003926 } else {
3927 Slog.w(TAG, "Duplicated subtype definition found: "
3928 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003929 }
3930 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003931 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3932 writeAdditionalInputMethodSubtypes(
3933 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003934 }
3935 }
3936
3937 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3938 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003939 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003940 }
3941 }
3942
3943 private static void writeAdditionalInputMethodSubtypes(
3944 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3945 HashMap<String, InputMethodInfo> methodMap) {
3946 // Safety net for the case that this function is called before methodMap is set.
3947 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3948 FileOutputStream fos = null;
3949 try {
3950 fos = subtypesFile.startWrite();
3951 final XmlSerializer out = new FastXmlSerializer();
3952 out.setOutput(fos, "utf-8");
3953 out.startDocument(null, true);
3954 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3955 out.startTag(null, NODE_SUBTYPES);
3956 for (String imiId : allSubtypes.keySet()) {
3957 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3958 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3959 continue;
3960 }
3961 out.startTag(null, NODE_IMI);
3962 out.attribute(null, ATTR_ID, imiId);
3963 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3964 final int N = subtypesList.size();
3965 for (int i = 0; i < N; ++i) {
3966 final InputMethodSubtype subtype = subtypesList.get(i);
3967 out.startTag(null, NODE_SUBTYPE);
3968 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3969 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3970 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3971 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3972 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3973 out.attribute(null, ATTR_IS_AUXILIARY,
3974 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3975 out.endTag(null, NODE_SUBTYPE);
3976 }
3977 out.endTag(null, NODE_IMI);
3978 }
3979 out.endTag(null, NODE_SUBTYPES);
3980 out.endDocument();
3981 subtypesFile.finishWrite(fos);
3982 } catch (java.io.IOException e) {
3983 Slog.w(TAG, "Error writing subtypes", e);
3984 if (fos != null) {
3985 subtypesFile.failWrite(fos);
3986 }
3987 }
3988 }
3989
3990 private static void readAdditionalInputMethodSubtypes(
3991 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3992 if (allSubtypes == null || subtypesFile == null) return;
3993 allSubtypes.clear();
3994 FileInputStream fis = null;
3995 try {
3996 fis = subtypesFile.openRead();
3997 final XmlPullParser parser = Xml.newPullParser();
3998 parser.setInput(fis, null);
3999 int type = parser.getEventType();
4000 // Skip parsing until START_TAG
4001 while ((type = parser.next()) != XmlPullParser.START_TAG
4002 && type != XmlPullParser.END_DOCUMENT) {}
4003 String firstNodeName = parser.getName();
4004 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4005 throw new XmlPullParserException("Xml doesn't start with subtypes");
4006 }
4007 final int depth =parser.getDepth();
4008 String currentImiId = null;
4009 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4010 while (((type = parser.next()) != XmlPullParser.END_TAG
4011 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4012 if (type != XmlPullParser.START_TAG)
4013 continue;
4014 final String nodeName = parser.getName();
4015 if (NODE_IMI.equals(nodeName)) {
4016 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4017 if (TextUtils.isEmpty(currentImiId)) {
4018 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4019 continue;
4020 }
4021 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4022 allSubtypes.put(currentImiId, tempSubtypesArray);
4023 } else if (NODE_SUBTYPE.equals(nodeName)) {
4024 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4025 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4026 continue;
4027 }
4028 final int icon = Integer.valueOf(
4029 parser.getAttributeValue(null, ATTR_ICON));
4030 final int label = Integer.valueOf(
4031 parser.getAttributeValue(null, ATTR_LABEL));
4032 final String imeSubtypeLocale =
4033 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4034 final String imeSubtypeMode =
4035 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4036 final String imeSubtypeExtraValue =
4037 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004038 final boolean isAuxiliary = "1".equals(String.valueOf(
4039 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004040 final InputMethodSubtype subtype =
4041 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4042 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4043 tempSubtypesArray.add(subtype);
4044 }
4045 }
4046 } catch (XmlPullParserException e) {
4047 Slog.w(TAG, "Error reading subtypes: " + e);
4048 return;
4049 } catch (java.io.IOException e) {
4050 Slog.w(TAG, "Error reading subtypes: " + e);
4051 return;
4052 } catch (NumberFormatException e) {
4053 Slog.w(TAG, "Error reading subtypes: " + e);
4054 return;
4055 } finally {
4056 if (fis != null) {
4057 try {
4058 fis.close();
4059 } catch (java.io.IOException e1) {
4060 Slog.w(TAG, "Failed to close.");
4061 }
4062 }
4063 }
4064 }
4065 }
4066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004068 // Utilities for debug
4069 private static String getStackTrace() {
4070 final StringBuilder sb = new StringBuilder();
4071 try {
4072 throw new RuntimeException();
4073 } catch (RuntimeException e) {
4074 final StackTraceElement[] frames = e.getStackTrace();
4075 // Start at 1 because the first frame is here and we don't care about it
4076 for (int j = 1; j < frames.length; ++j) {
4077 sb.append(frames[j].toString() + "\n");
4078 }
4079 }
4080 return sb.toString();
4081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 @Override
4084 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4085 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4086 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4089 + Binder.getCallingPid()
4090 + ", uid=" + Binder.getCallingUid());
4091 return;
4092 }
4093
4094 IInputMethod method;
4095 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 synchronized (mMethodMap) {
4100 p.println("Current Input Method Manager state:");
4101 int N = mMethodList.size();
4102 p.println(" Input Methods:");
4103 for (int i=0; i<N; i++) {
4104 InputMethodInfo info = mMethodList.get(i);
4105 p.println(" InputMethod #" + i + ":");
4106 info.dump(p, " ");
4107 }
4108 p.println(" Clients:");
4109 for (ClientState ci : mClients.values()) {
4110 p.println(" Client " + ci + ":");
4111 p.println(" client=" + ci.client);
4112 p.println(" inputContext=" + ci.inputContext);
4113 p.println(" sessionRequested=" + ci.sessionRequested);
4114 p.println(" curSession=" + ci.curSession);
4115 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004116 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004118 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4119 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4121 + " mBoundToMethod=" + mBoundToMethod);
4122 p.println(" mCurToken=" + mCurToken);
4123 p.println(" mCurIntent=" + mCurIntent);
4124 method = mCurMethod;
4125 p.println(" mCurMethod=" + mCurMethod);
4126 p.println(" mEnabledSession=" + mEnabledSession);
4127 p.println(" mShowRequested=" + mShowRequested
4128 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4129 + " mShowForced=" + mShowForced
4130 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004131 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004133
Jeff Brownb88102f2010-09-08 11:49:43 -07004134 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 pw.flush();
4137 try {
4138 client.client.asBinder().dump(fd, args);
4139 } catch (RemoteException e) {
4140 p.println("Input method client dead: " + e);
4141 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004142 } else {
4143 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004145
Jeff Brownb88102f2010-09-08 11:49:43 -07004146 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 pw.flush();
4149 try {
4150 method.asBinder().dump(fd, args);
4151 } catch (RemoteException e) {
4152 p.println("Input method service dead: " + e);
4153 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004154 } else {
4155 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 }
4157 }
4158}