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