blob: a296d34498dcc5a9250ea13e1a9e4673b3033973 [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 }
785 if (imi.getIsDefaultResourceId() != 0) {
786 try {
787 Resources res = context.createPackageContext(
788 imi.getPackageName(), 0).getResources();
789 if (res.getBoolean(imi.getIsDefaultResourceId())
790 && containsSubtypeOf(imi, context.getResources().getConfiguration().
791 locale.getLanguage())) {
792 return true;
793 }
794 } catch (PackageManager.NameNotFoundException ex) {
795 } catch (Resources.NotFoundException ex) {
796 }
797 }
798 if (imi.getSubtypeCount() == 0) {
799 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
800 }
801 return false;
802 }
803
804 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
805 if (!isSystemIme(imi)) {
806 return false;
807 }
808 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
809 }
810
811 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
812 final int N = imi.getSubtypeCount();
813 for (int i = 0; i < N; ++i) {
814 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
815 return true;
816 }
817 }
818 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820
821 @Override
822 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
823 throws RemoteException {
824 try {
825 return super.onTransact(code, data, reply, flags);
826 } catch (RuntimeException e) {
827 // The input method manager only throws security exceptions, so let's
828 // log all others.
829 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832 throw e;
833 }
834 }
835
Dianne Hackborn661cd522011-08-22 00:26:20 -0700836 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700837 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 if (DEBUG) {
839 Slog.d(TAG, "--- systemReady");
840 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700841 if (!mSystemReady) {
842 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900843 mKeyguardManager =
844 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700845 mNotificationManager = (NotificationManager)
846 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
847 mStatusBar = statusBar;
848 statusBar.setIconVisibility("ime", false);
849 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900850 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
851 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900852 if (mShowOngoingImeSwitcherForPhones) {
853 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
854 mHardKeyboardListener);
855 }
satok0a1bcf42012-05-16 19:26:31 +0900856 buildInputMethodListLocked(mMethodList, mMethodMap);
857 if (!mImeSelectedOnBoot) {
858 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
859 checkCurrentLocaleChangedLocked();
860 }
861 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700862 try {
863 startInputInnerLocked();
864 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800865 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700866 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700867 }
868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
satok15452a42011-10-28 17:58:28 +0900871 private void setImeWindowVisibilityStatusHiddenLocked() {
872 mImeWindowVis = 0;
873 updateImeWindowStatusLocked();
874 }
875
satok3afd6c02011-11-18 08:38:19 +0900876 private void refreshImeWindowVisibilityLocked() {
877 final Configuration conf = mRes.getConfiguration();
878 final boolean haveHardKeyboard = conf.keyboard
879 != Configuration.KEYBOARD_NOKEYS;
880 final boolean hardKeyShown = haveHardKeyboard
881 && conf.hardKeyboardHidden
882 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500883 final boolean isScreenLocked =
884 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
885 final boolean isScreenSecurelyLocked =
886 isScreenLocked && mKeyguardManager.isKeyguardSecure();
887 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
888 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900889 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
890 updateImeWindowStatusLocked();
891 }
892
satok15452a42011-10-28 17:58:28 +0900893 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900894 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700895 }
896
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 // ---------------------------------------------------------------------------------------
898 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
899 // 1) it comes from the system process
900 // 2) the calling process' user id is identical to the current user id IMMS thinks.
901 private boolean calledFromValidUser() {
902 final int uid = Binder.getCallingUid();
903 final int userId = UserHandle.getUserId(uid);
904 if (DEBUG) {
905 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
906 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
907 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900908 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900909 }
910 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
911 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900913
914 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
915 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
916 // must not manage background users' states in any functions.
917 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
918 // by a token.
919 if (mContext.checkCallingOrSelfPermission(
920 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
921 == PackageManager.PERMISSION_GRANTED) {
922 if (DEBUG) {
923 Slog.d(TAG, "--- Access granted because the calling process has "
924 + "the INTERACT_ACROSS_USERS_FULL permission");
925 }
926 return true;
927 }
928 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
929 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 }
931
932 private boolean bindCurrentInputMethodService(
933 Intent service, ServiceConnection conn, int flags) {
934 if (service == null || conn == null) {
935 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
936 return false;
937 }
938 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
939 }
940
satoke7c6998e2011-06-03 17:57:59 +0900941 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900943 // TODO: Make this work even for non-current users?
944 if (!calledFromValidUser()) {
945 return Collections.emptyList();
946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 synchronized (mMethodMap) {
948 return new ArrayList<InputMethodInfo>(mMethodList);
949 }
950 }
951
satoke7c6998e2011-06-03 17:57:59 +0900952 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900954 // TODO: Make this work even for non-current users?
955 if (!calledFromValidUser()) {
956 return Collections.emptyList();
957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900959 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961 }
962
satokbb4aa062011-01-19 21:40:27 +0900963 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
964 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
965 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
966 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900967 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900968 enabledInputMethodAndSubtypes.put(
969 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
970 }
971 return enabledInputMethodAndSubtypes;
972 }
973
974 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
975 boolean allowsImplicitlySelectedSubtypes) {
976 if (imi == null && mCurMethodId != null) {
977 imi = mMethodMap.get(mCurMethodId);
978 }
satok7265d9b2011-02-14 15:47:30 +0900979 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900980 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900981 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900982 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900983 }
satok7265d9b2011-02-14 15:47:30 +0900984 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900985 }
986
satoke7c6998e2011-06-03 17:57:59 +0900987 @Override
satok16331c82010-12-20 23:48:46 +0900988 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
989 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900990 // TODO: Make this work even for non-current users?
991 if (!calledFromValidUser()) {
992 return Collections.emptyList();
993 }
satok67ddf9c2010-11-17 09:45:54 +0900994 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900995 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900996 }
997 }
998
satoke7c6998e2011-06-03 17:57:59 +0900999 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 public void addClient(IInputMethodClient client,
1001 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001002 if (!calledFromValidUser()) {
1003 return;
1004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 synchronized (mMethodMap) {
1006 mClients.put(client.asBinder(), new ClientState(client,
1007 inputContext, uid, pid));
1008 }
1009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
satoke7c6998e2011-06-03 17:57:59 +09001011 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001013 if (!calledFromValidUser()) {
1014 return;
1015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 synchronized (mMethodMap) {
1017 mClients.remove(client.asBinder());
1018 }
1019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 void executeOrSendMessage(IInterface target, Message msg) {
1022 if (target.asBinder() instanceof Binder) {
1023 mCaller.sendMessage(msg);
1024 } else {
1025 handleMessage(msg);
1026 msg.recycle();
1027 }
1028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001029
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001030 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001032 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 + mCurClient.client.asBinder());
1034 if (mBoundToMethod) {
1035 mBoundToMethod = false;
1036 if (mCurMethod != null) {
1037 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1038 MSG_UNBIND_INPUT, mCurMethod));
1039 }
1040 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001041
1042 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1043 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1045 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1046 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
The Android Open Source Project10592532009-03-18 17:39:46 -07001049 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 }
1051 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 private int getImeShowFlags() {
1054 int flags = 0;
1055 if (mShowForced) {
1056 flags |= InputMethod.SHOW_FORCED
1057 | InputMethod.SHOW_EXPLICIT;
1058 } else if (mShowExplicitlyRequested) {
1059 flags |= InputMethod.SHOW_EXPLICIT;
1060 }
1061 return flags;
1062 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 private int getAppShowFlags() {
1065 int flags = 0;
1066 if (mShowForced) {
1067 flags |= InputMethodManager.SHOW_FORCED;
1068 } else if (!mShowExplicitlyRequested) {
1069 flags |= InputMethodManager.SHOW_IMPLICIT;
1070 }
1071 return flags;
1072 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001073
Dianne Hackborn7663d802012-02-24 13:08:49 -08001074 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 if (!mBoundToMethod) {
1076 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1077 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1078 mBoundToMethod = true;
1079 }
1080 final SessionState session = mCurClient.curSession;
1081 if (initial) {
1082 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1083 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1084 } else {
1085 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1086 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1087 }
1088 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001089 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001090 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001092 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001096 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 // If no method is currently selected, do nothing.
1098 if (mCurMethodId == null) {
1099 return mNoBinding;
1100 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 ClientState cs = mClients.get(client.asBinder());
1103 if (cs == null) {
1104 throw new IllegalArgumentException("unknown client "
1105 + client.asBinder());
1106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 try {
1109 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1110 // Check with the window manager to make sure this client actually
1111 // has a window with focus. If not, reject. This is thread safe
1112 // because if the focus changes some time before or after, the
1113 // next client receiving focus that has any interest in input will
1114 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1117 return null;
1118 }
1119 } catch (RemoteException e) {
1120 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001121
Dianne Hackborn7663d802012-02-24 13:08:49 -08001122 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1123 }
1124
1125 InputBindResult startInputUncheckedLocked(ClientState cs,
1126 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1127 // If no method is currently selected, do nothing.
1128 if (mCurMethodId == null) {
1129 return mNoBinding;
1130 }
1131
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001132 if (mCurClient == null) {
1133 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1134 if (DEBUG) {
1135 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1136 }
1137 }
1138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 if (mCurClient != cs) {
1140 // If the client is changing, we need to switch over to the new
1141 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001142 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001143 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 + cs.client.asBinder());
1145
1146 // If the screen is on, inform the new client it is active
1147 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001148 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1149 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 // Bump up the sequence for this client and attach it.
1154 mCurSeq++;
1155 if (mCurSeq <= 0) mCurSeq = 1;
1156 mCurClient = cs;
1157 mCurInputContext = inputContext;
1158 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 // Check if the input method is changing.
1161 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1162 if (cs.curSession != null) {
1163 // Fast case: if we are already connected to the input method,
1164 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001165 return attachNewInputLocked(
1166 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168 if (mHaveConnection) {
1169 if (mCurMethod != null) {
1170 if (!cs.sessionRequested) {
1171 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1174 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001175 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 // Return to client, and we will get back with it when
1178 // we have had a session made for it.
1179 return new InputBindResult(null, mCurId, mCurSeq);
1180 } else if (SystemClock.uptimeMillis()
1181 < (mLastBindTime+TIME_TO_RECONNECT)) {
1182 // In this case we have connected to the service, but
1183 // don't yet have its interface. If it hasn't been too
1184 // long since we did the connection, we'll return to
1185 // the client and wait to get the service interface so
1186 // we can report back. If it has been too long, we want
1187 // to fall through so we can try a disconnect/reconnect
1188 // to see if we can get back in touch with the service.
1189 return new InputBindResult(null, mCurId, mCurSeq);
1190 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1192 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194 }
1195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001197 return startInputInnerLocked();
1198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001200 InputBindResult startInputInnerLocked() {
1201 if (mCurMethodId == null) {
1202 return mNoBinding;
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001205 if (!mSystemReady) {
1206 // If the system is not yet ready, we shouldn't be running third
1207 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001208 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1212 if (info == null) {
1213 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001216 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1219 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001220 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1221 com.android.internal.R.string.input_method_binding_label);
1222 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1223 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001224 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001225 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mLastBindTime = SystemClock.uptimeMillis();
1227 mHaveConnection = true;
1228 mCurId = info.getId();
1229 mCurToken = new Binder();
1230 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001231 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 mIWindowManager.addWindowToken(mCurToken,
1233 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1234 } catch (RemoteException e) {
1235 }
1236 return new InputBindResult(null, mCurId, mCurSeq);
1237 } else {
1238 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001239 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 + mCurIntent);
1241 }
1242 return null;
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
satoke7c6998e2011-06-03 17:57:59 +09001245 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001247 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001248 if (!calledFromValidUser()) {
1249 return null;
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 synchronized (mMethodMap) {
1252 final long ident = Binder.clearCallingIdentity();
1253 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001254 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 } finally {
1256 Binder.restoreCallingIdentity(ident);
1257 }
1258 }
1259 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001260
satoke7c6998e2011-06-03 17:57:59 +09001261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 public void finishInput(IInputMethodClient client) {
1263 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
satoke7c6998e2011-06-03 17:57:59 +09001265 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 public void onServiceConnected(ComponentName name, IBinder service) {
1267 synchronized (mMethodMap) {
1268 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1269 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001270 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001271 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001272 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001273 return;
1274 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001275 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001276 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1277 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001279 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001280 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001282 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001283 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
1286 }
1287 }
1288
1289 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1290 synchronized (mMethodMap) {
1291 if (mCurMethod != null && method != null
1292 && mCurMethod.asBinder() == method.asBinder()) {
1293 if (mCurClient != null) {
1294 mCurClient.curSession = new SessionState(mCurClient,
1295 method, session);
1296 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001297 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if (res.method != null) {
1299 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1300 MSG_BIND_METHOD, mCurClient.client, res));
1301 }
1302 }
1303 }
1304 }
1305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001307 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001308 if (mVisibleBound) {
1309 mContext.unbindService(mVisibleConnection);
1310 mVisibleBound = false;
1311 }
1312
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001313 if (mHaveConnection) {
1314 mContext.unbindService(this);
1315 mHaveConnection = false;
1316 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001318 if (mCurToken != null) {
1319 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001320 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001321 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001322 // The current IME is shown. Hence an IME switch (transition) is happening.
1323 mWindowManagerService.saveLastInputMethodWindowForTransition();
1324 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001325 mIWindowManager.removeWindowToken(mCurToken);
1326 } catch (RemoteException e) {
1327 }
1328 mCurToken = null;
1329 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001330
The Android Open Source Project10592532009-03-18 17:39:46 -07001331 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001332 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001333
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001334 if (reportToClient && mCurClient != null) {
1335 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1336 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1337 }
1338 }
1339
Devin Taylor0c33ed22010-02-23 13:26:46 -06001340 private void finishSession(SessionState sessionState) {
1341 if (sessionState != null && sessionState.session != null) {
1342 try {
1343 sessionState.session.finishSession();
1344 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001345 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001346 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001347 }
1348 }
1349 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001350
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001351 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 if (mCurMethod != null) {
1353 for (ClientState cs : mClients.values()) {
1354 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001355 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 cs.curSession = null;
1357 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001358
1359 finishSession(mEnabledSession);
1360 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 mCurMethod = null;
1362 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001363 if (mStatusBar != null) {
1364 mStatusBar.setIconVisibility("ime", false);
1365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
satoke7c6998e2011-06-03 17:57:59 +09001368 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 public void onServiceDisconnected(ComponentName name) {
1370 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001371 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 + " mCurIntent=" + mCurIntent);
1373 if (mCurMethod != null && mCurIntent != null
1374 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001375 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 // We consider this to be a new bind attempt, since the system
1377 // should now try to restart the service for us.
1378 mLastBindTime = SystemClock.uptimeMillis();
1379 mShowRequested = mInputShown;
1380 mInputShown = false;
1381 if (mCurClient != null) {
1382 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1383 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1384 }
1385 }
1386 }
1387 }
1388
satokf9f01002011-05-19 21:31:50 +09001389 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001391 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 long ident = Binder.clearCallingIdentity();
1393 try {
1394 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001395 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 return;
1397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 synchronized (mMethodMap) {
1400 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001401 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001402 if (mStatusBar != null) {
1403 mStatusBar.setIconVisibility("ime", false);
1404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001406 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001407 CharSequence contentDescription = null;
1408 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001409 // Use PackageManager to load label
1410 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001411 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001412 mIPackageManager.getApplicationInfo(packageName, 0,
1413 mSettings.getCurrentUserId()));
1414 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001415 /* ignore */
1416 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001417 if (mStatusBar != null) {
1418 mStatusBar.setIcon("ime", packageName, iconId, 0,
1419 contentDescription != null
1420 ? contentDescription.toString() : null);
1421 mStatusBar.setIconVisibility("ime", true);
1422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 }
1425 } finally {
1426 Binder.restoreCallingIdentity(ident);
1427 }
1428 }
1429
satok7cfc0ed2011-06-20 21:29:36 +09001430 private boolean needsToShowImeSwitchOngoingNotification() {
1431 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001432 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001433 synchronized (mMethodMap) {
1434 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1435 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001436 if (N > 2) return true;
1437 if (N < 1) return false;
1438 int nonAuxCount = 0;
1439 int auxCount = 0;
1440 InputMethodSubtype nonAuxSubtype = null;
1441 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001442 for(int i = 0; i < N; ++i) {
1443 final InputMethodInfo imi = imis.get(i);
1444 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1445 imi, true);
1446 final int subtypeCount = subtypes.size();
1447 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001448 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001449 } else {
1450 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001451 final InputMethodSubtype subtype = subtypes.get(j);
1452 if (!subtype.isAuxiliary()) {
1453 ++nonAuxCount;
1454 nonAuxSubtype = subtype;
1455 } else {
1456 ++auxCount;
1457 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001458 }
1459 }
1460 }
satok7cfc0ed2011-06-20 21:29:36 +09001461 }
satokb6359412011-06-28 17:47:41 +09001462 if (nonAuxCount > 1 || auxCount > 1) {
1463 return true;
1464 } else if (nonAuxCount == 1 && auxCount == 1) {
1465 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001466 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1467 || auxSubtype.overridesImplicitlyEnabledSubtype()
1468 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001469 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1470 return false;
1471 }
1472 return true;
1473 }
1474 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001475 }
satok7cfc0ed2011-06-20 21:29:36 +09001476 }
1477
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001478 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001479 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001480 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001481 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001482 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001483 try {
1484 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001485 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001486 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001487 return;
1488 }
1489
1490 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001491 mImeWindowVis = vis;
1492 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001493 if (mStatusBar != null) {
1494 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1495 }
satok7cfc0ed2011-06-20 21:29:36 +09001496 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001497 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1498 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001499 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001500 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001501 final CharSequence title = mRes.getText(
1502 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001503 final CharSequence imiLabel = imi.loadLabel(pm);
1504 final CharSequence summary = mCurrentSubtype != null
1505 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1506 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1507 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001508 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001509 : imiLabel;
1510
satok7cfc0ed2011-06-20 21:29:36 +09001511 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001512 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001513 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001514 if (DEBUG) {
1515 Slog.d(TAG, "--- show notification: label = " + imiLabel
1516 + ", summary = " + summary);
1517 }
1518 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001519 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001520 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001521 mNotificationShown = true;
1522 }
satok7cfc0ed2011-06-20 21:29:36 +09001523 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001524 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001525 if (DEBUG) {
1526 Slog.d(TAG, "--- hide notification");
1527 }
1528 mNotificationManager.cancelAsUser(null,
1529 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001530 mNotificationShown = false;
1531 }
1532 }
satok06487a52010-10-29 11:37:18 +09001533 }
1534 } finally {
1535 Binder.restoreCallingIdentity(ident);
1536 }
1537 }
1538
satoke7c6998e2011-06-03 17:57:59 +09001539 @Override
satokf9f01002011-05-19 21:31:50 +09001540 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001541 if (!calledFromValidUser()) {
1542 return;
1543 }
satokf9f01002011-05-19 21:31:50 +09001544 synchronized (mMethodMap) {
1545 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1546 for (int i = 0; i < spans.length; ++i) {
1547 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001548 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001549 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001550 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001551 }
1552 }
1553 }
1554 }
1555
satoke7c6998e2011-06-03 17:57:59 +09001556 @Override
satokf9f01002011-05-19 21:31:50 +09001557 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001558 if (!calledFromValidUser()) {
1559 return false;
1560 }
satokf9f01002011-05-19 21:31:50 +09001561 synchronized (mMethodMap) {
1562 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1563 // TODO: Do not send the intent if the process of the targetImi is already dead.
1564 if (targetImi != null) {
1565 final String[] suggestions = span.getSuggestions();
1566 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001567 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001568 final Intent intent = new Intent();
1569 // Ensures that only a class in the original IME package will receive the
1570 // notification.
satok42c5a162011-05-26 16:46:14 +09001571 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001572 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1573 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1574 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1575 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001576 final long ident = Binder.clearCallingIdentity();
1577 try {
1578 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1579 } finally {
1580 Binder.restoreCallingIdentity(ident);
1581 }
satokf9f01002011-05-19 21:31:50 +09001582 return true;
1583 }
1584 }
1585 return false;
1586 }
1587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001589 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1590 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1591 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1592 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001593 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001594 // There is no input method selected, try to choose new applicable input method.
1595 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001596 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001597 }
1598 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 try {
satokab751aa2010-09-14 19:17:36 +09001600 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001602 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001603 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001604 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 }
satokf3db1af2010-11-23 13:34:33 +09001606 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001607 } else {
1608 // There is no longer an input method set, so stop any current one.
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 }
1612 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001613
satokab751aa2010-09-14 19:17:36 +09001614 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 InputMethodInfo info = mMethodMap.get(id);
1616 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001617 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
satokd81e9502012-05-21 12:58:45 +09001620 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001622 final int subtypeCount = info.getSubtypeCount();
1623 if (subtypeCount <= 0) {
1624 return;
satokcd7cd292010-11-20 15:46:23 +09001625 }
satokd81e9502012-05-21 12:58:45 +09001626 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1627 final InputMethodSubtype newSubtype;
1628 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1629 newSubtype = info.getSubtypeAt(subtypeId);
1630 } else {
1631 // If subtype is null, try to find the most applicable one from
1632 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001633 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001634 }
1635 if (newSubtype == null || oldSubtype == null) {
1636 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1637 + ", new subtype = " + newSubtype);
1638 return;
1639 }
1640 if (newSubtype != oldSubtype) {
1641 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1642 if (mCurMethod != null) {
1643 try {
1644 refreshImeWindowVisibilityLocked();
1645 mCurMethod.changeInputMethodSubtype(newSubtype);
1646 } catch (RemoteException e) {
1647 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001648 }
1649 }
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 return;
1652 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001653
satokd81e9502012-05-21 12:58:45 +09001654 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 final long ident = Binder.clearCallingIdentity();
1656 try {
satokab751aa2010-09-14 19:17:36 +09001657 // Set a subtype to this input method.
1658 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001659 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1660 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1661 // because mCurMethodId is stored as a history in
1662 // setSelectedInputMethodAndSubtypeLocked().
1663 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664
1665 if (ActivityManagerNative.isSystemReady()) {
1666 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001667 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001669 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001671 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 } finally {
1673 Binder.restoreCallingIdentity(ident);
1674 }
1675 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001676
satok42c5a162011-05-26 16:46:14 +09001677 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001678 public boolean showSoftInput(IInputMethodClient client, int flags,
1679 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001680 if (!calledFromValidUser()) {
1681 return false;
1682 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001683 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 long ident = Binder.clearCallingIdentity();
1685 try {
1686 synchronized (mMethodMap) {
1687 if (mCurClient == null || client == null
1688 || mCurClient.client.asBinder() != client.asBinder()) {
1689 try {
1690 // We need to check if this is the current client with
1691 // focus in the window manager, to allow this call to
1692 // be made before input is started in it.
1693 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001694 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001695 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 }
1700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701
Joe Onorato8a9b2202010-02-26 18:56:32 -08001702 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001703 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705 } finally {
1706 Binder.restoreCallingIdentity(ident);
1707 }
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 mShowRequested = true;
1712 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1713 mShowExplicitlyRequested = true;
1714 }
1715 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1716 mShowExplicitlyRequested = true;
1717 mShowForced = true;
1718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001719
Dianne Hackborncc278702009-09-02 23:07:23 -07001720 if (!mSystemReady) {
1721 return false;
1722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001723
The Android Open Source Project4df24232009-03-05 14:34:35 -08001724 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001726 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1727 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1728 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001730 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001731 bindCurrentInputMethodService(
1732 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001733 mVisibleBound = true;
1734 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001737 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 // The client has asked to have the input method shown, but
1739 // we have been sitting here too long with a connection to the
1740 // service and no interface received, so let's disconnect/connect
1741 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001744 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001746 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001747 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001748 } else {
1749 if (DEBUG) {
1750 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1751 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001754
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001757
satok42c5a162011-05-26 16:46:14 +09001758 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001759 public boolean hideSoftInput(IInputMethodClient client, int flags,
1760 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001761 if (!calledFromValidUser()) {
1762 return false;
1763 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001764 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 long ident = Binder.clearCallingIdentity();
1766 try {
1767 synchronized (mMethodMap) {
1768 if (mCurClient == null || client == null
1769 || mCurClient.client.asBinder() != client.asBinder()) {
1770 try {
1771 // We need to check if this is the current client with
1772 // focus in the window manager, to allow this call to
1773 // be made before input is started in it.
1774 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001775 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1776 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001777 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001778 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001781 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785
Joe Onorato8a9b2202010-02-26 18:56:32 -08001786 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 } finally {
1790 Binder.restoreCallingIdentity(ident);
1791 }
1792 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001793
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1796 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001797 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001799 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
1801 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001802 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001804 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001806 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001808 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1809 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1810 res = true;
1811 } else {
1812 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001814 if (mHaveConnection && mVisibleBound) {
1815 mContext.unbindService(mVisibleConnection);
1816 mVisibleBound = false;
1817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 mInputShown = false;
1819 mShowRequested = false;
1820 mShowExplicitlyRequested = false;
1821 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001822 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001824
satok42c5a162011-05-26 16:46:14 +09001825 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001826 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1827 int controlFlags, int softInputMode, int windowFlags,
1828 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001829 // Needs to check the validity before clearing calling identity
1830 final boolean calledFromValidUser = calledFromValidUser();
1831
Dianne Hackborn7663d802012-02-24 13:08:49 -08001832 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 long ident = Binder.clearCallingIdentity();
1834 try {
1835 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001836 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001837 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001839 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001840
Dianne Hackborn7663d802012-02-24 13:08:49 -08001841 ClientState cs = mClients.get(client.asBinder());
1842 if (cs == null) {
1843 throw new IllegalArgumentException("unknown client "
1844 + client.asBinder());
1845 }
1846
1847 try {
1848 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1849 // Check with the window manager to make sure this client actually
1850 // has a window with focus. If not, reject. This is thread safe
1851 // because if the focus changes some time before or after, the
1852 // next client receiving focus that has any interest in input will
1853 // be calling through here after that change happens.
1854 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1855 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1856 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001858 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001860
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001861 if (!calledFromValidUser) {
1862 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1863 Slog.w(TAG, "If you want to interect with IME, you need "
1864 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1865 hideCurrentInputLocked(0, null);
1866 return null;
1867 }
1868
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001869 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001870 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001871 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001872 if (attribute != null) {
1873 return startInputUncheckedLocked(cs, inputContext, attribute,
1874 controlFlags);
1875 }
1876 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001877 }
1878 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001879
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001880 // Should we auto-show the IME even if the caller has not
1881 // specified what should be done with it?
1882 // We only do this automatically if the window can resize
1883 // to accommodate the IME (so what the user sees will give
1884 // them good context without input information being obscured
1885 // by the IME) or if running on a large screen where there
1886 // is more room for the target window + IME.
1887 final boolean doAutoShow =
1888 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1889 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1890 || mRes.getConfiguration().isLayoutSizeAtLeast(
1891 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001892 final boolean isTextEditor =
1893 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1894
1895 // We want to start input before showing the IME, but after closing
1896 // it. We want to do this after closing it to help the IME disappear
1897 // more quickly (not get stuck behind it initializing itself for the
1898 // new focused input, even if its window wants to hide the IME).
1899 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1902 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001903 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1905 // There is no focus view, and this window will
1906 // be behind any soft input window, so hide the
1907 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001908 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001909 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001911 } else if (isTextEditor && doAutoShow && (softInputMode &
1912 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 // There is a focus view, and we are navigating forward
1914 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001915 // We only do this automatically if the window can resize
1916 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001917 // them good context without input information being obscured
1918 // by the IME) or if running on a large screen where there
1919 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001920 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001921 if (attribute != null) {
1922 res = startInputUncheckedLocked(cs, inputContext, attribute,
1923 controlFlags);
1924 didStart = true;
1925 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001926 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928 break;
1929 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1930 // Do nothing.
1931 break;
1932 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1933 if ((softInputMode &
1934 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001935 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001936 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
1938 break;
1939 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001940 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001941 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 break;
1943 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1944 if ((softInputMode &
1945 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001947 if (attribute != null) {
1948 res = startInputUncheckedLocked(cs, inputContext, attribute,
1949 controlFlags);
1950 didStart = true;
1951 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001952 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 break;
1955 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001956 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001957 if (attribute != null) {
1958 res = startInputUncheckedLocked(cs, inputContext, attribute,
1959 controlFlags);
1960 didStart = true;
1961 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001962 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 break;
1964 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001965
1966 if (!didStart && attribute != null) {
1967 res = startInputUncheckedLocked(cs, inputContext, attribute,
1968 controlFlags);
1969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971 } finally {
1972 Binder.restoreCallingIdentity(ident);
1973 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001974
1975 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001977
satok42c5a162011-05-26 16:46:14 +09001978 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001980 if (!calledFromValidUser()) {
1981 return;
1982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 synchronized (mMethodMap) {
1984 if (mCurClient == null || client == null
1985 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001986 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001987 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
1989
satok440aab52010-11-25 09:43:11 +09001990 // Always call subtype picker, because subtype picker is a superset of input method
1991 // picker.
satokab751aa2010-09-14 19:17:36 +09001992 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1993 }
1994 }
1995
satok42c5a162011-05-26 16:46:14 +09001996 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001998 if (!calledFromValidUser()) {
1999 return;
2000 }
satok28203512010-11-24 11:06:49 +09002001 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2002 }
2003
satok42c5a162011-05-26 16:46:14 +09002004 @Override
satok28203512010-11-24 11:06:49 +09002005 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002006 if (!calledFromValidUser()) {
2007 return;
2008 }
satok28203512010-11-24 11:06:49 +09002009 synchronized (mMethodMap) {
2010 if (subtype != null) {
2011 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
2012 mMethodMap.get(id), subtype.hashCode()));
2013 } else {
2014 setInputMethod(token, id);
2015 }
2016 }
satokab751aa2010-09-14 19:17:36 +09002017 }
2018
satok42c5a162011-05-26 16:46:14 +09002019 @Override
satokb416a712010-11-25 20:42:14 +09002020 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002021 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002022 if (!calledFromValidUser()) {
2023 return;
2024 }
satokb416a712010-11-25 20:42:14 +09002025 synchronized (mMethodMap) {
2026 if (mCurClient == null || client == null
2027 || mCurClient.client.asBinder() != client.asBinder()) {
2028 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2029 }
satok7fee71f2010-12-17 18:54:26 +09002030 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2031 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002032 }
2033 }
2034
satok4fc87d62011-05-20 16:13:43 +09002035 @Override
satok735cf382010-11-11 20:40:09 +09002036 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002037 if (!calledFromValidUser()) {
2038 return false;
2039 }
satok735cf382010-11-11 20:40:09 +09002040 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002041 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002042 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002043 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002044 lastImi = mMethodMap.get(lastIme.first);
2045 } else {
2046 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002047 }
satok4fc87d62011-05-20 16:13:43 +09002048 String targetLastImiId = null;
2049 int subtypeId = NOT_A_SUBTYPE_ID;
2050 if (lastIme != null && lastImi != null) {
2051 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2052 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2053 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2054 : mCurrentSubtype.hashCode();
2055 // If the last IME is the same as the current IME and the last subtype is not
2056 // defined, there is no need to switch to the last IME.
2057 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2058 targetLastImiId = lastIme.first;
2059 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2060 }
2061 }
2062
2063 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2064 // This is a safety net. If the currentSubtype can't be added to the history
2065 // and the framework couldn't find the last ime, we will make the last ime be
2066 // the most applicable enabled keyboard subtype of the system imes.
2067 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2068 if (enabled != null) {
2069 final int N = enabled.size();
2070 final String locale = mCurrentSubtype == null
2071 ? mRes.getConfiguration().locale.toString()
2072 : mCurrentSubtype.getLocale();
2073 for (int i = 0; i < N; ++i) {
2074 final InputMethodInfo imi = enabled.get(i);
2075 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2076 InputMethodSubtype keyboardSubtype =
2077 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2078 SUBTYPE_MODE_KEYBOARD, locale, true);
2079 if (keyboardSubtype != null) {
2080 targetLastImiId = imi.getId();
2081 subtypeId = getSubtypeIdFromHashCode(
2082 imi, keyboardSubtype.hashCode());
2083 if(keyboardSubtype.getLocale().equals(locale)) {
2084 break;
2085 }
2086 }
2087 }
2088 }
2089 }
2090 }
2091
2092 if (!TextUtils.isEmpty(targetLastImiId)) {
2093 if (DEBUG) {
2094 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2095 + ", from: " + mCurMethodId + ", " + subtypeId);
2096 }
2097 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2098 return true;
2099 } else {
2100 return false;
2101 }
satok735cf382010-11-11 20:40:09 +09002102 }
2103 }
2104
satoke7c6998e2011-06-03 17:57:59 +09002105 @Override
satok688bd472012-02-09 20:09:17 +09002106 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002107 if (!calledFromValidUser()) {
2108 return false;
2109 }
satok688bd472012-02-09 20:09:17 +09002110 synchronized (mMethodMap) {
2111 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2112 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2113 if (nextSubtype == null) {
2114 return false;
2115 }
2116 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2117 return true;
2118 }
2119 }
2120
2121 @Override
satok68f1b782011-04-11 14:26:04 +09002122 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002123 if (!calledFromValidUser()) {
2124 return null;
2125 }
satok68f1b782011-04-11 14:26:04 +09002126 synchronized (mMethodMap) {
2127 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2128 // TODO: Handle the case of the last IME with no subtypes
2129 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2130 || TextUtils.isEmpty(lastIme.second)) return null;
2131 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2132 if (lastImi == null) return null;
2133 try {
2134 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002135 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2136 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2137 return null;
2138 }
2139 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002140 } catch (NumberFormatException e) {
2141 return null;
2142 }
2143 }
2144 }
2145
satoke7c6998e2011-06-03 17:57:59 +09002146 @Override
satokee5e77c2011-09-02 18:50:15 +09002147 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002148 if (!calledFromValidUser()) {
2149 return;
2150 }
satok91e88122011-07-18 11:11:42 +09002151 // By this IPC call, only a process which shares the same uid with the IME can add
2152 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002153 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002154 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002155 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002156 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002157 final String[] packageInfos;
2158 try {
2159 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2160 } catch (RemoteException e) {
2161 Slog.e(TAG, "Failed to get package infos");
2162 return;
2163 }
satok91e88122011-07-18 11:11:42 +09002164 if (packageInfos != null) {
2165 final int packageNum = packageInfos.length;
2166 for (int i = 0; i < packageNum; ++i) {
2167 if (packageInfos[i].equals(imi.getPackageName())) {
2168 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002169 final long ident = Binder.clearCallingIdentity();
2170 try {
2171 buildInputMethodListLocked(mMethodList, mMethodMap);
2172 } finally {
2173 Binder.restoreCallingIdentity(ident);
2174 }
satokee5e77c2011-09-02 18:50:15 +09002175 return;
satok91e88122011-07-18 11:11:42 +09002176 }
2177 }
2178 }
satoke7c6998e2011-06-03 17:57:59 +09002179 }
satokee5e77c2011-09-02 18:50:15 +09002180 return;
satoke7c6998e2011-06-03 17:57:59 +09002181 }
2182
satok28203512010-11-24 11:06:49 +09002183 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 synchronized (mMethodMap) {
2185 if (token == null) {
2186 if (mContext.checkCallingOrSelfPermission(
2187 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2188 != PackageManager.PERMISSION_GRANTED) {
2189 throw new SecurityException(
2190 "Using null token requires permission "
2191 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2192 }
2193 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002194 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2195 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 return;
2197 }
2198
satokc5933802011-08-31 21:26:04 +09002199 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 try {
satokab751aa2010-09-14 19:17:36 +09002201 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 } finally {
2203 Binder.restoreCallingIdentity(ident);
2204 }
2205 }
2206 }
2207
satok42c5a162011-05-26 16:46:14 +09002208 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002210 if (!calledFromValidUser()) {
2211 return;
2212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002213 synchronized (mMethodMap) {
2214 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002215 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2216 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 return;
2218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 long ident = Binder.clearCallingIdentity();
2220 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002221 hideCurrentInputLocked(flags, null);
2222 } finally {
2223 Binder.restoreCallingIdentity(ident);
2224 }
2225 }
2226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002227
satok42c5a162011-05-26 16:46:14 +09002228 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002229 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002230 if (!calledFromValidUser()) {
2231 return;
2232 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002233 synchronized (mMethodMap) {
2234 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002235 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2236 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002237 return;
2238 }
2239 long ident = Binder.clearCallingIdentity();
2240 try {
2241 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 } finally {
2243 Binder.restoreCallingIdentity(ident);
2244 }
2245 }
2246 }
2247
2248 void setEnabledSessionInMainThread(SessionState session) {
2249 if (mEnabledSession != session) {
2250 if (mEnabledSession != null) {
2251 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002252 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 mEnabledSession.method.setSessionEnabled(
2254 mEnabledSession.session, false);
2255 } catch (RemoteException e) {
2256 }
2257 }
2258 mEnabledSession = session;
2259 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002260 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 session.method.setSessionEnabled(
2262 session.session, true);
2263 } catch (RemoteException e) {
2264 }
2265 }
2266 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002267
satok42c5a162011-05-26 16:46:14 +09002268 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002270 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 switch (msg.what) {
2272 case MSG_SHOW_IM_PICKER:
2273 showInputMethodMenu();
2274 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002275
satokab751aa2010-09-14 19:17:36 +09002276 case MSG_SHOW_IM_SUBTYPE_PICKER:
2277 showInputMethodSubtypeMenu();
2278 return true;
2279
satok47a44912010-10-06 16:03:58 +09002280 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002281 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002282 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002283 args.recycle();
satok217f5482010-12-15 05:19:19 +09002284 return true;
2285
2286 case MSG_SHOW_IM_CONFIG:
2287 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002288 return true;
2289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 case MSG_UNBIND_INPUT:
2293 try {
2294 ((IInputMethod)msg.obj).unbindInput();
2295 } catch (RemoteException e) {
2296 // There is nothing interesting about the method dying.
2297 }
2298 return true;
2299 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002300 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 try {
2302 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2303 } catch (RemoteException e) {
2304 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 return true;
2307 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002308 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002310 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2311 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 } catch (RemoteException e) {
2313 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002314 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 return true;
2316 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002317 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002319 ((IInputMethod)args.arg1).hideSoftInput(0,
2320 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 } catch (RemoteException e) {
2322 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002323 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 return true;
2325 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002326 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002328 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2330 } catch (RemoteException e) {
2331 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002332 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 return true;
2334 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 try {
2337 ((IInputMethod)args.arg1).createSession(
2338 (IInputMethodCallback)args.arg2);
2339 } catch (RemoteException e) {
2340 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002341 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002346 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 try {
2348 SessionState session = (SessionState)args.arg1;
2349 setEnabledSessionInMainThread(session);
2350 session.method.startInput((IInputContext)args.arg2,
2351 (EditorInfo)args.arg3);
2352 } catch (RemoteException e) {
2353 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002354 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 return true;
2356 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002357 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 try {
2359 SessionState session = (SessionState)args.arg1;
2360 setEnabledSessionInMainThread(session);
2361 session.method.restartInput((IInputContext)args.arg2,
2362 (EditorInfo)args.arg3);
2363 } catch (RemoteException e) {
2364 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002365 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 case MSG_UNBIND_METHOD:
2371 try {
2372 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2373 } catch (RemoteException e) {
2374 // There is nothing interesting about the last client dying.
2375 }
2376 return true;
2377 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002378 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 try {
2380 ((IInputMethodClient)args.arg1).onBindMethod(
2381 (InputBindResult)args.arg2);
2382 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002383 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002385 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002387 case MSG_SET_ACTIVE:
2388 try {
2389 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2390 } catch (RemoteException e) {
2391 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2392 + ((ClientState)msg.obj).pid + " uid "
2393 + ((ClientState)msg.obj).uid);
2394 }
2395 return true;
satok01038492012-04-09 21:08:27 +09002396
2397 // --------------------------------------------------------------
2398 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2399 mHardKeyboardListener.handleHardKeyboardStatusChange(
2400 msg.arg1 == 1, msg.arg2 == 1);
2401 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403 return false;
2404 }
2405
satok5b927c432012-05-01 20:09:34 +09002406 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002407 return (inputMethod.getServiceInfo().applicationInfo.flags
2408 & ApplicationInfo.FLAG_SYSTEM) != 0;
2409 }
2410
Ken Wakasa586f0512011-01-20 22:31:01 +09002411 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2412 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2413 final int subtypeCount = imi.getSubtypeCount();
2414 for (int i = 0; i < subtypeCount; ++i) {
2415 subtypes.add(imi.getSubtypeAt(i));
2416 }
2417 return subtypes;
2418 }
2419
satoka86f5e42011-09-02 17:12:42 +09002420 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2421 InputMethodInfo imi, String mode) {
2422 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2423 final int subtypeCount = imi.getSubtypeCount();
2424 for (int i = 0; i < subtypeCount; ++i) {
2425 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2426 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2427 subtypes.add(subtype);
2428 }
2429 }
2430 return subtypes;
2431 }
2432
satokdc9ddae2011-10-06 12:22:36 +09002433 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002434 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002435 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002436 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002437 int i = enabled.size();
2438 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002439 while (i > 0) {
2440 i--;
satokdc9ddae2011-10-06 12:22:36 +09002441 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002442 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2443 return imi;
2444 }
2445 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2446 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002447 }
2448 }
satok0a1bcf42012-05-16 19:26:31 +09002449 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002450 }
2451 return null;
2452 }
2453
2454 private boolean chooseNewDefaultIMELocked() {
2455 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2456 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002457 if (DEBUG) {
2458 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2459 }
satok723a27e2010-11-11 14:58:11 +09002460 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002461 return true;
2462 }
2463
2464 return false;
2465 }
2466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2468 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002469 if (DEBUG) {
2470 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 list.clear();
2473 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002474
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 // Use for queryIntentServicesAsUser
2476 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002477 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002478 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002479 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002480 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002482 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002484 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002485
satoke7c6998e2011-06-03 17:57:59 +09002486 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2487 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 for (int i = 0; i < services.size(); ++i) {
2489 ResolveInfo ri = services.get(i);
2490 ServiceInfo si = ri.serviceInfo;
2491 ComponentName compName = new ComponentName(si.packageName, si.name);
2492 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2493 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002494 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 + ": it does not require the permission "
2496 + android.Manifest.permission.BIND_INPUT_METHOD);
2497 continue;
2498 }
2499
Joe Onorato8a9b2202010-02-26 18:56:32 -08002500 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501
2502 try {
satoke7c6998e2011-06-03 17:57:59 +09002503 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002505 final String id = p.getId();
2506 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
satok5b927c432012-05-01 20:09:34 +09002508 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002509 // by default
2510 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002511 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002512 }
2513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002515 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002519 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002521 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
2523 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002524
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002525 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002526 if (!TextUtils.isEmpty(defaultImiId)) {
2527 if (!map.containsKey(defaultImiId)) {
2528 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2529 if (chooseNewDefaultIMELocked()) {
2530 updateFromSettingsLocked();
2531 }
2532 } else {
2533 // Double check that the default IME is certainly enabled.
2534 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002535 }
2536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002540
satokab751aa2010-09-14 19:17:36 +09002541 private void showInputMethodMenu() {
2542 showInputMethodMenuInternal(false);
2543 }
2544
2545 private void showInputMethodSubtypeMenu() {
2546 showInputMethodMenuInternal(true);
2547 }
2548
satok217f5482010-12-15 05:19:19 +09002549 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002550 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002551 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002552 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2553 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002554 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002555 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002556 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002557 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002558 }
2559
2560 private void showConfigureInputMethods() {
2561 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2562 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2563 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2564 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002565 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002566 }
2567
satok2c93efc2012-04-02 19:33:47 +09002568 private boolean isScreenLocked() {
2569 return mKeyguardManager != null
2570 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2571 }
satokab751aa2010-09-14 19:17:36 +09002572 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002573 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002576 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002577
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002578 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002579 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002580 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002581
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002582 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002583 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2584 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002585 if (immis == null || immis.size() == 0) {
2586 return;
2587 }
2588
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002589 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590
satok688bd472012-02-09 20:09:17 +09002591 final List<ImeSubtypeListItem> imList =
2592 mImListManager.getSortedInputMethodAndSubtypeList(
2593 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002594
satokc3690562012-01-10 20:14:43 +09002595 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002596 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002597 if (currentSubtype != null) {
2598 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2599 lastInputMethodSubtypeId =
2600 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2601 }
2602 }
2603
Ken Wakasa761eb372011-03-04 19:06:18 +09002604 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002605 mIms = new InputMethodInfo[N];
2606 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002607 int checkedItem = 0;
2608 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002609 final ImeSubtypeListItem item = imList.get(i);
2610 mIms[i] = item.mImi;
2611 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002612 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002613 int subtypeId = mSubtypeIds[i];
2614 if ((subtypeId == NOT_A_SUBTYPE_ID)
2615 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2616 || (subtypeId == lastInputMethodSubtypeId)) {
2617 checkedItem = i;
2618 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002621 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002622 com.android.internal.R.styleable.DialogPreference,
2623 com.android.internal.R.attr.alertDialogStyle, 0);
2624 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002626 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002627 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002629 }
2630 })
2631 .setIcon(a.getDrawable(
2632 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2633 a.recycle();
satok01038492012-04-09 21:08:27 +09002634 final LayoutInflater inflater =
2635 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2636 final View tv = inflater.inflate(
2637 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2638 mDialogBuilder.setCustomTitle(tv);
2639
2640 // Setup layout for a toggle switch of the hardware keyboard
2641 mSwitchingDialogTitleView = tv;
2642 mSwitchingDialogTitleView.findViewById(
2643 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2644 mWindowManagerService.isHardKeyboardAvailable() ?
2645 View.VISIBLE : View.GONE);
2646 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2647 com.android.internal.R.id.hard_keyboard_switch));
2648 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2649 hardKeySwitch.setOnCheckedChangeListener(
2650 new OnCheckedChangeListener() {
2651 @Override
2652 public void onCheckedChanged(
2653 CompoundButton buttonView, boolean isChecked) {
2654 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2655 }
2656 });
satokd87c2592010-09-29 11:52:06 +09002657
Ken Wakasa05dbb652011-08-22 15:22:43 +09002658 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2659 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2660 checkedItem);
2661
2662 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002663 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002664 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002665 public void onClick(DialogInterface dialog, int which) {
2666 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002667 if (mIms == null || mIms.length <= which
2668 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002669 return;
2670 }
2671 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002672 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002673 hideInputMethodMenu();
2674 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002675 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002676 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002677 subtypeId = NOT_A_SUBTYPE_ID;
2678 }
2679 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002680 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002683 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684
satokbc81b692011-08-26 16:22:22 +09002685 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002686 mDialogBuilder.setPositiveButton(
2687 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002688 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002689 @Override
satok7f35c8c2010-10-07 21:13:11 +09002690 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002691 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002692 }
2693 });
2694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002696 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 mSwitchingDialog.getWindow().setType(
2698 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002699 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2700 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002701 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 mSwitchingDialog.show();
2703 }
2704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002705
satok93d744d2012-05-09 17:14:08 +09002706 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002707 public final CharSequence mImeName;
2708 public final CharSequence mSubtypeName;
2709 public final InputMethodInfo mImi;
2710 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002711 private final boolean mIsSystemLocale;
2712 private final boolean mIsSystemLanguage;
2713
Ken Wakasa05dbb652011-08-22 15:22:43 +09002714 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002715 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002716 mImeName = imeName;
2717 mSubtypeName = subtypeName;
2718 mImi = imi;
2719 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002720 if (TextUtils.isEmpty(subtypeLocale)) {
2721 mIsSystemLocale = false;
2722 mIsSystemLanguage = false;
2723 } else {
2724 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2725 mIsSystemLanguage = mIsSystemLocale
2726 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2727 }
2728 }
2729
2730 @Override
2731 public int compareTo(ImeSubtypeListItem other) {
2732 if (TextUtils.isEmpty(mImeName)) {
2733 return 1;
2734 }
2735 if (TextUtils.isEmpty(other.mImeName)) {
2736 return -1;
2737 }
2738 if (!TextUtils.equals(mImeName, other.mImeName)) {
2739 return mImeName.toString().compareTo(other.mImeName.toString());
2740 }
2741 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2742 return 0;
2743 }
2744 if (mIsSystemLocale) {
2745 return -1;
2746 }
2747 if (other.mIsSystemLocale) {
2748 return 1;
2749 }
2750 if (mIsSystemLanguage) {
2751 return -1;
2752 }
2753 if (other.mIsSystemLanguage) {
2754 return 1;
2755 }
2756 if (TextUtils.isEmpty(mSubtypeName)) {
2757 return 1;
2758 }
2759 if (TextUtils.isEmpty(other.mSubtypeName)) {
2760 return -1;
2761 }
2762 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002763 }
2764 }
2765
2766 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2767 private final LayoutInflater mInflater;
2768 private final int mTextViewResourceId;
2769 private final List<ImeSubtypeListItem> mItemsList;
2770 private final int mCheckedItem;
2771 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2772 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2773 super(context, textViewResourceId, itemsList);
2774 mTextViewResourceId = textViewResourceId;
2775 mItemsList = itemsList;
2776 mCheckedItem = checkedItem;
2777 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2778 }
2779
2780 @Override
2781 public View getView(int position, View convertView, ViewGroup parent) {
2782 final View view = convertView != null ? convertView
2783 : mInflater.inflate(mTextViewResourceId, null);
2784 if (position < 0 || position >= mItemsList.size()) return view;
2785 final ImeSubtypeListItem item = mItemsList.get(position);
2786 final CharSequence imeName = item.mImeName;
2787 final CharSequence subtypeName = item.mSubtypeName;
2788 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2789 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2790 if (TextUtils.isEmpty(subtypeName)) {
2791 firstTextView.setText(imeName);
2792 secondTextView.setVisibility(View.GONE);
2793 } else {
2794 firstTextView.setText(subtypeName);
2795 secondTextView.setText(imeName);
2796 secondTextView.setVisibility(View.VISIBLE);
2797 }
2798 final RadioButton radioButton =
2799 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2800 radioButton.setChecked(position == mCheckedItem);
2801 return view;
2802 }
2803 }
2804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002806 synchronized (mMethodMap) {
2807 hideInputMethodMenuLocked();
2808 }
2809 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002810
The Android Open Source Project10592532009-03-18 17:39:46 -07002811 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002812 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813
The Android Open Source Project10592532009-03-18 17:39:46 -07002814 if (mSwitchingDialog != null) {
2815 mSwitchingDialog.dismiss();
2816 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002818
The Android Open Source Project10592532009-03-18 17:39:46 -07002819 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002820 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002824
satok42c5a162011-05-26 16:46:14 +09002825 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002827 // TODO: Make this work even for non-current users?
2828 if (!calledFromValidUser()) {
2829 return false;
2830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 synchronized (mMethodMap) {
2832 if (mContext.checkCallingOrSelfPermission(
2833 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2834 != PackageManager.PERMISSION_GRANTED) {
2835 throw new SecurityException(
2836 "Requires permission "
2837 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2838 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 long ident = Binder.clearCallingIdentity();
2841 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 } finally {
2844 Binder.restoreCallingIdentity(ident);
2845 }
2846 }
2847 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848
2849 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2850 // Make sure this is a valid input method.
2851 InputMethodInfo imm = mMethodMap.get(id);
2852 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002853 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002854 }
2855
satokd87c2592010-09-29 11:52:06 +09002856 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2857 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002858
satokd87c2592010-09-29 11:52:06 +09002859 if (enabled) {
2860 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2861 if (pair.first.equals(id)) {
2862 // We are enabling this input method, but it is already enabled.
2863 // Nothing to do. The previous state was enabled.
2864 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002865 }
2866 }
satokd87c2592010-09-29 11:52:06 +09002867 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2868 // Previous state was disabled.
2869 return false;
2870 } else {
2871 StringBuilder builder = new StringBuilder();
2872 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2873 builder, enabledInputMethodsList, id)) {
2874 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002875 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002876 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2877 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2878 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002879 }
2880 // Previous state was enabled.
2881 return true;
2882 } else {
2883 // We are disabling the input method but it is already disabled.
2884 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002885 return false;
2886 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002887 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002888 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002889
satok57ffc002011-01-25 00:11:47 +09002890 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2891 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002892 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002893 }
2894
satok723a27e2010-11-11 14:58:11 +09002895 private void saveCurrentInputMethodAndSubtypeToHistory() {
2896 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2897 if (mCurrentSubtype != null) {
2898 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2899 }
satok57ffc002011-01-25 00:11:47 +09002900 if (canAddToLastInputMethod(mCurrentSubtype)) {
2901 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2902 }
satokab751aa2010-09-14 19:17:36 +09002903 }
2904
satok723a27e2010-11-11 14:58:11 +09002905 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2906 boolean setSubtypeOnly) {
2907 // Update the history of InputMethod and Subtype
2908 saveCurrentInputMethodAndSubtypeToHistory();
2909
2910 // Set Subtype here
2911 if (imi == null || subtypeId < 0) {
2912 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002913 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002914 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002915 if (subtypeId < imi.getSubtypeCount()) {
2916 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2917 mSettings.putSelectedSubtype(subtype.hashCode());
2918 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002919 } else {
2920 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002921 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002922 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002923 }
satokab751aa2010-09-14 19:17:36 +09002924 }
satok723a27e2010-11-11 14:58:11 +09002925
satok4c0e7152012-06-20 20:08:44 +09002926 // Workaround.
2927 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2928 // IMEs are not recognized and considered uninstalled.
2929 // Actually, we can't move everything after SystemReady because
2930 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2931 // the default IME here and try cheking the default IME again in systemReady().
2932 // TODO: Do nothing before system ready and implement a separated logic for
2933 // the encryption lock screen.
2934 // TODO: ASEC should be ready before IMMS is instantiated.
2935 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002936 // Set InputMethod here
2937 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2938 }
2939 }
2940
2941 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2942 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2943 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2944 // newDefaultIme is empty when there is no candidate for the selected IME.
2945 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2946 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2947 if (subtypeHashCode != null) {
2948 try {
2949 lastSubtypeId = getSubtypeIdFromHashCode(
2950 imi, Integer.valueOf(subtypeHashCode));
2951 } catch (NumberFormatException e) {
2952 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2953 }
2954 }
2955 }
2956 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002957 }
2958
2959 private int getSelectedInputMethodSubtypeId(String id) {
2960 InputMethodInfo imi = mMethodMap.get(id);
2961 if (imi == null) {
2962 return NOT_A_SUBTYPE_ID;
2963 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002964 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2965 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002966 }
2967
satokfdf419e2012-05-08 16:52:08 +09002968 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2969 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2970 }
2971
2972 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002973 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002974 final int subtypeCount = imi.getSubtypeCount();
2975 for (int i = 0; i < subtypeCount; ++i) {
2976 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002977 if (subtypeHashCode == ims.hashCode()) {
2978 return i;
2979 }
satokab751aa2010-09-14 19:17:36 +09002980 }
2981 }
2982 return NOT_A_SUBTYPE_ID;
2983 }
2984
satoka86f5e42011-09-02 17:12:42 +09002985 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2986 Resources res, InputMethodInfo imi) {
2987 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002988 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002989 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002990 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002991 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002992 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002993 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002994 // scan overriding implicitly enabled subtypes.
2995 InputMethodSubtype subtype = subtypes.get(i);
2996 if (subtype.overridesImplicitlyEnabledSubtype()) {
2997 final String mode = subtype.getMode();
2998 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2999 applicableModeAndSubtypesMap.put(mode, subtype);
3000 }
3001 }
3002 }
3003 if (applicableModeAndSubtypesMap.size() > 0) {
3004 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
3005 }
3006 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09003007 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09003008 final String locale = subtype.getLocale();
3009 final String mode = subtype.getMode();
3010 // When system locale starts with subtype's locale, that subtype will be applicable
3011 // for system locale
3012 // For instance, it's clearly applicable for cases like system locale = en_US and
3013 // subtype = en, but it is not necessarily considered applicable for cases like system
3014 // locale = en and subtype = en_US.
3015 // We just call systemLocale.startsWith(locale) in this function because there is no
3016 // need to find applicable subtypes aggressively unlike
3017 // findLastResortApplicableSubtypeLocked.
3018 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003019 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003020 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003021 if (applicableSubtype != null) {
3022 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3023 if (!systemLocale.equals(locale)) continue;
3024 }
satok3da92232011-01-11 22:46:30 +09003025 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003026 }
3027 }
3028 final InputMethodSubtype keyboardSubtype
3029 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3030 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3031 applicableModeAndSubtypesMap.values());
3032 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3033 for (int i = 0; i < N; ++i) {
3034 final InputMethodSubtype subtype = subtypes.get(i);
3035 final String mode = subtype.getMode();
3036 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3037 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3038 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003039 }
3040 }
3041 }
satokc3690562012-01-10 20:14:43 +09003042 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003043 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003044 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003045 if (lastResortKeyboardSubtype != null) {
3046 applicableSubtypes.add(lastResortKeyboardSubtype);
3047 }
3048 }
3049 return applicableSubtypes;
3050 }
3051
satok4e4569d2010-11-19 18:45:53 +09003052 /**
3053 * If there are no selected subtypes, tries finding the most applicable one according to the
3054 * given locale.
3055 * @param subtypes this function will search the most applicable subtype in subtypes
3056 * @param mode subtypes will be filtered by mode
3057 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003058 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3059 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003060 * @return the most applicable subtypeId
3061 */
satokdf31ae62011-01-15 06:19:44 +09003062 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3063 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003064 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003065 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003066 return null;
satok8fbb1e82010-11-02 23:15:58 +09003067 }
satok4e4569d2010-11-19 18:45:53 +09003068 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003069 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003070 }
satok8fbb1e82010-11-02 23:15:58 +09003071 final String language = locale.substring(0, 2);
3072 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003073 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003074 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003075 final int N = subtypes.size();
3076 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003077 InputMethodSubtype subtype = subtypes.get(i);
3078 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003079 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3080 // and all subtypes with all modes can be candidates.
3081 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003082 if (firstMatchedModeSubtype == null) {
3083 firstMatchedModeSubtype = subtype;
3084 }
satok9ef02832010-11-04 21:17:48 +09003085 if (locale.equals(subtypeLocale)) {
3086 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003087 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003088 break;
3089 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3090 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003091 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003092 partialMatchFound = true;
3093 }
satok8fbb1e82010-11-02 23:15:58 +09003094 }
3095 }
3096
satok7599a7f2010-12-22 13:45:23 +09003097 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3098 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003099 }
3100
satok8fbb1e82010-11-02 23:15:58 +09003101 // The first subtype applicable to the system locale will be defined as the most applicable
3102 // subtype.
3103 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003104 if (applicableSubtype != null) {
3105 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3106 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3107 }
satok8fbb1e82010-11-02 23:15:58 +09003108 }
satokcd7cd292010-11-20 15:46:23 +09003109 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003110 }
3111
satok4e4569d2010-11-19 18:45:53 +09003112 // If there are no selected shortcuts, tries finding the most applicable ones.
3113 private Pair<InputMethodInfo, InputMethodSubtype>
3114 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3115 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3116 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003117 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003118 boolean foundInSystemIME = false;
3119
3120 // Search applicable subtype for each InputMethodInfo
3121 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003122 final String imiId = imi.getId();
3123 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3124 continue;
3125 }
satokcd7cd292010-11-20 15:46:23 +09003126 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003127 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003128 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003129 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003130 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003131 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003132 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003133 }
satokdf31ae62011-01-15 06:19:44 +09003134 // 2. Search by the system locale from enabledSubtypes.
3135 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003136 if (subtype == null) {
3137 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003138 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003139 }
satoka86f5e42011-09-02 17:12:42 +09003140 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3141 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3142 final ArrayList<InputMethodSubtype> subtypesForSearch =
3143 overridingImplicitlyEnabledSubtypes.isEmpty()
3144 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003145 // 4. Search by the current subtype's locale from all subtypes.
3146 if (subtype == null && mCurrentSubtype != null) {
3147 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003148 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003149 }
3150 // 5. Search by the system locale from all subtypes.
3151 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003152 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003153 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003154 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003155 }
satokcd7cd292010-11-20 15:46:23 +09003156 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003157 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003158 // The current input method is the most applicable IME.
3159 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003160 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003161 break;
satok7599a7f2010-12-22 13:45:23 +09003162 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003163 // The system input method is 2nd applicable IME.
3164 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003165 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003166 if ((imi.getServiceInfo().applicationInfo.flags
3167 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3168 foundInSystemIME = true;
3169 }
satok4e4569d2010-11-19 18:45:53 +09003170 }
3171 }
3172 }
3173 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003174 if (mostApplicableIMI != null) {
3175 Slog.w(TAG, "Most applicable shortcut input method was:"
3176 + mostApplicableIMI.getId());
3177 if (mostApplicableSubtype != null) {
3178 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3179 + "," + mostApplicableSubtype.getMode() + ","
3180 + mostApplicableSubtype.getLocale());
3181 }
3182 }
satok4e4569d2010-11-19 18:45:53 +09003183 }
satokcd7cd292010-11-20 15:46:23 +09003184 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003185 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003186 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003187 } else {
3188 return null;
3189 }
3190 }
3191
satokab751aa2010-09-14 19:17:36 +09003192 /**
3193 * @return Return the current subtype of this input method.
3194 */
satok42c5a162011-05-26 16:46:14 +09003195 @Override
satokab751aa2010-09-14 19:17:36 +09003196 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003197 // TODO: Make this work even for non-current users?
3198 if (!calledFromValidUser()) {
3199 return null;
3200 }
3201 synchronized (mMethodMap) {
3202 return getCurrentInputMethodSubtypeLocked();
3203 }
3204 }
3205
3206 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003207 if (mCurMethodId == null) {
3208 return null;
3209 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003210 final boolean subtypeIsSelected =
3211 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3212 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3213 if (imi == null || imi.getSubtypeCount() == 0) {
3214 return null;
satok4e4569d2010-11-19 18:45:53 +09003215 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003216 if (!subtypeIsSelected || mCurrentSubtype == null
3217 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3218 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3219 if (subtypeId == NOT_A_SUBTYPE_ID) {
3220 // If there are no selected subtypes, the framework will try to find
3221 // the most applicable subtype from explicitly or implicitly enabled
3222 // subtypes.
3223 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3224 getEnabledInputMethodSubtypeListLocked(imi, true);
3225 // If there is only one explicitly or implicitly enabled subtype,
3226 // just returns it.
3227 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3228 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3229 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3230 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3231 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3232 SUBTYPE_MODE_KEYBOARD, null, true);
3233 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003234 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003235 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3236 true);
satok4e4569d2010-11-19 18:45:53 +09003237 }
satok3ef8b292010-11-23 06:06:29 +09003238 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003239 } else {
3240 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003241 }
3242 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003243 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003244 }
3245
satokf3db1af2010-11-23 13:34:33 +09003246 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3247 InputMethodSubtype subtype) {
3248 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3249 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3250 } else {
3251 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3252 subtypes.add(subtype);
3253 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3254 }
3255 }
3256
satok4e4569d2010-11-19 18:45:53 +09003257 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003258 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003259 @Override
satok4e4569d2010-11-19 18:45:53 +09003260 public List getShortcutInputMethodsAndSubtypes() {
3261 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003262 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003263 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003264 // If there are no selected shortcut subtypes, the framework will try to find
3265 // the most applicable subtype from all subtypes whose mode is
3266 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003267 Pair<InputMethodInfo, InputMethodSubtype> info =
3268 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3269 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003270 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003271 ret.add(info.first);
3272 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003273 }
satok3da92232011-01-11 22:46:30 +09003274 return ret;
satokf3db1af2010-11-23 13:34:33 +09003275 }
satokf3db1af2010-11-23 13:34:33 +09003276 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3277 ret.add(imi);
3278 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3279 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003280 }
3281 }
satokf3db1af2010-11-23 13:34:33 +09003282 return ret;
satok4e4569d2010-11-19 18:45:53 +09003283 }
3284 }
3285
satok42c5a162011-05-26 16:46:14 +09003286 @Override
satokb66d2872010-11-10 01:04:04 +09003287 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003288 // TODO: Make this work even for non-current users?
3289 if (!calledFromValidUser()) {
3290 return false;
3291 }
satokb66d2872010-11-10 01:04:04 +09003292 synchronized (mMethodMap) {
3293 if (subtype != null && mCurMethodId != null) {
3294 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3295 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3296 if (subtypeId != NOT_A_SUBTYPE_ID) {
3297 setInputMethodLocked(mCurMethodId, subtypeId);
3298 return true;
3299 }
3300 }
3301 return false;
3302 }
3303 }
3304
satok688bd472012-02-09 20:09:17 +09003305 private static class InputMethodAndSubtypeListManager {
3306 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003307 // Used to load label
satok688bd472012-02-09 20:09:17 +09003308 private final PackageManager mPm;
3309 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003310 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003311 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3312 mContext = context;
3313 mPm = context.getPackageManager();
3314 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003315 final Locale locale = context.getResources().getConfiguration().locale;
3316 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003317 }
3318
3319 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3320 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3321 new Comparator<InputMethodInfo>() {
3322 @Override
3323 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3324 if (imi2 == null) return 0;
3325 if (imi1 == null) return 1;
3326 if (mPm == null) {
3327 return imi1.getId().compareTo(imi2.getId());
3328 }
3329 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3330 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3331 return imiId1.toString().compareTo(imiId2.toString());
3332 }
3333 });
3334
3335 public ImeSubtypeListItem getNextInputMethod(
3336 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3337 if (imi == null) {
3338 return null;
3339 }
3340 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3341 if (imList.size() <= 1) {
3342 return null;
3343 }
3344 final int N = imList.size();
3345 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003346 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003347 : NOT_A_SUBTYPE_ID;
3348 for (int i = 0; i < N; ++i) {
3349 final ImeSubtypeListItem isli = imList.get(i);
3350 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3351 if (!onlyCurrentIme) {
3352 return imList.get((i + 1) % N);
3353 }
3354 for (int j = 0; j < N - 1; ++j) {
3355 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3356 if (candidate.mImi.equals(imi)) {
3357 return candidate;
3358 }
3359 }
3360 return null;
3361 }
3362 }
3363 return null;
3364 }
3365
3366 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3367 return getSortedInputMethodAndSubtypeList(true, false, false);
3368 }
3369
3370 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3371 boolean inputShown, boolean isScreenLocked) {
3372 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3373 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3374 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3375 if (immis == null || immis.size() == 0) {
3376 return Collections.emptyList();
3377 }
3378 mSortedImmis.clear();
3379 mSortedImmis.putAll(immis);
3380 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3381 if (imi == null) continue;
3382 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3383 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3384 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3385 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3386 }
3387 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3388 final CharSequence imeLabel = imi.loadLabel(mPm);
3389 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3390 final int subtypeCount = imi.getSubtypeCount();
3391 if (DEBUG) {
3392 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3393 }
3394 for (int j = 0; j < subtypeCount; ++j) {
3395 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3396 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3397 // We show all enabled IMEs and subtypes when an IME is shown.
3398 if (enabledSubtypeSet.contains(subtypeHashCode)
3399 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3400 final CharSequence subtypeLabel =
3401 subtype.overridesImplicitlyEnabledSubtype() ? null
3402 : subtype.getDisplayName(mContext, imi.getPackageName(),
3403 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003404 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3405 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003406
3407 // Removing this subtype from enabledSubtypeSet because we no longer
3408 // need to add an entry of this subtype to imList to avoid duplicated
3409 // entries.
3410 enabledSubtypeSet.remove(subtypeHashCode);
3411 }
3412 }
3413 } else {
satok93d744d2012-05-09 17:14:08 +09003414 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3415 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003416 }
3417 }
satok93d744d2012-05-09 17:14:08 +09003418 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003419 return imList;
3420 }
3421 }
3422
satokd87c2592010-09-29 11:52:06 +09003423 /**
3424 * Utility class for putting and getting settings for InputMethod
3425 * TODO: Move all putters and getters of settings to this class.
3426 */
3427 private static class InputMethodSettings {
3428 // The string for enabled input method is saved as follows:
3429 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3430 private static final char INPUT_METHOD_SEPARATER = ':';
3431 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003432 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003433 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3434
satok723a27e2010-11-11 14:58:11 +09003435 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003436 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3437
satokdf31ae62011-01-15 06:19:44 +09003438 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003439 private final ContentResolver mResolver;
3440 private final HashMap<String, InputMethodInfo> mMethodMap;
3441 private final ArrayList<InputMethodInfo> mMethodList;
3442
3443 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003444 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003445
3446 private static void buildEnabledInputMethodsSettingString(
3447 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3448 String id = pair.first;
3449 ArrayList<String> subtypes = pair.second;
3450 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003451 // Inputmethod and subtypes are saved in the settings as follows:
3452 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3453 for (String subtypeId: subtypes) {
3454 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003455 }
3456 }
3457
3458 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003459 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003460 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3461 int userId) {
3462 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003463 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003464 mResolver = resolver;
3465 mMethodMap = methodMap;
3466 mMethodList = methodList;
3467 }
3468
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003469 public void setCurrentUserId(int userId) {
3470 if (DEBUG) {
3471 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3472 + userId + ", new ime = " + getSelectedInputMethod());
3473 }
3474 // IMMS settings are kept per user, so keep track of current user
3475 mCurrentUserId = userId;
3476 }
3477
satokd87c2592010-09-29 11:52:06 +09003478 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3479 return createEnabledInputMethodListLocked(
3480 getEnabledInputMethodsAndSubtypeListLocked());
3481 }
3482
satok7f35c8c2010-10-07 21:13:11 +09003483 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003484 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3485 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003486 getEnabledInputMethodsAndSubtypeListLocked());
3487 }
3488
satok67ddf9c2010-11-17 09:45:54 +09003489 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3490 InputMethodInfo imi) {
3491 List<Pair<String, ArrayList<String>>> imsList =
3492 getEnabledInputMethodsAndSubtypeListLocked();
3493 ArrayList<InputMethodSubtype> enabledSubtypes =
3494 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003495 if (imi != null) {
3496 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3497 InputMethodInfo info = mMethodMap.get(imsPair.first);
3498 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003499 final int subtypeCount = info.getSubtypeCount();
3500 for (int i = 0; i < subtypeCount; ++i) {
3501 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003502 for (String s: imsPair.second) {
3503 if (String.valueOf(ims.hashCode()).equals(s)) {
3504 enabledSubtypes.add(ims);
3505 }
satok67ddf9c2010-11-17 09:45:54 +09003506 }
3507 }
satok884ef9a2010-11-18 10:39:46 +09003508 break;
satok67ddf9c2010-11-17 09:45:54 +09003509 }
satok67ddf9c2010-11-17 09:45:54 +09003510 }
3511 }
3512 return enabledSubtypes;
3513 }
3514
satokd87c2592010-09-29 11:52:06 +09003515 // At the initial boot, the settings for input methods are not set,
3516 // so we need to enable IME in that case.
3517 public void enableAllIMEsIfThereIsNoEnabledIME() {
3518 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3519 StringBuilder sb = new StringBuilder();
3520 final int N = mMethodList.size();
3521 for (int i = 0; i < N; i++) {
3522 InputMethodInfo imi = mMethodList.get(i);
3523 Slog.i(TAG, "Adding: " + imi.getId());
3524 if (i > 0) sb.append(':');
3525 sb.append(imi.getId());
3526 }
3527 putEnabledInputMethodsStr(sb.toString());
3528 }
3529 }
3530
satokbb4aa062011-01-19 21:40:27 +09003531 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003532 ArrayList<Pair<String, ArrayList<String>>> imsList
3533 = new ArrayList<Pair<String, ArrayList<String>>>();
3534 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3535 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3536 return imsList;
3537 }
satok723a27e2010-11-11 14:58:11 +09003538 mInputMethodSplitter.setString(enabledInputMethodsStr);
3539 while (mInputMethodSplitter.hasNext()) {
3540 String nextImsStr = mInputMethodSplitter.next();
3541 mSubtypeSplitter.setString(nextImsStr);
3542 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003543 ArrayList<String> subtypeHashes = new ArrayList<String>();
3544 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003545 String imeId = mSubtypeSplitter.next();
3546 while (mSubtypeSplitter.hasNext()) {
3547 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003548 }
3549 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3550 }
3551 }
3552 return imsList;
3553 }
3554
3555 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3556 if (reloadInputMethodStr) {
3557 getEnabledInputMethodsStr();
3558 }
3559 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3560 // Add in the newly enabled input method.
3561 putEnabledInputMethodsStr(id);
3562 } else {
3563 putEnabledInputMethodsStr(
3564 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3565 }
3566 }
3567
3568 /**
3569 * Build and put a string of EnabledInputMethods with removing specified Id.
3570 * @return the specified id was removed or not.
3571 */
3572 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3573 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3574 boolean isRemoved = false;
3575 boolean needsAppendSeparator = false;
3576 for (Pair<String, ArrayList<String>> ims: imsList) {
3577 String curId = ims.first;
3578 if (curId.equals(id)) {
3579 // We are disabling this input method, and it is
3580 // currently enabled. Skip it to remove from the
3581 // new list.
3582 isRemoved = true;
3583 } else {
3584 if (needsAppendSeparator) {
3585 builder.append(INPUT_METHOD_SEPARATER);
3586 } else {
3587 needsAppendSeparator = true;
3588 }
3589 buildEnabledInputMethodsSettingString(builder, ims);
3590 }
3591 }
3592 if (isRemoved) {
3593 // Update the setting with the new list of input methods.
3594 putEnabledInputMethodsStr(builder.toString());
3595 }
3596 return isRemoved;
3597 }
3598
3599 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3600 List<Pair<String, ArrayList<String>>> imsList) {
3601 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3602 for (Pair<String, ArrayList<String>> ims: imsList) {
3603 InputMethodInfo info = mMethodMap.get(ims.first);
3604 if (info != null) {
3605 res.add(info);
3606 }
3607 }
3608 return res;
3609 }
3610
satok7f35c8c2010-10-07 21:13:11 +09003611 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003612 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003613 List<Pair<String, ArrayList<String>>> imsList) {
3614 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3615 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3616 for (Pair<String, ArrayList<String>> ims : imsList) {
3617 InputMethodInfo info = mMethodMap.get(ims.first);
3618 if (info != null) {
3619 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3620 }
3621 }
3622 return res;
3623 }
3624
satokd87c2592010-09-29 11:52:06 +09003625 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003626 Settings.Secure.putStringForUser(
3627 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003628 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003629 if (DEBUG) {
3630 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3631 }
satokd87c2592010-09-29 11:52:06 +09003632 }
3633
3634 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003635 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3636 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003637 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003638 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3639 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003640 }
satokd87c2592010-09-29 11:52:06 +09003641 return mEnabledInputMethodsStrCache;
3642 }
satok723a27e2010-11-11 14:58:11 +09003643
3644 private void saveSubtypeHistory(
3645 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3646 StringBuilder builder = new StringBuilder();
3647 boolean isImeAdded = false;
3648 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3649 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3650 newSubtypeId);
3651 isImeAdded = true;
3652 }
3653 for (Pair<String, String> ime: savedImes) {
3654 String imeId = ime.first;
3655 String subtypeId = ime.second;
3656 if (TextUtils.isEmpty(subtypeId)) {
3657 subtypeId = NOT_A_SUBTYPE_ID_STR;
3658 }
3659 if (isImeAdded) {
3660 builder.append(INPUT_METHOD_SEPARATER);
3661 } else {
3662 isImeAdded = true;
3663 }
3664 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3665 subtypeId);
3666 }
3667 // Remove the last INPUT_METHOD_SEPARATER
3668 putSubtypeHistoryStr(builder.toString());
3669 }
3670
3671 public void addSubtypeToHistory(String imeId, String subtypeId) {
3672 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3673 for (Pair<String, String> ime: subtypeHistory) {
3674 if (ime.first.equals(imeId)) {
3675 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003676 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003677 + ime.second);
3678 }
3679 // We should break here
3680 subtypeHistory.remove(ime);
3681 break;
3682 }
3683 }
satokbb4aa062011-01-19 21:40:27 +09003684 if (DEBUG) {
3685 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3686 }
satok723a27e2010-11-11 14:58:11 +09003687 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3688 }
3689
3690 private void putSubtypeHistoryStr(String str) {
3691 if (DEBUG) {
3692 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3693 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003694 Settings.Secure.putStringForUser(
3695 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003696 }
3697
3698 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3699 // Gets the first one from the history
3700 return getLastSubtypeForInputMethodLockedInternal(null);
3701 }
3702
3703 public String getLastSubtypeForInputMethodLocked(String imeId) {
3704 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3705 if (ime != null) {
3706 return ime.second;
3707 } else {
3708 return null;
3709 }
3710 }
3711
3712 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3713 List<Pair<String, ArrayList<String>>> enabledImes =
3714 getEnabledInputMethodsAndSubtypeListLocked();
3715 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003716 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003717 final String imeInTheHistory = imeAndSubtype.first;
3718 // If imeId is empty, returns the first IME and subtype in the history
3719 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3720 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003721 final String subtypeHashCode =
3722 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3723 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003724 if (!TextUtils.isEmpty(subtypeHashCode)) {
3725 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003726 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003727 }
3728 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3729 }
3730 }
3731 }
3732 if (DEBUG) {
3733 Slog.d(TAG, "No enabled IME found in the history");
3734 }
3735 return null;
3736 }
3737
satokdf31ae62011-01-15 06:19:44 +09003738 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003739 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3740 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3741 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003742 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003743 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003744 if (explicitlyEnabledSubtypes.size() == 0) {
3745 // If there are no explicitly enabled subtypes, applicable subtypes are
3746 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003747 // If IME is enabled and no subtypes are enabled, applicable subtypes
3748 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003749 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003750 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003751 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003752 if (implicitlySelectedSubtypes != null) {
3753 final int N = implicitlySelectedSubtypes.size();
3754 for (int i = 0; i < N; ++i) {
3755 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3756 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3757 return subtypeHashCode;
3758 }
3759 }
3760 }
3761 }
3762 } else {
satokf6cafb62011-01-17 16:29:02 +09003763 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003764 if (s.equals(subtypeHashCode)) {
3765 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003766 try {
3767 final int hashCode = Integer.valueOf(subtypeHashCode);
3768 // Check whether the subtype id is valid or not
3769 if (isValidSubtypeId(imi, hashCode)) {
3770 return s;
3771 } else {
3772 return NOT_A_SUBTYPE_ID_STR;
3773 }
3774 } catch (NumberFormatException e) {
3775 return NOT_A_SUBTYPE_ID_STR;
3776 }
satokdf31ae62011-01-15 06:19:44 +09003777 }
satok723a27e2010-11-11 14:58:11 +09003778 }
3779 }
3780 // If imeId was enabled but subtypeId was disabled.
3781 return NOT_A_SUBTYPE_ID_STR;
3782 }
3783 }
3784 // If both imeId and subtypeId are disabled, return null
3785 return null;
3786 }
3787
3788 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3789 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3790 final String subtypeHistoryStr = getSubtypeHistoryStr();
3791 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3792 return imsList;
3793 }
3794 mInputMethodSplitter.setString(subtypeHistoryStr);
3795 while (mInputMethodSplitter.hasNext()) {
3796 String nextImsStr = mInputMethodSplitter.next();
3797 mSubtypeSplitter.setString(nextImsStr);
3798 if (mSubtypeSplitter.hasNext()) {
3799 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3800 // The first element is ime id.
3801 String imeId = mSubtypeSplitter.next();
3802 while (mSubtypeSplitter.hasNext()) {
3803 subtypeId = mSubtypeSplitter.next();
3804 break;
3805 }
3806 imsList.add(new Pair<String, String>(imeId, subtypeId));
3807 }
3808 }
3809 return imsList;
3810 }
3811
3812 private String getSubtypeHistoryStr() {
3813 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003814 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3815 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003816 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003817 return Settings.Secure.getStringForUser(
3818 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003819 }
3820
3821 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003822 if (DEBUG) {
3823 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3824 + mCurrentUserId);
3825 }
3826 Settings.Secure.putStringForUser(
3827 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003828 }
3829
3830 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003831 if (DEBUG) {
3832 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3833 + mCurrentUserId);
3834 }
3835 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3836 subtypeId, mCurrentUserId);
3837 }
3838
3839 public String getDisabledSystemInputMethods() {
3840 return Settings.Secure.getStringForUser(
3841 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3842 }
3843
3844 public String getSelectedInputMethod() {
3845 if (DEBUG) {
3846 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3847 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3848 + ", " + mCurrentUserId);
3849 }
3850 return Settings.Secure.getStringForUser(
3851 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3852 }
3853
3854 public int getSelectedInputMethodSubtypeHashCode() {
3855 try {
3856 return Settings.Secure.getIntForUser(
3857 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3858 } catch (SettingNotFoundException e) {
3859 return NOT_A_SUBTYPE_ID;
3860 }
3861 }
3862
3863 public int getCurrentUserId() {
3864 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003865 }
satokd87c2592010-09-29 11:52:06 +09003866 }
3867
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003868 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003869 private static class InputMethodFileManager {
3870 private static final String SYSTEM_PATH = "system";
3871 private static final String INPUT_METHOD_PATH = "inputmethod";
3872 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3873 private static final String NODE_SUBTYPES = "subtypes";
3874 private static final String NODE_SUBTYPE = "subtype";
3875 private static final String NODE_IMI = "imi";
3876 private static final String ATTR_ID = "id";
3877 private static final String ATTR_LABEL = "label";
3878 private static final String ATTR_ICON = "icon";
3879 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3880 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3881 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3882 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3883 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3884 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003885 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003886 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003887 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003888 if (methodMap == null) {
3889 throw new NullPointerException("methodMap is null");
3890 }
3891 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003892 final File systemDir = userId == UserHandle.USER_OWNER
3893 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3894 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003895 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3896 if (!inputMethodDir.mkdirs()) {
3897 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3898 }
3899 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3900 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3901 if (!subtypeFile.exists()) {
3902 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003903 writeAdditionalInputMethodSubtypes(
3904 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003905 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003906 readAdditionalInputMethodSubtypes(
3907 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003908 }
3909 }
3910
3911 private void deleteAllInputMethodSubtypes(String imiId) {
3912 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003913 mAdditionalSubtypesMap.remove(imiId);
3914 writeAdditionalInputMethodSubtypes(
3915 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003916 }
3917 }
3918
3919 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003920 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003921 synchronized (mMethodMap) {
3922 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3923 final int N = additionalSubtypes.length;
3924 for (int i = 0; i < N; ++i) {
3925 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003926 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003927 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003928 } else {
3929 Slog.w(TAG, "Duplicated subtype definition found: "
3930 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003931 }
3932 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003933 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3934 writeAdditionalInputMethodSubtypes(
3935 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003936 }
3937 }
3938
3939 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3940 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003941 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003942 }
3943 }
3944
3945 private static void writeAdditionalInputMethodSubtypes(
3946 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3947 HashMap<String, InputMethodInfo> methodMap) {
3948 // Safety net for the case that this function is called before methodMap is set.
3949 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3950 FileOutputStream fos = null;
3951 try {
3952 fos = subtypesFile.startWrite();
3953 final XmlSerializer out = new FastXmlSerializer();
3954 out.setOutput(fos, "utf-8");
3955 out.startDocument(null, true);
3956 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3957 out.startTag(null, NODE_SUBTYPES);
3958 for (String imiId : allSubtypes.keySet()) {
3959 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3960 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3961 continue;
3962 }
3963 out.startTag(null, NODE_IMI);
3964 out.attribute(null, ATTR_ID, imiId);
3965 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3966 final int N = subtypesList.size();
3967 for (int i = 0; i < N; ++i) {
3968 final InputMethodSubtype subtype = subtypesList.get(i);
3969 out.startTag(null, NODE_SUBTYPE);
3970 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3971 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3972 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3973 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3974 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3975 out.attribute(null, ATTR_IS_AUXILIARY,
3976 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3977 out.endTag(null, NODE_SUBTYPE);
3978 }
3979 out.endTag(null, NODE_IMI);
3980 }
3981 out.endTag(null, NODE_SUBTYPES);
3982 out.endDocument();
3983 subtypesFile.finishWrite(fos);
3984 } catch (java.io.IOException e) {
3985 Slog.w(TAG, "Error writing subtypes", e);
3986 if (fos != null) {
3987 subtypesFile.failWrite(fos);
3988 }
3989 }
3990 }
3991
3992 private static void readAdditionalInputMethodSubtypes(
3993 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3994 if (allSubtypes == null || subtypesFile == null) return;
3995 allSubtypes.clear();
3996 FileInputStream fis = null;
3997 try {
3998 fis = subtypesFile.openRead();
3999 final XmlPullParser parser = Xml.newPullParser();
4000 parser.setInput(fis, null);
4001 int type = parser.getEventType();
4002 // Skip parsing until START_TAG
4003 while ((type = parser.next()) != XmlPullParser.START_TAG
4004 && type != XmlPullParser.END_DOCUMENT) {}
4005 String firstNodeName = parser.getName();
4006 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4007 throw new XmlPullParserException("Xml doesn't start with subtypes");
4008 }
4009 final int depth =parser.getDepth();
4010 String currentImiId = null;
4011 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4012 while (((type = parser.next()) != XmlPullParser.END_TAG
4013 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4014 if (type != XmlPullParser.START_TAG)
4015 continue;
4016 final String nodeName = parser.getName();
4017 if (NODE_IMI.equals(nodeName)) {
4018 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4019 if (TextUtils.isEmpty(currentImiId)) {
4020 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4021 continue;
4022 }
4023 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4024 allSubtypes.put(currentImiId, tempSubtypesArray);
4025 } else if (NODE_SUBTYPE.equals(nodeName)) {
4026 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4027 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4028 continue;
4029 }
4030 final int icon = Integer.valueOf(
4031 parser.getAttributeValue(null, ATTR_ICON));
4032 final int label = Integer.valueOf(
4033 parser.getAttributeValue(null, ATTR_LABEL));
4034 final String imeSubtypeLocale =
4035 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4036 final String imeSubtypeMode =
4037 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4038 final String imeSubtypeExtraValue =
4039 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004040 final boolean isAuxiliary = "1".equals(String.valueOf(
4041 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004042 final InputMethodSubtype subtype =
4043 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4044 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4045 tempSubtypesArray.add(subtype);
4046 }
4047 }
4048 } catch (XmlPullParserException e) {
4049 Slog.w(TAG, "Error reading subtypes: " + e);
4050 return;
4051 } catch (java.io.IOException e) {
4052 Slog.w(TAG, "Error reading subtypes: " + e);
4053 return;
4054 } catch (NumberFormatException e) {
4055 Slog.w(TAG, "Error reading subtypes: " + e);
4056 return;
4057 } finally {
4058 if (fis != null) {
4059 try {
4060 fis.close();
4061 } catch (java.io.IOException e1) {
4062 Slog.w(TAG, "Failed to close.");
4063 }
4064 }
4065 }
4066 }
4067 }
4068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004070 // Utilities for debug
4071 private static String getStackTrace() {
4072 final StringBuilder sb = new StringBuilder();
4073 try {
4074 throw new RuntimeException();
4075 } catch (RuntimeException e) {
4076 final StackTraceElement[] frames = e.getStackTrace();
4077 // Start at 1 because the first frame is here and we don't care about it
4078 for (int j = 1; j < frames.length; ++j) {
4079 sb.append(frames[j].toString() + "\n");
4080 }
4081 }
4082 return sb.toString();
4083 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 @Override
4086 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4087 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4088 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4091 + Binder.getCallingPid()
4092 + ", uid=" + Binder.getCallingUid());
4093 return;
4094 }
4095
4096 IInputMethod method;
4097 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 synchronized (mMethodMap) {
4102 p.println("Current Input Method Manager state:");
4103 int N = mMethodList.size();
4104 p.println(" Input Methods:");
4105 for (int i=0; i<N; i++) {
4106 InputMethodInfo info = mMethodList.get(i);
4107 p.println(" InputMethod #" + i + ":");
4108 info.dump(p, " ");
4109 }
4110 p.println(" Clients:");
4111 for (ClientState ci : mClients.values()) {
4112 p.println(" Client " + ci + ":");
4113 p.println(" client=" + ci.client);
4114 p.println(" inputContext=" + ci.inputContext);
4115 p.println(" sessionRequested=" + ci.sessionRequested);
4116 p.println(" curSession=" + ci.curSession);
4117 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004118 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004120 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4121 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4123 + " mBoundToMethod=" + mBoundToMethod);
4124 p.println(" mCurToken=" + mCurToken);
4125 p.println(" mCurIntent=" + mCurIntent);
4126 method = mCurMethod;
4127 p.println(" mCurMethod=" + mCurMethod);
4128 p.println(" mEnabledSession=" + mEnabledSession);
4129 p.println(" mShowRequested=" + mShowRequested
4130 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4131 + " mShowForced=" + mShowForced
4132 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004133 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004135
Jeff Brownb88102f2010-09-08 11:49:43 -07004136 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 pw.flush();
4139 try {
4140 client.client.asBinder().dump(fd, args);
4141 } catch (RemoteException e) {
4142 p.println("Input method client dead: " + e);
4143 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004144 } else {
4145 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004147
Jeff Brownb88102f2010-09-08 11:49:43 -07004148 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 pw.flush();
4151 try {
4152 method.asBinder().dump(fd, args);
4153 } catch (RemoteException e) {
4154 p.println("Input method service dead: " + e);
4155 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004156 } else {
4157 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159 }
4160}