blob: 9c533da5f2726a297f1dde16b22fcf97d9759b11 [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;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 class SettingsObserver extends ContentObserver {
391 SettingsObserver(Handler handler) {
392 super(handler);
393 ContentResolver resolver = mContext.getContentResolver();
394 resolver.registerContentObserver(Settings.Secure.getUriFor(
395 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900396 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900397 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
398 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900399 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 @Override public void onChange(boolean selfChange) {
403 synchronized (mMethodMap) {
404 updateFromSettingsLocked();
405 }
406 }
407 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800408
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900409 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
410 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700412 if (mCurClient != null && mCurClient.client != null) {
413 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
414 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900417
418 @Override
419 public void onReceive(Context context, Intent intent) {
420 final String action = intent.getAction();
421 if (Intent.ACTION_SCREEN_ON.equals(action)) {
422 mScreenOn = true;
423 refreshImeWindowVisibilityLocked();
424 updateActive();
425 return;
426 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
427 mScreenOn = false;
428 setImeWindowVisibilityStatusHiddenLocked();
429 updateActive();
430 return;
431 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
432 hideInputMethodMenu();
433 // No need to updateActive
434 return;
435 } else {
436 Slog.w(TAG, "Unexpected intent " + intent);
437 }
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900442 private boolean isChangingPackagesOfCurrentUser() {
443 final int userId = getChangingUserId();
444 final boolean retval = userId == mSettings.getCurrentUserId();
445 if (DEBUG) {
446 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
447 }
448 return retval;
449 }
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900453 if (!isChangingPackagesOfCurrentUser()) {
454 return false;
455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 final int N = mMethodList.size();
459 if (curInputMethodId != null) {
460 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800461 InputMethodInfo imi = mMethodList.get(i);
462 if (imi.getId().equals(curInputMethodId)) {
463 for (String pkg : packages) {
464 if (imi.getPackageName().equals(pkg)) {
465 if (!doit) {
466 return true;
467 }
satok723a27e2010-11-11 14:58:11 +0900468 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800469 chooseNewDefaultIMELocked();
470 return true;
471 }
472 }
473 }
474 }
475 }
476 }
477 return false;
478 }
479
480 @Override
481 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900482 if (!isChangingPackagesOfCurrentUser()) {
483 return;
484 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800485 synchronized (mMethodMap) {
486 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900487 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 final int N = mMethodList.size();
489 if (curInputMethodId != null) {
490 for (int i=0; i<N; i++) {
491 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900492 final String imiId = imi.getId();
493 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800494 curIm = imi;
495 }
satoke7c6998e2011-06-03 17:57:59 +0900496
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900498 if (isPackageModified(imi.getPackageName())) {
499 mFileManager.deleteAllInputMethodSubtypes(imiId);
500 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501 if (change == PACKAGE_TEMPORARY_CHANGE
502 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800503 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800504 + imi.getComponent());
505 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507 }
508 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 buildInputMethodListLocked(mMethodList, mMethodMap);
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800513
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800514 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 int change = isPackageDisappearing(curIm.getPackageName());
516 if (change == PACKAGE_TEMPORARY_CHANGE
517 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800518 ServiceInfo si = null;
519 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900520 si = mIPackageManager.getServiceInfo(
521 curIm.getComponent(), 0, mSettings.getCurrentUserId());
522 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 }
524 if (si == null) {
525 // Uh oh, current input method is no longer around!
526 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900528 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800529 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 changed = true;
531 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800532 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900533 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800534 }
535 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800536 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800537 }
satokab751aa2010-09-14 19:17:36 +0900538
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800539 if (curIm == null) {
540 // We currently don't have a default input method... is
541 // one now available?
542 changed = chooseNewDefaultIMELocked();
543 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800544
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800545 if (changed) {
546 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 }
549 }
550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800551
Jean Chalarde0d32a62011-10-20 20:36:07 +0900552 private static class MethodCallback extends IInputMethodCallback.Stub {
553 private final IInputMethod mMethod;
554 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Jean Chalarde0d32a62011-10-20 20:36:07 +0900556 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900558 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800560
satoke7c6998e2011-06-03 17:57:59 +0900561 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public void finishedEvent(int seq, boolean handled) throws RemoteException {
563 }
564
satoke7c6998e2011-06-03 17:57:59 +0900565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900567 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800570
satok01038492012-04-09 21:08:27 +0900571 private class HardKeyboardListener
572 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
573 @Override
574 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
575 mHandler.sendMessage(mHandler.obtainMessage(
576 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
577 }
578
579 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
580 if (DEBUG) {
581 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
582 + enabled);
583 }
584 synchronized(mMethodMap) {
585 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
586 && mSwitchingDialog.isShowing()) {
587 mSwitchingDialogTitleView.findViewById(
588 com.android.internal.R.id.hard_keyboard_section).setVisibility(
589 available ? View.VISIBLE : View.GONE);
590 }
591 }
592 }
593 }
594
595 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900596 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800598 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 mHandler = new Handler(this);
600 mIWindowManager = IWindowManager.Stub.asInterface(
601 ServiceManager.getService(Context.WINDOW_SERVICE));
602 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900603 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public void executeMessage(Message msg) {
605 handleMessage(msg);
606 }
607 });
satok01038492012-04-09 21:08:27 +0900608 mWindowManagerService = windowManager;
609 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900610
satok7cfc0ed2011-06-20 21:29:36 +0900611 mImeSwitcherNotification = new Notification();
612 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
613 mImeSwitcherNotification.when = 0;
614 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
615 mImeSwitcherNotification.tickerText = null;
616 mImeSwitcherNotification.defaults = 0; // please be quiet
617 mImeSwitcherNotification.sound = null;
618 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400619
620 // Tag this notification specially so SystemUI knows it's important
621 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
622
satok7cfc0ed2011-06-20 21:29:36 +0900623 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900624 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900625
626 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900627
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900628 final IntentFilter broadcastFilter = new IntentFilter();
629 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
630 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
631 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
632 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800633
satok7cfc0ed2011-06-20 21:29:36 +0900634 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900635 int userId = 0;
636 try {
637 ActivityManagerNative.getDefault().registerUserSwitchObserver(
638 new IUserSwitchObserver.Stub() {
639 @Override
640 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900641 synchronized(mMethodMap) {
642 switchUserLocked(newUserId);
643 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900644 if (reply != null) {
645 try {
646 reply.sendResult(null);
647 } catch (RemoteException e) {
648 }
649 }
650 }
651
652 @Override
653 public void onUserSwitchComplete(int newUserId) throws RemoteException {
654 }
655 });
656 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
657 } catch (RemoteException e) {
658 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
659 }
660 mMyPackageMonitor.register(mContext, null, true);
satok913a8922010-08-26 21:53:41 +0900661
satokd87c2592010-09-29 11:52:06 +0900662 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900663 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900664 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900665 mFileManager = new InputMethodFileManager(mMethodMap, userId);
666 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900667
668 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900669 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900670 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900673 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
satok0a1bcf42012-05-16 19:26:31 +0900675 if (!mImeSelectedOnBoot) {
676 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900677 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 mSettingsObserver = new SettingsObserver(mHandler);
681 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900682
683 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
684 // according to the new system locale.
685 final IntentFilter filter = new IntentFilter();
686 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
687 mContext.registerReceiver(
688 new BroadcastReceiver() {
689 @Override
690 public void onReceive(Context context, Intent intent) {
691 synchronized(mMethodMap) {
692 checkCurrentLocaleChangedLocked();
693 }
694 }
695 }, filter);
696 }
697
satok5b927c432012-05-01 20:09:34 +0900698 private void resetDefaultImeLocked(Context context) {
699 // Do not reset the default (current) IME when it is a 3rd-party IME
700 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
701 return;
702 }
703
704 InputMethodInfo defIm = null;
705 for (InputMethodInfo imi : mMethodList) {
706 if (defIm == null) {
707 if (isValidSystemDefaultIme(imi, context)) {
708 defIm = imi;
709 Slog.i(TAG, "Selected default: " + imi.getId());
710 }
711 }
712 }
713 if (defIm == null && mMethodList.size() > 0) {
714 defIm = getMostApplicableDefaultIMELocked();
715 Slog.i(TAG, "No default found, using " + defIm.getId());
716 }
717 if (defIm != null) {
718 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
719 }
720 }
721
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900722 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
723 if (!mSystemReady) {
724 // not system ready
725 return;
726 }
727 final Locale newLocale = mRes.getConfiguration().locale;
728 if (!updateOnlyWhenLocaleChanged
729 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
730 if (!updateOnlyWhenLocaleChanged) {
731 hideCurrentInputLocked(0, null);
732 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700733 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900734 }
735 if (DEBUG) {
736 Slog.i(TAG, "Locale has been changed to " + newLocale);
737 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900738 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
739 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900740 buildInputMethodListLocked(mMethodList, mMethodMap);
741 if (!updateOnlyWhenLocaleChanged) {
742 final String selectedImiId = mSettings.getSelectedInputMethod();
743 if (TextUtils.isEmpty(selectedImiId)) {
744 // This is the first time of the user switch and
745 // set the current ime to the proper one.
746 resetDefaultImeLocked(mContext);
747 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900748 } else {
749 // If the locale is changed, needs to reset the default ime
750 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900751 }
752 updateFromSettingsLocked();
753 mLastSystemLocale = newLocale;
754 if (!updateOnlyWhenLocaleChanged) {
755 try {
756 startInputInnerLocked();
757 } catch (RuntimeException e) {
758 Slog.w(TAG, "Unexpected exception", e);
759 }
760 }
761 }
762 }
763
764 private void checkCurrentLocaleChangedLocked() {
765 resetAllInternalStateLocked(true);
766 }
767
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900768 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900769 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900770 // InputMethodFileManager should be reset when the user is changed
771 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 resetAllInternalStateLocked(false);
773 }
774
satok0a1bcf42012-05-16 19:26:31 +0900775 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
776 if (!mSystemReady) {
777 return false;
778 }
satok5b927c432012-05-01 20:09:34 +0900779 if (!isSystemIme(imi)) {
780 return false;
781 }
782 if (imi.getIsDefaultResourceId() != 0) {
783 try {
784 Resources res = context.createPackageContext(
785 imi.getPackageName(), 0).getResources();
786 if (res.getBoolean(imi.getIsDefaultResourceId())
787 && containsSubtypeOf(imi, context.getResources().getConfiguration().
788 locale.getLanguage())) {
789 return true;
790 }
791 } catch (PackageManager.NameNotFoundException ex) {
792 } catch (Resources.NotFoundException ex) {
793 }
794 }
795 if (imi.getSubtypeCount() == 0) {
796 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
797 }
798 return false;
799 }
800
801 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
802 if (!isSystemIme(imi)) {
803 return false;
804 }
805 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
806 }
807
808 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
809 final int N = imi.getSubtypeCount();
810 for (int i = 0; i < N; ++i) {
811 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
812 return true;
813 }
814 }
815 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
818 @Override
819 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
820 throws RemoteException {
821 try {
822 return super.onTransact(code, data, reply, flags);
823 } catch (RuntimeException e) {
824 // The input method manager only throws security exceptions, so let's
825 // log all others.
826 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829 throw e;
830 }
831 }
832
Dianne Hackborn661cd522011-08-22 00:26:20 -0700833 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700834 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900835 if (DEBUG) {
836 Slog.d(TAG, "--- systemReady");
837 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700838 if (!mSystemReady) {
839 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900840 mKeyguardManager =
841 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700842 mNotificationManager = (NotificationManager)
843 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
844 mStatusBar = statusBar;
845 statusBar.setIconVisibility("ime", false);
846 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900847 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
848 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900849 if (mShowOngoingImeSwitcherForPhones) {
850 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
851 mHardKeyboardListener);
852 }
satok0a1bcf42012-05-16 19:26:31 +0900853 buildInputMethodListLocked(mMethodList, mMethodMap);
854 if (!mImeSelectedOnBoot) {
855 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
856 checkCurrentLocaleChangedLocked();
857 }
858 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700859 try {
860 startInputInnerLocked();
861 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800862 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700863 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700864 }
865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800867
satok15452a42011-10-28 17:58:28 +0900868 private void setImeWindowVisibilityStatusHiddenLocked() {
869 mImeWindowVis = 0;
870 updateImeWindowStatusLocked();
871 }
872
satok3afd6c02011-11-18 08:38:19 +0900873 private void refreshImeWindowVisibilityLocked() {
874 final Configuration conf = mRes.getConfiguration();
875 final boolean haveHardKeyboard = conf.keyboard
876 != Configuration.KEYBOARD_NOKEYS;
877 final boolean hardKeyShown = haveHardKeyboard
878 && conf.hardKeyboardHidden
879 != Configuration.HARDKEYBOARDHIDDEN_YES;
880 final boolean isScreenLocked = mKeyguardManager != null
881 && mKeyguardManager.isKeyguardLocked()
882 && mKeyguardManager.isKeyguardSecure();
883 mImeWindowVis = (!isScreenLocked && (mInputShown || hardKeyShown)) ?
884 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
885 updateImeWindowStatusLocked();
886 }
887
satok15452a42011-10-28 17:58:28 +0900888 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900889 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700890 }
891
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 // ---------------------------------------------------------------------------------------
893 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
894 // 1) it comes from the system process
895 // 2) the calling process' user id is identical to the current user id IMMS thinks.
896 private boolean calledFromValidUser() {
897 final int uid = Binder.getCallingUid();
898 final int userId = UserHandle.getUserId(uid);
899 if (DEBUG) {
900 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
901 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
902 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900903 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900904 }
905 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
906 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900908
909 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
910 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
911 // must not manage background users' states in any functions.
912 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
913 // by a token.
914 if (mContext.checkCallingOrSelfPermission(
915 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
916 == PackageManager.PERMISSION_GRANTED) {
917 if (DEBUG) {
918 Slog.d(TAG, "--- Access granted because the calling process has "
919 + "the INTERACT_ACROSS_USERS_FULL permission");
920 }
921 return true;
922 }
923 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
924 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900925 }
926
927 private boolean bindCurrentInputMethodService(
928 Intent service, ServiceConnection conn, int flags) {
929 if (service == null || conn == null) {
930 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
931 return false;
932 }
933 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
934 }
935
satoke7c6998e2011-06-03 17:57:59 +0900936 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900938 // TODO: Make this work even for non-current users?
939 if (!calledFromValidUser()) {
940 return Collections.emptyList();
941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 synchronized (mMethodMap) {
943 return new ArrayList<InputMethodInfo>(mMethodList);
944 }
945 }
946
satoke7c6998e2011-06-03 17:57:59 +0900947 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900949 // TODO: Make this work even for non-current users?
950 if (!calledFromValidUser()) {
951 return Collections.emptyList();
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900954 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
956 }
957
satokbb4aa062011-01-19 21:40:27 +0900958 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
959 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
960 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
961 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900962 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900963 enabledInputMethodAndSubtypes.put(
964 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
965 }
966 return enabledInputMethodAndSubtypes;
967 }
968
969 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
970 boolean allowsImplicitlySelectedSubtypes) {
971 if (imi == null && mCurMethodId != null) {
972 imi = mMethodMap.get(mCurMethodId);
973 }
satok7265d9b2011-02-14 15:47:30 +0900974 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900975 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900976 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900977 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900978 }
satok7265d9b2011-02-14 15:47:30 +0900979 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900980 }
981
satoke7c6998e2011-06-03 17:57:59 +0900982 @Override
satok16331c82010-12-20 23:48:46 +0900983 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
984 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 // TODO: Make this work even for non-current users?
986 if (!calledFromValidUser()) {
987 return Collections.emptyList();
988 }
satok67ddf9c2010-11-17 09:45:54 +0900989 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900990 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900991 }
992 }
993
satoke7c6998e2011-06-03 17:57:59 +0900994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 public void addClient(IInputMethodClient client,
996 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900997 if (!calledFromValidUser()) {
998 return;
999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 synchronized (mMethodMap) {
1001 mClients.put(client.asBinder(), new ClientState(client,
1002 inputContext, uid, pid));
1003 }
1004 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001005
satoke7c6998e2011-06-03 17:57:59 +09001006 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001008 if (!calledFromValidUser()) {
1009 return;
1010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 synchronized (mMethodMap) {
1012 mClients.remove(client.asBinder());
1013 }
1014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 void executeOrSendMessage(IInterface target, Message msg) {
1017 if (target.asBinder() instanceof Binder) {
1018 mCaller.sendMessage(msg);
1019 } else {
1020 handleMessage(msg);
1021 msg.recycle();
1022 }
1023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001025 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001027 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 + mCurClient.client.asBinder());
1029 if (mBoundToMethod) {
1030 mBoundToMethod = false;
1031 if (mCurMethod != null) {
1032 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1033 MSG_UNBIND_INPUT, mCurMethod));
1034 }
1035 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001036
1037 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1038 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1040 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1041 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
The Android Open Source Project10592532009-03-18 17:39:46 -07001044 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 private int getImeShowFlags() {
1049 int flags = 0;
1050 if (mShowForced) {
1051 flags |= InputMethod.SHOW_FORCED
1052 | InputMethod.SHOW_EXPLICIT;
1053 } else if (mShowExplicitlyRequested) {
1054 flags |= InputMethod.SHOW_EXPLICIT;
1055 }
1056 return flags;
1057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 private int getAppShowFlags() {
1060 int flags = 0;
1061 if (mShowForced) {
1062 flags |= InputMethodManager.SHOW_FORCED;
1063 } else if (!mShowExplicitlyRequested) {
1064 flags |= InputMethodManager.SHOW_IMPLICIT;
1065 }
1066 return flags;
1067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001068
Dianne Hackborn7663d802012-02-24 13:08:49 -08001069 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 if (!mBoundToMethod) {
1071 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1072 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1073 mBoundToMethod = true;
1074 }
1075 final SessionState session = mCurClient.curSession;
1076 if (initial) {
1077 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1078 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1079 } else {
1080 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1081 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1082 }
1083 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001085 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001087 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001091 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 // If no method is currently selected, do nothing.
1093 if (mCurMethodId == null) {
1094 return mNoBinding;
1095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 ClientState cs = mClients.get(client.asBinder());
1098 if (cs == null) {
1099 throw new IllegalArgumentException("unknown client "
1100 + client.asBinder());
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 try {
1104 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1105 // Check with the window manager to make sure this client actually
1106 // has a window with focus. If not, reject. This is thread safe
1107 // because if the focus changes some time before or after, the
1108 // next client receiving focus that has any interest in input will
1109 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1112 return null;
1113 }
1114 } catch (RemoteException e) {
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Dianne Hackborn7663d802012-02-24 13:08:49 -08001117 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1118 }
1119
1120 InputBindResult startInputUncheckedLocked(ClientState cs,
1121 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1122 // If no method is currently selected, do nothing.
1123 if (mCurMethodId == null) {
1124 return mNoBinding;
1125 }
1126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 if (mCurClient != cs) {
1128 // If the client is changing, we need to switch over to the new
1129 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001130 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001131 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 + cs.client.asBinder());
1133
1134 // If the screen is on, inform the new client it is active
1135 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001136 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1137 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
1139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // Bump up the sequence for this client and attach it.
1142 mCurSeq++;
1143 if (mCurSeq <= 0) mCurSeq = 1;
1144 mCurClient = cs;
1145 mCurInputContext = inputContext;
1146 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // Check if the input method is changing.
1149 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1150 if (cs.curSession != null) {
1151 // Fast case: if we are already connected to the input method,
1152 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001153 return attachNewInputLocked(
1154 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156 if (mHaveConnection) {
1157 if (mCurMethod != null) {
1158 if (!cs.sessionRequested) {
1159 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001160 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1162 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001163 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
1165 // Return to client, and we will get back with it when
1166 // we have had a session made for it.
1167 return new InputBindResult(null, mCurId, mCurSeq);
1168 } else if (SystemClock.uptimeMillis()
1169 < (mLastBindTime+TIME_TO_RECONNECT)) {
1170 // In this case we have connected to the service, but
1171 // don't yet have its interface. If it hasn't been too
1172 // long since we did the connection, we'll return to
1173 // the client and wait to get the service interface so
1174 // we can report back. If it has been too long, we want
1175 // to fall through so we can try a disconnect/reconnect
1176 // to see if we can get back in touch with the service.
1177 return new InputBindResult(null, mCurId, mCurSeq);
1178 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1180 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182 }
1183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001185 return startInputInnerLocked();
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001188 InputBindResult startInputInnerLocked() {
1189 if (mCurMethodId == null) {
1190 return mNoBinding;
1191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001193 if (!mSystemReady) {
1194 // If the system is not yet ready, we shouldn't be running third
1195 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001196 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1200 if (info == null) {
1201 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001204 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1207 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001208 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1209 com.android.internal.R.string.input_method_binding_label);
1210 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1211 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001213 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 mLastBindTime = SystemClock.uptimeMillis();
1215 mHaveConnection = true;
1216 mCurId = info.getId();
1217 mCurToken = new Binder();
1218 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001219 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 mIWindowManager.addWindowToken(mCurToken,
1221 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1222 } catch (RemoteException e) {
1223 }
1224 return new InputBindResult(null, mCurId, mCurSeq);
1225 } else {
1226 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001227 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 + mCurIntent);
1229 }
1230 return null;
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
satoke7c6998e2011-06-03 17:57:59 +09001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001235 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001236 if (!calledFromValidUser()) {
1237 return null;
1238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (mMethodMap) {
1240 final long ident = Binder.clearCallingIdentity();
1241 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001242 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 } finally {
1244 Binder.restoreCallingIdentity(ident);
1245 }
1246 }
1247 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001248
satoke7c6998e2011-06-03 17:57:59 +09001249 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 public void finishInput(IInputMethodClient client) {
1251 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001252
satoke7c6998e2011-06-03 17:57:59 +09001253 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 public void onServiceConnected(ComponentName name, IBinder service) {
1255 synchronized (mMethodMap) {
1256 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1257 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001258 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001259 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001260 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001261 return;
1262 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001263 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001264 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1265 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001267 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001268 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001270 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001271 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
1273 }
1274 }
1275 }
1276
1277 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1278 synchronized (mMethodMap) {
1279 if (mCurMethod != null && method != null
1280 && mCurMethod.asBinder() == method.asBinder()) {
1281 if (mCurClient != null) {
1282 mCurClient.curSession = new SessionState(mCurClient,
1283 method, session);
1284 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001285 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 if (res.method != null) {
1287 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1288 MSG_BIND_METHOD, mCurClient.client, res));
1289 }
1290 }
1291 }
1292 }
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001295 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001296 if (mVisibleBound) {
1297 mContext.unbindService(mVisibleConnection);
1298 mVisibleBound = false;
1299 }
1300
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001301 if (mHaveConnection) {
1302 mContext.unbindService(this);
1303 mHaveConnection = false;
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001306 if (mCurToken != null) {
1307 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001309 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001310 // The current IME is shown. Hence an IME switch (transition) is happening.
1311 mWindowManagerService.saveLastInputMethodWindowForTransition();
1312 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001313 mIWindowManager.removeWindowToken(mCurToken);
1314 } catch (RemoteException e) {
1315 }
1316 mCurToken = null;
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Project10592532009-03-18 17:39:46 -07001319 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001320 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001322 if (reportToClient && mCurClient != null) {
1323 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1324 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1325 }
1326 }
1327
Devin Taylor0c33ed22010-02-23 13:26:46 -06001328 private void finishSession(SessionState sessionState) {
1329 if (sessionState != null && sessionState.session != null) {
1330 try {
1331 sessionState.session.finishSession();
1332 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001333 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001334 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001335 }
1336 }
1337 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001339 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 if (mCurMethod != null) {
1341 for (ClientState cs : mClients.values()) {
1342 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001343 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 cs.curSession = null;
1345 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001346
1347 finishSession(mEnabledSession);
1348 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 mCurMethod = null;
1350 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001351 if (mStatusBar != null) {
1352 mStatusBar.setIconVisibility("ime", false);
1353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
satoke7c6998e2011-06-03 17:57:59 +09001356 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 public void onServiceDisconnected(ComponentName name) {
1358 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001359 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 + " mCurIntent=" + mCurIntent);
1361 if (mCurMethod != null && mCurIntent != null
1362 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001363 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 // We consider this to be a new bind attempt, since the system
1365 // should now try to restart the service for us.
1366 mLastBindTime = SystemClock.uptimeMillis();
1367 mShowRequested = mInputShown;
1368 mInputShown = false;
1369 if (mCurClient != null) {
1370 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1371 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1372 }
1373 }
1374 }
1375 }
1376
satokf9f01002011-05-19 21:31:50 +09001377 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001379 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 long ident = Binder.clearCallingIdentity();
1381 try {
1382 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001383 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 return;
1385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 synchronized (mMethodMap) {
1388 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001389 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001390 if (mStatusBar != null) {
1391 mStatusBar.setIconVisibility("ime", false);
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001394 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001395 CharSequence contentDescription = null;
1396 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001397 // Use PackageManager to load label
1398 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001399 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001400 mIPackageManager.getApplicationInfo(packageName, 0,
1401 mSettings.getCurrentUserId()));
1402 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001403 /* ignore */
1404 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001405 if (mStatusBar != null) {
1406 mStatusBar.setIcon("ime", packageName, iconId, 0,
1407 contentDescription != null
1408 ? contentDescription.toString() : null);
1409 mStatusBar.setIconVisibility("ime", true);
1410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
1412 }
1413 } finally {
1414 Binder.restoreCallingIdentity(ident);
1415 }
1416 }
1417
satok7cfc0ed2011-06-20 21:29:36 +09001418 private boolean needsToShowImeSwitchOngoingNotification() {
1419 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001420 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001421 synchronized (mMethodMap) {
1422 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1423 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001424 if (N > 2) return true;
1425 if (N < 1) return false;
1426 int nonAuxCount = 0;
1427 int auxCount = 0;
1428 InputMethodSubtype nonAuxSubtype = null;
1429 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001430 for(int i = 0; i < N; ++i) {
1431 final InputMethodInfo imi = imis.get(i);
1432 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1433 imi, true);
1434 final int subtypeCount = subtypes.size();
1435 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001436 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001437 } else {
1438 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001439 final InputMethodSubtype subtype = subtypes.get(j);
1440 if (!subtype.isAuxiliary()) {
1441 ++nonAuxCount;
1442 nonAuxSubtype = subtype;
1443 } else {
1444 ++auxCount;
1445 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001446 }
1447 }
1448 }
satok7cfc0ed2011-06-20 21:29:36 +09001449 }
satokb6359412011-06-28 17:47:41 +09001450 if (nonAuxCount > 1 || auxCount > 1) {
1451 return true;
1452 } else if (nonAuxCount == 1 && auxCount == 1) {
1453 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001454 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1455 || auxSubtype.overridesImplicitlyEnabledSubtype()
1456 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001457 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1458 return false;
1459 }
1460 return true;
1461 }
1462 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001463 }
satok7cfc0ed2011-06-20 21:29:36 +09001464 }
1465
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001466 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001467 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001468 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001469 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001470 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001471 try {
1472 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001473 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001474 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001475 return;
1476 }
1477
1478 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001479 mImeWindowVis = vis;
1480 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001481 if (mStatusBar != null) {
1482 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1483 }
satok7cfc0ed2011-06-20 21:29:36 +09001484 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001485 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1486 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001487 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001488 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001489 final CharSequence title = mRes.getText(
1490 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001491 final CharSequence imiLabel = imi.loadLabel(pm);
1492 final CharSequence summary = mCurrentSubtype != null
1493 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1494 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1495 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001496 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001497 : imiLabel;
1498
satok7cfc0ed2011-06-20 21:29:36 +09001499 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001500 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001501 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001502 if (DEBUG) {
1503 Slog.d(TAG, "--- show notification: label = " + imiLabel
1504 + ", summary = " + summary);
1505 }
1506 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001507 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001508 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001509 mNotificationShown = true;
1510 }
satok7cfc0ed2011-06-20 21:29:36 +09001511 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001512 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001513 if (DEBUG) {
1514 Slog.d(TAG, "--- hide notification");
1515 }
1516 mNotificationManager.cancelAsUser(null,
1517 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001518 mNotificationShown = false;
1519 }
1520 }
satok06487a52010-10-29 11:37:18 +09001521 }
1522 } finally {
1523 Binder.restoreCallingIdentity(ident);
1524 }
1525 }
1526
satoke7c6998e2011-06-03 17:57:59 +09001527 @Override
satokf9f01002011-05-19 21:31:50 +09001528 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001529 if (!calledFromValidUser()) {
1530 return;
1531 }
satokf9f01002011-05-19 21:31:50 +09001532 synchronized (mMethodMap) {
1533 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1534 for (int i = 0; i < spans.length; ++i) {
1535 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001536 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001537 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001538 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001539 }
1540 }
1541 }
1542 }
1543
satoke7c6998e2011-06-03 17:57:59 +09001544 @Override
satokf9f01002011-05-19 21:31:50 +09001545 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001546 if (!calledFromValidUser()) {
1547 return false;
1548 }
satokf9f01002011-05-19 21:31:50 +09001549 synchronized (mMethodMap) {
1550 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1551 // TODO: Do not send the intent if the process of the targetImi is already dead.
1552 if (targetImi != null) {
1553 final String[] suggestions = span.getSuggestions();
1554 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001555 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001556 final Intent intent = new Intent();
1557 // Ensures that only a class in the original IME package will receive the
1558 // notification.
satok42c5a162011-05-26 16:46:14 +09001559 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001560 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1561 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1562 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1563 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001564 final long ident = Binder.clearCallingIdentity();
1565 try {
1566 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1567 } finally {
1568 Binder.restoreCallingIdentity(ident);
1569 }
satokf9f01002011-05-19 21:31:50 +09001570 return true;
1571 }
1572 }
1573 return false;
1574 }
1575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001577 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1578 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1579 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1580 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001581 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001582 // There is no input method selected, try to choose new applicable input method.
1583 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001584 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001585 }
1586 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 try {
satokab751aa2010-09-14 19:17:36 +09001588 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001590 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001591 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001592 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
satokf3db1af2010-11-23 13:34:33 +09001594 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001595 } else {
1596 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001597 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001598 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001601
satokab751aa2010-09-14 19:17:36 +09001602 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 InputMethodInfo info = mMethodMap.get(id);
1604 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001605 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
satokd81e9502012-05-21 12:58:45 +09001608 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001610 final int subtypeCount = info.getSubtypeCount();
1611 if (subtypeCount <= 0) {
1612 return;
satokcd7cd292010-11-20 15:46:23 +09001613 }
satokd81e9502012-05-21 12:58:45 +09001614 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1615 final InputMethodSubtype newSubtype;
1616 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1617 newSubtype = info.getSubtypeAt(subtypeId);
1618 } else {
1619 // If subtype is null, try to find the most applicable one from
1620 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001621 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001622 }
1623 if (newSubtype == null || oldSubtype == null) {
1624 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1625 + ", new subtype = " + newSubtype);
1626 return;
1627 }
1628 if (newSubtype != oldSubtype) {
1629 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1630 if (mCurMethod != null) {
1631 try {
1632 refreshImeWindowVisibilityLocked();
1633 mCurMethod.changeInputMethodSubtype(newSubtype);
1634 } catch (RemoteException e) {
1635 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001636 }
1637 }
1638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 return;
1640 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001641
satokd81e9502012-05-21 12:58:45 +09001642 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 final long ident = Binder.clearCallingIdentity();
1644 try {
satokab751aa2010-09-14 19:17:36 +09001645 // Set a subtype to this input method.
1646 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001647 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1648 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1649 // because mCurMethodId is stored as a history in
1650 // setSelectedInputMethodAndSubtypeLocked().
1651 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652
1653 if (ActivityManagerNative.isSystemReady()) {
1654 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001655 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001657 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001659 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 } finally {
1661 Binder.restoreCallingIdentity(ident);
1662 }
1663 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001664
satok42c5a162011-05-26 16:46:14 +09001665 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001666 public boolean showSoftInput(IInputMethodClient client, int flags,
1667 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001668 if (!calledFromValidUser()) {
1669 return false;
1670 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001671 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 long ident = Binder.clearCallingIdentity();
1673 try {
1674 synchronized (mMethodMap) {
1675 if (mCurClient == null || client == null
1676 || mCurClient.client.asBinder() != client.asBinder()) {
1677 try {
1678 // We need to check if this is the current client with
1679 // focus in the window manager, to allow this call to
1680 // be made before input is started in it.
1681 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001682 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001683 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001686 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
1688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001689
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001691 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693 } finally {
1694 Binder.restoreCallingIdentity(ident);
1695 }
1696 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001697
The Android Open Source Project4df24232009-03-05 14:34:35 -08001698 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 mShowRequested = true;
1700 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1701 mShowExplicitlyRequested = true;
1702 }
1703 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1704 mShowExplicitlyRequested = true;
1705 mShowForced = true;
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
Dianne Hackborncc278702009-09-02 23:07:23 -07001708 if (!mSystemReady) {
1709 return false;
1710 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001711
The Android Open Source Project4df24232009-03-05 14:34:35 -08001712 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001714 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1715 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1716 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001718 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001719 bindCurrentInputMethodService(
1720 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001721 mVisibleBound = true;
1722 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001723 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001725 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 // The client has asked to have the input method shown, but
1727 // we have been sitting here too long with a connection to the
1728 // service and no interface received, so let's disconnect/connect
1729 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001730 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001732 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001734 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001735 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001736 } else {
1737 if (DEBUG) {
1738 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1739 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
The Android Open Source Project4df24232009-03-05 14:34:35 -08001743 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001745
satok42c5a162011-05-26 16:46:14 +09001746 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001747 public boolean hideSoftInput(IInputMethodClient client, int flags,
1748 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001749 if (!calledFromValidUser()) {
1750 return false;
1751 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001752 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 long ident = Binder.clearCallingIdentity();
1754 try {
1755 synchronized (mMethodMap) {
1756 if (mCurClient == null || client == null
1757 || mCurClient.client.asBinder() != client.asBinder()) {
1758 try {
1759 // We need to check if this is the current client with
1760 // focus in the window manager, to allow this call to
1761 // be made before input is started in it.
1762 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001763 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1764 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001765 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001766 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001769 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001770 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001773
Joe Onorato8a9b2202010-02-26 18:56:32 -08001774 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001775 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
1777 } finally {
1778 Binder.restoreCallingIdentity(ident);
1779 }
1780 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001781
The Android Open Source Project4df24232009-03-05 14:34:35 -08001782 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1784 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001785 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001790 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001794 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001796 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1797 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1798 res = true;
1799 } else {
1800 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001802 if (mHaveConnection && mVisibleBound) {
1803 mContext.unbindService(mVisibleConnection);
1804 mVisibleBound = false;
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 mInputShown = false;
1807 mShowRequested = false;
1808 mShowExplicitlyRequested = false;
1809 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001812
satok42c5a162011-05-26 16:46:14 +09001813 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001814 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1815 int controlFlags, int softInputMode, int windowFlags,
1816 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001817 if (!calledFromValidUser()) {
1818 return null;
1819 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001820 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 long ident = Binder.clearCallingIdentity();
1822 try {
1823 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001824 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001825 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001827 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001828
Dianne Hackborn7663d802012-02-24 13:08:49 -08001829 ClientState cs = mClients.get(client.asBinder());
1830 if (cs == null) {
1831 throw new IllegalArgumentException("unknown client "
1832 + client.asBinder());
1833 }
1834
1835 try {
1836 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1837 // Check with the window manager to make sure this client actually
1838 // has a window with focus. If not, reject. This is thread safe
1839 // because if the focus changes some time before or after, the
1840 // next client receiving focus that has any interest in input will
1841 // be calling through here after that change happens.
1842 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1843 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1844 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001846 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001848
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001849 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001850 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001851 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001852 if (attribute != null) {
1853 return startInputUncheckedLocked(cs, inputContext, attribute,
1854 controlFlags);
1855 }
1856 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001857 }
1858 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001859
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001860 // Should we auto-show the IME even if the caller has not
1861 // specified what should be done with it?
1862 // We only do this automatically if the window can resize
1863 // to accommodate the IME (so what the user sees will give
1864 // them good context without input information being obscured
1865 // by the IME) or if running on a large screen where there
1866 // is more room for the target window + IME.
1867 final boolean doAutoShow =
1868 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1869 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1870 || mRes.getConfiguration().isLayoutSizeAtLeast(
1871 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001872 final boolean isTextEditor =
1873 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1874
1875 // We want to start input before showing the IME, but after closing
1876 // it. We want to do this after closing it to help the IME disappear
1877 // more quickly (not get stuck behind it initializing itself for the
1878 // new focused input, even if its window wants to hide the IME).
1879 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1882 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001883 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1885 // There is no focus view, and this window will
1886 // be behind any soft input window, so hide the
1887 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001888 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001889 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001891 } else if (isTextEditor && doAutoShow && (softInputMode &
1892 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 // There is a focus view, and we are navigating forward
1894 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001895 // We only do this automatically if the window can resize
1896 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001897 // them good context without input information being obscured
1898 // by the IME) or if running on a large screen where there
1899 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001900 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001901 if (attribute != null) {
1902 res = startInputUncheckedLocked(cs, inputContext, attribute,
1903 controlFlags);
1904 didStart = true;
1905 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001906 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 break;
1909 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1910 // Do nothing.
1911 break;
1912 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1913 if ((softInputMode &
1914 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001915 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001916 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918 break;
1919 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001920 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001921 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 break;
1923 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1924 if ((softInputMode &
1925 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001926 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001927 if (attribute != null) {
1928 res = startInputUncheckedLocked(cs, inputContext, attribute,
1929 controlFlags);
1930 didStart = true;
1931 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001932 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 break;
1935 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001936 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001937 if (attribute != null) {
1938 res = startInputUncheckedLocked(cs, inputContext, attribute,
1939 controlFlags);
1940 didStart = true;
1941 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001942 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 break;
1944 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001945
1946 if (!didStart && attribute != null) {
1947 res = startInputUncheckedLocked(cs, inputContext, attribute,
1948 controlFlags);
1949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
1951 } finally {
1952 Binder.restoreCallingIdentity(ident);
1953 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001954
1955 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001957
satok42c5a162011-05-26 16:46:14 +09001958 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001960 if (!calledFromValidUser()) {
1961 return;
1962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 synchronized (mMethodMap) {
1964 if (mCurClient == null || client == null
1965 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001966 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001967 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969
satok440aab52010-11-25 09:43:11 +09001970 // Always call subtype picker, because subtype picker is a superset of input method
1971 // picker.
satokab751aa2010-09-14 19:17:36 +09001972 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1973 }
1974 }
1975
satok42c5a162011-05-26 16:46:14 +09001976 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001978 if (!calledFromValidUser()) {
1979 return;
1980 }
satok28203512010-11-24 11:06:49 +09001981 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1982 }
1983
satok42c5a162011-05-26 16:46:14 +09001984 @Override
satok28203512010-11-24 11:06:49 +09001985 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001986 if (!calledFromValidUser()) {
1987 return;
1988 }
satok28203512010-11-24 11:06:49 +09001989 synchronized (mMethodMap) {
1990 if (subtype != null) {
1991 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
1992 mMethodMap.get(id), subtype.hashCode()));
1993 } else {
1994 setInputMethod(token, id);
1995 }
1996 }
satokab751aa2010-09-14 19:17:36 +09001997 }
1998
satok42c5a162011-05-26 16:46:14 +09001999 @Override
satokb416a712010-11-25 20:42:14 +09002000 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002001 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002002 if (!calledFromValidUser()) {
2003 return;
2004 }
satokb416a712010-11-25 20:42:14 +09002005 synchronized (mMethodMap) {
2006 if (mCurClient == null || client == null
2007 || mCurClient.client.asBinder() != client.asBinder()) {
2008 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2009 }
satok7fee71f2010-12-17 18:54:26 +09002010 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2011 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002012 }
2013 }
2014
satok4fc87d62011-05-20 16:13:43 +09002015 @Override
satok735cf382010-11-11 20:40:09 +09002016 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002017 if (!calledFromValidUser()) {
2018 return false;
2019 }
satok735cf382010-11-11 20:40:09 +09002020 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002021 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002022 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002023 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002024 lastImi = mMethodMap.get(lastIme.first);
2025 } else {
2026 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002027 }
satok4fc87d62011-05-20 16:13:43 +09002028 String targetLastImiId = null;
2029 int subtypeId = NOT_A_SUBTYPE_ID;
2030 if (lastIme != null && lastImi != null) {
2031 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2032 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2033 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2034 : mCurrentSubtype.hashCode();
2035 // If the last IME is the same as the current IME and the last subtype is not
2036 // defined, there is no need to switch to the last IME.
2037 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2038 targetLastImiId = lastIme.first;
2039 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2040 }
2041 }
2042
2043 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2044 // This is a safety net. If the currentSubtype can't be added to the history
2045 // and the framework couldn't find the last ime, we will make the last ime be
2046 // the most applicable enabled keyboard subtype of the system imes.
2047 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2048 if (enabled != null) {
2049 final int N = enabled.size();
2050 final String locale = mCurrentSubtype == null
2051 ? mRes.getConfiguration().locale.toString()
2052 : mCurrentSubtype.getLocale();
2053 for (int i = 0; i < N; ++i) {
2054 final InputMethodInfo imi = enabled.get(i);
2055 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2056 InputMethodSubtype keyboardSubtype =
2057 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2058 SUBTYPE_MODE_KEYBOARD, locale, true);
2059 if (keyboardSubtype != null) {
2060 targetLastImiId = imi.getId();
2061 subtypeId = getSubtypeIdFromHashCode(
2062 imi, keyboardSubtype.hashCode());
2063 if(keyboardSubtype.getLocale().equals(locale)) {
2064 break;
2065 }
2066 }
2067 }
2068 }
2069 }
2070 }
2071
2072 if (!TextUtils.isEmpty(targetLastImiId)) {
2073 if (DEBUG) {
2074 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2075 + ", from: " + mCurMethodId + ", " + subtypeId);
2076 }
2077 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2078 return true;
2079 } else {
2080 return false;
2081 }
satok735cf382010-11-11 20:40:09 +09002082 }
2083 }
2084
satoke7c6998e2011-06-03 17:57:59 +09002085 @Override
satok688bd472012-02-09 20:09:17 +09002086 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002087 if (!calledFromValidUser()) {
2088 return false;
2089 }
satok688bd472012-02-09 20:09:17 +09002090 synchronized (mMethodMap) {
2091 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2092 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2093 if (nextSubtype == null) {
2094 return false;
2095 }
2096 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2097 return true;
2098 }
2099 }
2100
2101 @Override
satok68f1b782011-04-11 14:26:04 +09002102 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002103 if (!calledFromValidUser()) {
2104 return null;
2105 }
satok68f1b782011-04-11 14:26:04 +09002106 synchronized (mMethodMap) {
2107 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2108 // TODO: Handle the case of the last IME with no subtypes
2109 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2110 || TextUtils.isEmpty(lastIme.second)) return null;
2111 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2112 if (lastImi == null) return null;
2113 try {
2114 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002115 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2116 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2117 return null;
2118 }
2119 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002120 } catch (NumberFormatException e) {
2121 return null;
2122 }
2123 }
2124 }
2125
satoke7c6998e2011-06-03 17:57:59 +09002126 @Override
satokee5e77c2011-09-02 18:50:15 +09002127 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002128 if (!calledFromValidUser()) {
2129 return;
2130 }
satok91e88122011-07-18 11:11:42 +09002131 // By this IPC call, only a process which shares the same uid with the IME can add
2132 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002133 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002134 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002135 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002136 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002137 final String[] packageInfos;
2138 try {
2139 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2140 } catch (RemoteException e) {
2141 Slog.e(TAG, "Failed to get package infos");
2142 return;
2143 }
satok91e88122011-07-18 11:11:42 +09002144 if (packageInfos != null) {
2145 final int packageNum = packageInfos.length;
2146 for (int i = 0; i < packageNum; ++i) {
2147 if (packageInfos[i].equals(imi.getPackageName())) {
2148 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002149 final long ident = Binder.clearCallingIdentity();
2150 try {
2151 buildInputMethodListLocked(mMethodList, mMethodMap);
2152 } finally {
2153 Binder.restoreCallingIdentity(ident);
2154 }
satokee5e77c2011-09-02 18:50:15 +09002155 return;
satok91e88122011-07-18 11:11:42 +09002156 }
2157 }
2158 }
satoke7c6998e2011-06-03 17:57:59 +09002159 }
satokee5e77c2011-09-02 18:50:15 +09002160 return;
satoke7c6998e2011-06-03 17:57:59 +09002161 }
2162
satok28203512010-11-24 11:06:49 +09002163 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 synchronized (mMethodMap) {
2165 if (token == null) {
2166 if (mContext.checkCallingOrSelfPermission(
2167 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2168 != PackageManager.PERMISSION_GRANTED) {
2169 throw new SecurityException(
2170 "Using null token requires permission "
2171 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2172 }
2173 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002174 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2175 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 return;
2177 }
2178
satokc5933802011-08-31 21:26:04 +09002179 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 try {
satokab751aa2010-09-14 19:17:36 +09002181 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 } finally {
2183 Binder.restoreCallingIdentity(ident);
2184 }
2185 }
2186 }
2187
satok42c5a162011-05-26 16:46:14 +09002188 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002190 if (!calledFromValidUser()) {
2191 return;
2192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 synchronized (mMethodMap) {
2194 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002195 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2196 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 return;
2198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 long ident = Binder.clearCallingIdentity();
2200 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002201 hideCurrentInputLocked(flags, null);
2202 } finally {
2203 Binder.restoreCallingIdentity(ident);
2204 }
2205 }
2206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002207
satok42c5a162011-05-26 16:46:14 +09002208 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002209 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002210 if (!calledFromValidUser()) {
2211 return;
2212 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002213 synchronized (mMethodMap) {
2214 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002215 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2216 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002217 return;
2218 }
2219 long ident = Binder.clearCallingIdentity();
2220 try {
2221 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 } finally {
2223 Binder.restoreCallingIdentity(ident);
2224 }
2225 }
2226 }
2227
2228 void setEnabledSessionInMainThread(SessionState session) {
2229 if (mEnabledSession != session) {
2230 if (mEnabledSession != null) {
2231 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002232 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 mEnabledSession.method.setSessionEnabled(
2234 mEnabledSession.session, false);
2235 } catch (RemoteException e) {
2236 }
2237 }
2238 mEnabledSession = session;
2239 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002240 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 session.method.setSessionEnabled(
2242 session.session, true);
2243 } catch (RemoteException e) {
2244 }
2245 }
2246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002247
satok42c5a162011-05-26 16:46:14 +09002248 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002250 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 switch (msg.what) {
2252 case MSG_SHOW_IM_PICKER:
2253 showInputMethodMenu();
2254 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002255
satokab751aa2010-09-14 19:17:36 +09002256 case MSG_SHOW_IM_SUBTYPE_PICKER:
2257 showInputMethodSubtypeMenu();
2258 return true;
2259
satok47a44912010-10-06 16:03:58 +09002260 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002261 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002262 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002263 args.recycle();
satok217f5482010-12-15 05:19:19 +09002264 return true;
2265
2266 case MSG_SHOW_IM_CONFIG:
2267 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002268 return true;
2269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 case MSG_UNBIND_INPUT:
2273 try {
2274 ((IInputMethod)msg.obj).unbindInput();
2275 } catch (RemoteException e) {
2276 // There is nothing interesting about the method dying.
2277 }
2278 return true;
2279 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002280 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 try {
2282 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2283 } catch (RemoteException e) {
2284 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002285 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 return true;
2287 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002288 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002290 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2291 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 } catch (RemoteException e) {
2293 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002294 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295 return true;
2296 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002297 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002299 ((IInputMethod)args.arg1).hideSoftInput(0,
2300 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 } catch (RemoteException e) {
2302 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002303 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 return true;
2305 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002306 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002308 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2310 } catch (RemoteException e) {
2311 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002312 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 return true;
2314 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002315 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 try {
2317 ((IInputMethod)args.arg1).createSession(
2318 (IInputMethodCallback)args.arg2);
2319 } catch (RemoteException e) {
2320 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002321 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002326 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 try {
2328 SessionState session = (SessionState)args.arg1;
2329 setEnabledSessionInMainThread(session);
2330 session.method.startInput((IInputContext)args.arg2,
2331 (EditorInfo)args.arg3);
2332 } catch (RemoteException e) {
2333 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002334 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 return true;
2336 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002337 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 try {
2339 SessionState session = (SessionState)args.arg1;
2340 setEnabledSessionInMainThread(session);
2341 session.method.restartInput((IInputContext)args.arg2,
2342 (EditorInfo)args.arg3);
2343 } catch (RemoteException e) {
2344 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002345 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 case MSG_UNBIND_METHOD:
2351 try {
2352 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2353 } catch (RemoteException e) {
2354 // There is nothing interesting about the last client dying.
2355 }
2356 return true;
2357 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002358 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 try {
2360 ((IInputMethodClient)args.arg1).onBindMethod(
2361 (InputBindResult)args.arg2);
2362 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002363 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002365 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002367 case MSG_SET_ACTIVE:
2368 try {
2369 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2370 } catch (RemoteException e) {
2371 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2372 + ((ClientState)msg.obj).pid + " uid "
2373 + ((ClientState)msg.obj).uid);
2374 }
2375 return true;
satok01038492012-04-09 21:08:27 +09002376
2377 // --------------------------------------------------------------
2378 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2379 mHardKeyboardListener.handleHardKeyboardStatusChange(
2380 msg.arg1 == 1, msg.arg2 == 1);
2381 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 }
2383 return false;
2384 }
2385
satok5b927c432012-05-01 20:09:34 +09002386 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002387 return (inputMethod.getServiceInfo().applicationInfo.flags
2388 & ApplicationInfo.FLAG_SYSTEM) != 0;
2389 }
2390
Ken Wakasa586f0512011-01-20 22:31:01 +09002391 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2392 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2393 final int subtypeCount = imi.getSubtypeCount();
2394 for (int i = 0; i < subtypeCount; ++i) {
2395 subtypes.add(imi.getSubtypeAt(i));
2396 }
2397 return subtypes;
2398 }
2399
satoka86f5e42011-09-02 17:12:42 +09002400 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2401 InputMethodInfo imi, String mode) {
2402 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2403 final int subtypeCount = imi.getSubtypeCount();
2404 for (int i = 0; i < subtypeCount; ++i) {
2405 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2406 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2407 subtypes.add(subtype);
2408 }
2409 }
2410 return subtypes;
2411 }
2412
satokdc9ddae2011-10-06 12:22:36 +09002413 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002414 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002415 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002416 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002417 int i = enabled.size();
2418 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002419 while (i > 0) {
2420 i--;
satokdc9ddae2011-10-06 12:22:36 +09002421 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002422 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2423 return imi;
2424 }
2425 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2426 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002427 }
2428 }
satok0a1bcf42012-05-16 19:26:31 +09002429 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002430 }
2431 return null;
2432 }
2433
2434 private boolean chooseNewDefaultIMELocked() {
2435 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2436 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002437 if (DEBUG) {
2438 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2439 }
satok723a27e2010-11-11 14:58:11 +09002440 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002441 return true;
2442 }
2443
2444 return false;
2445 }
2446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2448 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002449 if (DEBUG) {
2450 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 list.clear();
2453 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002454
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 // Use for queryIntentServicesAsUser
2456 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002457 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002458 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002459 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002460 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002462 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002464 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002465
satoke7c6998e2011-06-03 17:57:59 +09002466 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2467 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 for (int i = 0; i < services.size(); ++i) {
2469 ResolveInfo ri = services.get(i);
2470 ServiceInfo si = ri.serviceInfo;
2471 ComponentName compName = new ComponentName(si.packageName, si.name);
2472 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2473 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002474 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 + ": it does not require the permission "
2476 + android.Manifest.permission.BIND_INPUT_METHOD);
2477 continue;
2478 }
2479
Joe Onorato8a9b2202010-02-26 18:56:32 -08002480 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481
2482 try {
satoke7c6998e2011-06-03 17:57:59 +09002483 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002485 final String id = p.getId();
2486 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487
satok5b927c432012-05-01 20:09:34 +09002488 // Valid system default IMEs and IMEs that have English subtypes are enabled
2489 // by default, unless there's a hard keyboard and the system IME was explicitly
2490 // disabled
2491 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))
2492 && (!haveHardKeyboard || disabledSysImes.indexOf(id) < 0)) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002493 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002494 }
2495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002497 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002501 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002503 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 }
2505 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002506
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002507 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002508 if (!TextUtils.isEmpty(defaultImiId)) {
2509 if (!map.containsKey(defaultImiId)) {
2510 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2511 if (chooseNewDefaultIMELocked()) {
2512 updateFromSettingsLocked();
2513 }
2514 } else {
2515 // Double check that the default IME is certainly enabled.
2516 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002517 }
2518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002522
satokab751aa2010-09-14 19:17:36 +09002523 private void showInputMethodMenu() {
2524 showInputMethodMenuInternal(false);
2525 }
2526
2527 private void showInputMethodSubtypeMenu() {
2528 showInputMethodMenuInternal(true);
2529 }
2530
satok217f5482010-12-15 05:19:19 +09002531 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002532 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002533 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002534 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2535 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002536 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002537 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002538 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002539 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002540 }
2541
2542 private void showConfigureInputMethods() {
2543 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2544 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2545 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2546 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002547 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002548 }
2549
satok2c93efc2012-04-02 19:33:47 +09002550 private boolean isScreenLocked() {
2551 return mKeyguardManager != null
2552 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2553 }
satokab751aa2010-09-14 19:17:36 +09002554 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002555 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002558 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002559
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002560 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002561 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002562 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002563
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002564 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002565 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2566 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002567 if (immis == null || immis.size() == 0) {
2568 return;
2569 }
2570
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002571 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572
satok688bd472012-02-09 20:09:17 +09002573 final List<ImeSubtypeListItem> imList =
2574 mImListManager.getSortedInputMethodAndSubtypeList(
2575 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002576
satokc3690562012-01-10 20:14:43 +09002577 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002578 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002579 if (currentSubtype != null) {
2580 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2581 lastInputMethodSubtypeId =
2582 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2583 }
2584 }
2585
Ken Wakasa761eb372011-03-04 19:06:18 +09002586 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002587 mIms = new InputMethodInfo[N];
2588 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002589 int checkedItem = 0;
2590 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002591 final ImeSubtypeListItem item = imList.get(i);
2592 mIms[i] = item.mImi;
2593 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002594 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002595 int subtypeId = mSubtypeIds[i];
2596 if ((subtypeId == NOT_A_SUBTYPE_ID)
2597 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2598 || (subtypeId == lastInputMethodSubtypeId)) {
2599 checkedItem = i;
2600 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002603 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002604 com.android.internal.R.styleable.DialogPreference,
2605 com.android.internal.R.attr.alertDialogStyle, 0);
2606 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002607 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002608 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002609 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002611 }
2612 })
2613 .setIcon(a.getDrawable(
2614 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2615 a.recycle();
satok01038492012-04-09 21:08:27 +09002616 final LayoutInflater inflater =
2617 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2618 final View tv = inflater.inflate(
2619 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2620 mDialogBuilder.setCustomTitle(tv);
2621
2622 // Setup layout for a toggle switch of the hardware keyboard
2623 mSwitchingDialogTitleView = tv;
2624 mSwitchingDialogTitleView.findViewById(
2625 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2626 mWindowManagerService.isHardKeyboardAvailable() ?
2627 View.VISIBLE : View.GONE);
2628 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2629 com.android.internal.R.id.hard_keyboard_switch));
2630 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2631 hardKeySwitch.setOnCheckedChangeListener(
2632 new OnCheckedChangeListener() {
2633 @Override
2634 public void onCheckedChanged(
2635 CompoundButton buttonView, boolean isChecked) {
2636 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2637 }
2638 });
satokd87c2592010-09-29 11:52:06 +09002639
Ken Wakasa05dbb652011-08-22 15:22:43 +09002640 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2641 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2642 checkedItem);
2643
2644 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002645 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002646 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002647 public void onClick(DialogInterface dialog, int which) {
2648 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002649 if (mIms == null || mIms.length <= which
2650 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002651 return;
2652 }
2653 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002654 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002655 adapter.mCheckedItem = which;
2656 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002657 hideInputMethodMenu();
2658 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002659 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002660 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002661 subtypeId = NOT_A_SUBTYPE_ID;
2662 }
2663 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002664 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002667 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668
satokbc81b692011-08-26 16:22:22 +09002669 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002670 mDialogBuilder.setPositiveButton(
2671 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002672 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002673 @Override
satok7f35c8c2010-10-07 21:13:11 +09002674 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002675 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002676 }
2677 });
2678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002680 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 mSwitchingDialog.getWindow().setType(
2682 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002683 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2684 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002685 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 mSwitchingDialog.show();
2687 }
2688 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002689
satok93d744d2012-05-09 17:14:08 +09002690 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002691 public final CharSequence mImeName;
2692 public final CharSequence mSubtypeName;
2693 public final InputMethodInfo mImi;
2694 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002695 private final boolean mIsSystemLocale;
2696 private final boolean mIsSystemLanguage;
2697
Ken Wakasa05dbb652011-08-22 15:22:43 +09002698 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002699 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002700 mImeName = imeName;
2701 mSubtypeName = subtypeName;
2702 mImi = imi;
2703 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002704 if (TextUtils.isEmpty(subtypeLocale)) {
2705 mIsSystemLocale = false;
2706 mIsSystemLanguage = false;
2707 } else {
2708 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2709 mIsSystemLanguage = mIsSystemLocale
2710 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2711 }
2712 }
2713
2714 @Override
2715 public int compareTo(ImeSubtypeListItem other) {
2716 if (TextUtils.isEmpty(mImeName)) {
2717 return 1;
2718 }
2719 if (TextUtils.isEmpty(other.mImeName)) {
2720 return -1;
2721 }
2722 if (!TextUtils.equals(mImeName, other.mImeName)) {
2723 return mImeName.toString().compareTo(other.mImeName.toString());
2724 }
2725 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2726 return 0;
2727 }
2728 if (mIsSystemLocale) {
2729 return -1;
2730 }
2731 if (other.mIsSystemLocale) {
2732 return 1;
2733 }
2734 if (mIsSystemLanguage) {
2735 return -1;
2736 }
2737 if (other.mIsSystemLanguage) {
2738 return 1;
2739 }
2740 if (TextUtils.isEmpty(mSubtypeName)) {
2741 return 1;
2742 }
2743 if (TextUtils.isEmpty(other.mSubtypeName)) {
2744 return -1;
2745 }
2746 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002747 }
2748 }
2749
2750 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2751 private final LayoutInflater mInflater;
2752 private final int mTextViewResourceId;
2753 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002754 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002755 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2756 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2757 super(context, textViewResourceId, itemsList);
2758 mTextViewResourceId = textViewResourceId;
2759 mItemsList = itemsList;
2760 mCheckedItem = checkedItem;
2761 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2762 }
2763
2764 @Override
2765 public View getView(int position, View convertView, ViewGroup parent) {
2766 final View view = convertView != null ? convertView
2767 : mInflater.inflate(mTextViewResourceId, null);
2768 if (position < 0 || position >= mItemsList.size()) return view;
2769 final ImeSubtypeListItem item = mItemsList.get(position);
2770 final CharSequence imeName = item.mImeName;
2771 final CharSequence subtypeName = item.mSubtypeName;
2772 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2773 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2774 if (TextUtils.isEmpty(subtypeName)) {
2775 firstTextView.setText(imeName);
2776 secondTextView.setVisibility(View.GONE);
2777 } else {
2778 firstTextView.setText(subtypeName);
2779 secondTextView.setText(imeName);
2780 secondTextView.setVisibility(View.VISIBLE);
2781 }
2782 final RadioButton radioButton =
2783 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2784 radioButton.setChecked(position == mCheckedItem);
2785 return view;
2786 }
2787 }
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002790 synchronized (mMethodMap) {
2791 hideInputMethodMenuLocked();
2792 }
2793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002794
The Android Open Source Project10592532009-03-18 17:39:46 -07002795 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002796 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797
The Android Open Source Project10592532009-03-18 17:39:46 -07002798 if (mSwitchingDialog != null) {
2799 mSwitchingDialog.dismiss();
2800 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002802
The Android Open Source Project10592532009-03-18 17:39:46 -07002803 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002804 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002808
satok42c5a162011-05-26 16:46:14 +09002809 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002811 // TODO: Make this work even for non-current users?
2812 if (!calledFromValidUser()) {
2813 return false;
2814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 synchronized (mMethodMap) {
2816 if (mContext.checkCallingOrSelfPermission(
2817 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2818 != PackageManager.PERMISSION_GRANTED) {
2819 throw new SecurityException(
2820 "Requires permission "
2821 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2822 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 long ident = Binder.clearCallingIdentity();
2825 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002826 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 } finally {
2828 Binder.restoreCallingIdentity(ident);
2829 }
2830 }
2831 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002832
2833 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2834 // Make sure this is a valid input method.
2835 InputMethodInfo imm = mMethodMap.get(id);
2836 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002837 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838 }
2839
satokd87c2592010-09-29 11:52:06 +09002840 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2841 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842
satokd87c2592010-09-29 11:52:06 +09002843 if (enabled) {
2844 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2845 if (pair.first.equals(id)) {
2846 // We are enabling this input method, but it is already enabled.
2847 // Nothing to do. The previous state was enabled.
2848 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002849 }
2850 }
satokd87c2592010-09-29 11:52:06 +09002851 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2852 // Previous state was disabled.
2853 return false;
2854 } else {
2855 StringBuilder builder = new StringBuilder();
2856 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2857 builder, enabledInputMethodsList, id)) {
2858 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002859 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002860 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2861 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2862 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002863 }
2864 // Previous state was enabled.
2865 return true;
2866 } else {
2867 // We are disabling the input method but it is already disabled.
2868 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002869 return false;
2870 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002871 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002872 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002873
satok57ffc002011-01-25 00:11:47 +09002874 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2875 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002876 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002877 }
2878
satok723a27e2010-11-11 14:58:11 +09002879 private void saveCurrentInputMethodAndSubtypeToHistory() {
2880 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2881 if (mCurrentSubtype != null) {
2882 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2883 }
satok57ffc002011-01-25 00:11:47 +09002884 if (canAddToLastInputMethod(mCurrentSubtype)) {
2885 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2886 }
satokab751aa2010-09-14 19:17:36 +09002887 }
2888
satok723a27e2010-11-11 14:58:11 +09002889 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2890 boolean setSubtypeOnly) {
2891 // Update the history of InputMethod and Subtype
2892 saveCurrentInputMethodAndSubtypeToHistory();
2893
2894 // Set Subtype here
2895 if (imi == null || subtypeId < 0) {
2896 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002897 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002898 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002899 if (subtypeId < imi.getSubtypeCount()) {
2900 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2901 mSettings.putSelectedSubtype(subtype.hashCode());
2902 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002903 } else {
2904 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002905 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002906 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002907 }
satokab751aa2010-09-14 19:17:36 +09002908 }
satok723a27e2010-11-11 14:58:11 +09002909
satok4c0e7152012-06-20 20:08:44 +09002910 // Workaround.
2911 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2912 // IMEs are not recognized and considered uninstalled.
2913 // Actually, we can't move everything after SystemReady because
2914 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2915 // the default IME here and try cheking the default IME again in systemReady().
2916 // TODO: Do nothing before system ready and implement a separated logic for
2917 // the encryption lock screen.
2918 // TODO: ASEC should be ready before IMMS is instantiated.
2919 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002920 // Set InputMethod here
2921 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2922 }
2923 }
2924
2925 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2926 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2927 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2928 // newDefaultIme is empty when there is no candidate for the selected IME.
2929 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2930 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2931 if (subtypeHashCode != null) {
2932 try {
2933 lastSubtypeId = getSubtypeIdFromHashCode(
2934 imi, Integer.valueOf(subtypeHashCode));
2935 } catch (NumberFormatException e) {
2936 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2937 }
2938 }
2939 }
2940 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002941 }
2942
2943 private int getSelectedInputMethodSubtypeId(String id) {
2944 InputMethodInfo imi = mMethodMap.get(id);
2945 if (imi == null) {
2946 return NOT_A_SUBTYPE_ID;
2947 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002948 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2949 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002950 }
2951
satokfdf419e2012-05-08 16:52:08 +09002952 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2953 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2954 }
2955
2956 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002957 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002958 final int subtypeCount = imi.getSubtypeCount();
2959 for (int i = 0; i < subtypeCount; ++i) {
2960 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002961 if (subtypeHashCode == ims.hashCode()) {
2962 return i;
2963 }
satokab751aa2010-09-14 19:17:36 +09002964 }
2965 }
2966 return NOT_A_SUBTYPE_ID;
2967 }
2968
satoka86f5e42011-09-02 17:12:42 +09002969 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2970 Resources res, InputMethodInfo imi) {
2971 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002972 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002973 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002974 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002975 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002976 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002977 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002978 // scan overriding implicitly enabled subtypes.
2979 InputMethodSubtype subtype = subtypes.get(i);
2980 if (subtype.overridesImplicitlyEnabledSubtype()) {
2981 final String mode = subtype.getMode();
2982 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2983 applicableModeAndSubtypesMap.put(mode, subtype);
2984 }
2985 }
2986 }
2987 if (applicableModeAndSubtypesMap.size() > 0) {
2988 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2989 }
2990 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002991 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002992 final String locale = subtype.getLocale();
2993 final String mode = subtype.getMode();
2994 // When system locale starts with subtype's locale, that subtype will be applicable
2995 // for system locale
2996 // For instance, it's clearly applicable for cases like system locale = en_US and
2997 // subtype = en, but it is not necessarily considered applicable for cases like system
2998 // locale = en and subtype = en_US.
2999 // We just call systemLocale.startsWith(locale) in this function because there is no
3000 // need to find applicable subtypes aggressively unlike
3001 // findLastResortApplicableSubtypeLocked.
3002 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003003 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003004 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003005 if (applicableSubtype != null) {
3006 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3007 if (!systemLocale.equals(locale)) continue;
3008 }
satok3da92232011-01-11 22:46:30 +09003009 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003010 }
3011 }
3012 final InputMethodSubtype keyboardSubtype
3013 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3014 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3015 applicableModeAndSubtypesMap.values());
3016 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3017 for (int i = 0; i < N; ++i) {
3018 final InputMethodSubtype subtype = subtypes.get(i);
3019 final String mode = subtype.getMode();
3020 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3021 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3022 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003023 }
3024 }
3025 }
satokc3690562012-01-10 20:14:43 +09003026 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003027 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003028 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003029 if (lastResortKeyboardSubtype != null) {
3030 applicableSubtypes.add(lastResortKeyboardSubtype);
3031 }
3032 }
3033 return applicableSubtypes;
3034 }
3035
satok4e4569d2010-11-19 18:45:53 +09003036 /**
3037 * If there are no selected subtypes, tries finding the most applicable one according to the
3038 * given locale.
3039 * @param subtypes this function will search the most applicable subtype in subtypes
3040 * @param mode subtypes will be filtered by mode
3041 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003042 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3043 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003044 * @return the most applicable subtypeId
3045 */
satokdf31ae62011-01-15 06:19:44 +09003046 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3047 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003048 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003049 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003050 return null;
satok8fbb1e82010-11-02 23:15:58 +09003051 }
satok4e4569d2010-11-19 18:45:53 +09003052 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003053 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003054 }
satok8fbb1e82010-11-02 23:15:58 +09003055 final String language = locale.substring(0, 2);
3056 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003057 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003058 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003059 final int N = subtypes.size();
3060 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003061 InputMethodSubtype subtype = subtypes.get(i);
3062 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003063 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3064 // and all subtypes with all modes can be candidates.
3065 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003066 if (firstMatchedModeSubtype == null) {
3067 firstMatchedModeSubtype = subtype;
3068 }
satok9ef02832010-11-04 21:17:48 +09003069 if (locale.equals(subtypeLocale)) {
3070 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003071 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003072 break;
3073 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3074 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003075 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003076 partialMatchFound = true;
3077 }
satok8fbb1e82010-11-02 23:15:58 +09003078 }
3079 }
3080
satok7599a7f2010-12-22 13:45:23 +09003081 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3082 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003083 }
3084
satok8fbb1e82010-11-02 23:15:58 +09003085 // The first subtype applicable to the system locale will be defined as the most applicable
3086 // subtype.
3087 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003088 if (applicableSubtype != null) {
3089 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3090 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3091 }
satok8fbb1e82010-11-02 23:15:58 +09003092 }
satokcd7cd292010-11-20 15:46:23 +09003093 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003094 }
3095
satok4e4569d2010-11-19 18:45:53 +09003096 // If there are no selected shortcuts, tries finding the most applicable ones.
3097 private Pair<InputMethodInfo, InputMethodSubtype>
3098 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3099 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3100 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003101 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003102 boolean foundInSystemIME = false;
3103
3104 // Search applicable subtype for each InputMethodInfo
3105 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003106 final String imiId = imi.getId();
3107 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3108 continue;
3109 }
satokcd7cd292010-11-20 15:46:23 +09003110 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003111 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003112 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003113 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003114 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003115 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003116 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003117 }
satokdf31ae62011-01-15 06:19:44 +09003118 // 2. Search by the system locale from enabledSubtypes.
3119 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003120 if (subtype == null) {
3121 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003122 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003123 }
satoka86f5e42011-09-02 17:12:42 +09003124 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3125 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3126 final ArrayList<InputMethodSubtype> subtypesForSearch =
3127 overridingImplicitlyEnabledSubtypes.isEmpty()
3128 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003129 // 4. Search by the current subtype's locale from all subtypes.
3130 if (subtype == null && mCurrentSubtype != null) {
3131 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003132 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003133 }
3134 // 5. Search by the system locale from all subtypes.
3135 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003136 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003137 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003138 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003139 }
satokcd7cd292010-11-20 15:46:23 +09003140 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003141 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003142 // The current input method is the most applicable IME.
3143 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003144 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003145 break;
satok7599a7f2010-12-22 13:45:23 +09003146 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003147 // The system input method is 2nd applicable IME.
3148 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003149 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003150 if ((imi.getServiceInfo().applicationInfo.flags
3151 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3152 foundInSystemIME = true;
3153 }
satok4e4569d2010-11-19 18:45:53 +09003154 }
3155 }
3156 }
3157 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003158 if (mostApplicableIMI != null) {
3159 Slog.w(TAG, "Most applicable shortcut input method was:"
3160 + mostApplicableIMI.getId());
3161 if (mostApplicableSubtype != null) {
3162 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3163 + "," + mostApplicableSubtype.getMode() + ","
3164 + mostApplicableSubtype.getLocale());
3165 }
3166 }
satok4e4569d2010-11-19 18:45:53 +09003167 }
satokcd7cd292010-11-20 15:46:23 +09003168 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003169 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003170 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003171 } else {
3172 return null;
3173 }
3174 }
3175
satokab751aa2010-09-14 19:17:36 +09003176 /**
3177 * @return Return the current subtype of this input method.
3178 */
satok42c5a162011-05-26 16:46:14 +09003179 @Override
satokab751aa2010-09-14 19:17:36 +09003180 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003181 // TODO: Make this work even for non-current users?
3182 if (!calledFromValidUser()) {
3183 return null;
3184 }
3185 synchronized (mMethodMap) {
3186 return getCurrentInputMethodSubtypeLocked();
3187 }
3188 }
3189
3190 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003191 if (mCurMethodId == null) {
3192 return null;
3193 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003194 final boolean subtypeIsSelected =
3195 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3196 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3197 if (imi == null || imi.getSubtypeCount() == 0) {
3198 return null;
satok4e4569d2010-11-19 18:45:53 +09003199 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003200 if (!subtypeIsSelected || mCurrentSubtype == null
3201 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3202 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3203 if (subtypeId == NOT_A_SUBTYPE_ID) {
3204 // If there are no selected subtypes, the framework will try to find
3205 // the most applicable subtype from explicitly or implicitly enabled
3206 // subtypes.
3207 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3208 getEnabledInputMethodSubtypeListLocked(imi, true);
3209 // If there is only one explicitly or implicitly enabled subtype,
3210 // just returns it.
3211 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3212 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3213 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3214 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3215 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3216 SUBTYPE_MODE_KEYBOARD, null, true);
3217 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003218 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003219 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3220 true);
satok4e4569d2010-11-19 18:45:53 +09003221 }
satok3ef8b292010-11-23 06:06:29 +09003222 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003223 } else {
3224 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003225 }
3226 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003227 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003228 }
3229
satokf3db1af2010-11-23 13:34:33 +09003230 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3231 InputMethodSubtype subtype) {
3232 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3233 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3234 } else {
3235 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3236 subtypes.add(subtype);
3237 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3238 }
3239 }
3240
satok4e4569d2010-11-19 18:45:53 +09003241 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003242 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003243 @Override
satok4e4569d2010-11-19 18:45:53 +09003244 public List getShortcutInputMethodsAndSubtypes() {
3245 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003246 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003247 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003248 // If there are no selected shortcut subtypes, the framework will try to find
3249 // the most applicable subtype from all subtypes whose mode is
3250 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003251 Pair<InputMethodInfo, InputMethodSubtype> info =
3252 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3253 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003254 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003255 ret.add(info.first);
3256 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003257 }
satok3da92232011-01-11 22:46:30 +09003258 return ret;
satokf3db1af2010-11-23 13:34:33 +09003259 }
satokf3db1af2010-11-23 13:34:33 +09003260 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3261 ret.add(imi);
3262 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3263 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003264 }
3265 }
satokf3db1af2010-11-23 13:34:33 +09003266 return ret;
satok4e4569d2010-11-19 18:45:53 +09003267 }
3268 }
3269
satok42c5a162011-05-26 16:46:14 +09003270 @Override
satokb66d2872010-11-10 01:04:04 +09003271 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003272 // TODO: Make this work even for non-current users?
3273 if (!calledFromValidUser()) {
3274 return false;
3275 }
satokb66d2872010-11-10 01:04:04 +09003276 synchronized (mMethodMap) {
3277 if (subtype != null && mCurMethodId != null) {
3278 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3279 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3280 if (subtypeId != NOT_A_SUBTYPE_ID) {
3281 setInputMethodLocked(mCurMethodId, subtypeId);
3282 return true;
3283 }
3284 }
3285 return false;
3286 }
3287 }
3288
satok688bd472012-02-09 20:09:17 +09003289 private static class InputMethodAndSubtypeListManager {
3290 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003291 // Used to load label
satok688bd472012-02-09 20:09:17 +09003292 private final PackageManager mPm;
3293 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003294 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003295 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3296 mContext = context;
3297 mPm = context.getPackageManager();
3298 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003299 final Locale locale = context.getResources().getConfiguration().locale;
3300 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003301 }
3302
3303 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3304 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3305 new Comparator<InputMethodInfo>() {
3306 @Override
3307 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3308 if (imi2 == null) return 0;
3309 if (imi1 == null) return 1;
3310 if (mPm == null) {
3311 return imi1.getId().compareTo(imi2.getId());
3312 }
3313 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3314 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3315 return imiId1.toString().compareTo(imiId2.toString());
3316 }
3317 });
3318
3319 public ImeSubtypeListItem getNextInputMethod(
3320 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3321 if (imi == null) {
3322 return null;
3323 }
3324 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3325 if (imList.size() <= 1) {
3326 return null;
3327 }
3328 final int N = imList.size();
3329 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003330 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003331 : NOT_A_SUBTYPE_ID;
3332 for (int i = 0; i < N; ++i) {
3333 final ImeSubtypeListItem isli = imList.get(i);
3334 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3335 if (!onlyCurrentIme) {
3336 return imList.get((i + 1) % N);
3337 }
3338 for (int j = 0; j < N - 1; ++j) {
3339 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3340 if (candidate.mImi.equals(imi)) {
3341 return candidate;
3342 }
3343 }
3344 return null;
3345 }
3346 }
3347 return null;
3348 }
3349
3350 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3351 return getSortedInputMethodAndSubtypeList(true, false, false);
3352 }
3353
3354 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3355 boolean inputShown, boolean isScreenLocked) {
3356 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3357 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3358 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3359 if (immis == null || immis.size() == 0) {
3360 return Collections.emptyList();
3361 }
3362 mSortedImmis.clear();
3363 mSortedImmis.putAll(immis);
3364 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3365 if (imi == null) continue;
3366 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3367 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3368 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3369 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3370 }
3371 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3372 final CharSequence imeLabel = imi.loadLabel(mPm);
3373 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3374 final int subtypeCount = imi.getSubtypeCount();
3375 if (DEBUG) {
3376 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3377 }
3378 for (int j = 0; j < subtypeCount; ++j) {
3379 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3380 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3381 // We show all enabled IMEs and subtypes when an IME is shown.
3382 if (enabledSubtypeSet.contains(subtypeHashCode)
3383 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3384 final CharSequence subtypeLabel =
3385 subtype.overridesImplicitlyEnabledSubtype() ? null
3386 : subtype.getDisplayName(mContext, imi.getPackageName(),
3387 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003388 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3389 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003390
3391 // Removing this subtype from enabledSubtypeSet because we no longer
3392 // need to add an entry of this subtype to imList to avoid duplicated
3393 // entries.
3394 enabledSubtypeSet.remove(subtypeHashCode);
3395 }
3396 }
3397 } else {
satok93d744d2012-05-09 17:14:08 +09003398 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3399 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003400 }
3401 }
satok93d744d2012-05-09 17:14:08 +09003402 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003403 return imList;
3404 }
3405 }
3406
satokd87c2592010-09-29 11:52:06 +09003407 /**
3408 * Utility class for putting and getting settings for InputMethod
3409 * TODO: Move all putters and getters of settings to this class.
3410 */
3411 private static class InputMethodSettings {
3412 // The string for enabled input method is saved as follows:
3413 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3414 private static final char INPUT_METHOD_SEPARATER = ':';
3415 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003416 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003417 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3418
satok723a27e2010-11-11 14:58:11 +09003419 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003420 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3421
satokdf31ae62011-01-15 06:19:44 +09003422 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003423 private final ContentResolver mResolver;
3424 private final HashMap<String, InputMethodInfo> mMethodMap;
3425 private final ArrayList<InputMethodInfo> mMethodList;
3426
3427 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003428 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003429
3430 private static void buildEnabledInputMethodsSettingString(
3431 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3432 String id = pair.first;
3433 ArrayList<String> subtypes = pair.second;
3434 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003435 // Inputmethod and subtypes are saved in the settings as follows:
3436 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3437 for (String subtypeId: subtypes) {
3438 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003439 }
3440 }
3441
3442 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003443 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003444 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3445 int userId) {
3446 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003447 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003448 mResolver = resolver;
3449 mMethodMap = methodMap;
3450 mMethodList = methodList;
3451 }
3452
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003453 public void setCurrentUserId(int userId) {
3454 if (DEBUG) {
3455 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3456 + userId + ", new ime = " + getSelectedInputMethod());
3457 }
3458 // IMMS settings are kept per user, so keep track of current user
3459 mCurrentUserId = userId;
3460 }
3461
satokd87c2592010-09-29 11:52:06 +09003462 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3463 return createEnabledInputMethodListLocked(
3464 getEnabledInputMethodsAndSubtypeListLocked());
3465 }
3466
satok7f35c8c2010-10-07 21:13:11 +09003467 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003468 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3469 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003470 getEnabledInputMethodsAndSubtypeListLocked());
3471 }
3472
satok67ddf9c2010-11-17 09:45:54 +09003473 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3474 InputMethodInfo imi) {
3475 List<Pair<String, ArrayList<String>>> imsList =
3476 getEnabledInputMethodsAndSubtypeListLocked();
3477 ArrayList<InputMethodSubtype> enabledSubtypes =
3478 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003479 if (imi != null) {
3480 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3481 InputMethodInfo info = mMethodMap.get(imsPair.first);
3482 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003483 final int subtypeCount = info.getSubtypeCount();
3484 for (int i = 0; i < subtypeCount; ++i) {
3485 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003486 for (String s: imsPair.second) {
3487 if (String.valueOf(ims.hashCode()).equals(s)) {
3488 enabledSubtypes.add(ims);
3489 }
satok67ddf9c2010-11-17 09:45:54 +09003490 }
3491 }
satok884ef9a2010-11-18 10:39:46 +09003492 break;
satok67ddf9c2010-11-17 09:45:54 +09003493 }
satok67ddf9c2010-11-17 09:45:54 +09003494 }
3495 }
3496 return enabledSubtypes;
3497 }
3498
satokd87c2592010-09-29 11:52:06 +09003499 // At the initial boot, the settings for input methods are not set,
3500 // so we need to enable IME in that case.
3501 public void enableAllIMEsIfThereIsNoEnabledIME() {
3502 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3503 StringBuilder sb = new StringBuilder();
3504 final int N = mMethodList.size();
3505 for (int i = 0; i < N; i++) {
3506 InputMethodInfo imi = mMethodList.get(i);
3507 Slog.i(TAG, "Adding: " + imi.getId());
3508 if (i > 0) sb.append(':');
3509 sb.append(imi.getId());
3510 }
3511 putEnabledInputMethodsStr(sb.toString());
3512 }
3513 }
3514
satokbb4aa062011-01-19 21:40:27 +09003515 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003516 ArrayList<Pair<String, ArrayList<String>>> imsList
3517 = new ArrayList<Pair<String, ArrayList<String>>>();
3518 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3519 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3520 return imsList;
3521 }
satok723a27e2010-11-11 14:58:11 +09003522 mInputMethodSplitter.setString(enabledInputMethodsStr);
3523 while (mInputMethodSplitter.hasNext()) {
3524 String nextImsStr = mInputMethodSplitter.next();
3525 mSubtypeSplitter.setString(nextImsStr);
3526 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003527 ArrayList<String> subtypeHashes = new ArrayList<String>();
3528 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003529 String imeId = mSubtypeSplitter.next();
3530 while (mSubtypeSplitter.hasNext()) {
3531 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003532 }
3533 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3534 }
3535 }
3536 return imsList;
3537 }
3538
3539 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3540 if (reloadInputMethodStr) {
3541 getEnabledInputMethodsStr();
3542 }
3543 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3544 // Add in the newly enabled input method.
3545 putEnabledInputMethodsStr(id);
3546 } else {
3547 putEnabledInputMethodsStr(
3548 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3549 }
3550 }
3551
3552 /**
3553 * Build and put a string of EnabledInputMethods with removing specified Id.
3554 * @return the specified id was removed or not.
3555 */
3556 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3557 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3558 boolean isRemoved = false;
3559 boolean needsAppendSeparator = false;
3560 for (Pair<String, ArrayList<String>> ims: imsList) {
3561 String curId = ims.first;
3562 if (curId.equals(id)) {
3563 // We are disabling this input method, and it is
3564 // currently enabled. Skip it to remove from the
3565 // new list.
3566 isRemoved = true;
3567 } else {
3568 if (needsAppendSeparator) {
3569 builder.append(INPUT_METHOD_SEPARATER);
3570 } else {
3571 needsAppendSeparator = true;
3572 }
3573 buildEnabledInputMethodsSettingString(builder, ims);
3574 }
3575 }
3576 if (isRemoved) {
3577 // Update the setting with the new list of input methods.
3578 putEnabledInputMethodsStr(builder.toString());
3579 }
3580 return isRemoved;
3581 }
3582
3583 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3584 List<Pair<String, ArrayList<String>>> imsList) {
3585 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3586 for (Pair<String, ArrayList<String>> ims: imsList) {
3587 InputMethodInfo info = mMethodMap.get(ims.first);
3588 if (info != null) {
3589 res.add(info);
3590 }
3591 }
3592 return res;
3593 }
3594
satok7f35c8c2010-10-07 21:13:11 +09003595 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003596 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003597 List<Pair<String, ArrayList<String>>> imsList) {
3598 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3599 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3600 for (Pair<String, ArrayList<String>> ims : imsList) {
3601 InputMethodInfo info = mMethodMap.get(ims.first);
3602 if (info != null) {
3603 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3604 }
3605 }
3606 return res;
3607 }
3608
satokd87c2592010-09-29 11:52:06 +09003609 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003610 Settings.Secure.putStringForUser(
3611 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003612 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003613 if (DEBUG) {
3614 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3615 }
satokd87c2592010-09-29 11:52:06 +09003616 }
3617
3618 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003619 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3620 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003621 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003622 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3623 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003624 }
satokd87c2592010-09-29 11:52:06 +09003625 return mEnabledInputMethodsStrCache;
3626 }
satok723a27e2010-11-11 14:58:11 +09003627
3628 private void saveSubtypeHistory(
3629 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3630 StringBuilder builder = new StringBuilder();
3631 boolean isImeAdded = false;
3632 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3633 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3634 newSubtypeId);
3635 isImeAdded = true;
3636 }
3637 for (Pair<String, String> ime: savedImes) {
3638 String imeId = ime.first;
3639 String subtypeId = ime.second;
3640 if (TextUtils.isEmpty(subtypeId)) {
3641 subtypeId = NOT_A_SUBTYPE_ID_STR;
3642 }
3643 if (isImeAdded) {
3644 builder.append(INPUT_METHOD_SEPARATER);
3645 } else {
3646 isImeAdded = true;
3647 }
3648 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3649 subtypeId);
3650 }
3651 // Remove the last INPUT_METHOD_SEPARATER
3652 putSubtypeHistoryStr(builder.toString());
3653 }
3654
3655 public void addSubtypeToHistory(String imeId, String subtypeId) {
3656 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3657 for (Pair<String, String> ime: subtypeHistory) {
3658 if (ime.first.equals(imeId)) {
3659 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003660 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003661 + ime.second);
3662 }
3663 // We should break here
3664 subtypeHistory.remove(ime);
3665 break;
3666 }
3667 }
satokbb4aa062011-01-19 21:40:27 +09003668 if (DEBUG) {
3669 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3670 }
satok723a27e2010-11-11 14:58:11 +09003671 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3672 }
3673
3674 private void putSubtypeHistoryStr(String str) {
3675 if (DEBUG) {
3676 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3677 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003678 Settings.Secure.putStringForUser(
3679 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003680 }
3681
3682 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3683 // Gets the first one from the history
3684 return getLastSubtypeForInputMethodLockedInternal(null);
3685 }
3686
3687 public String getLastSubtypeForInputMethodLocked(String imeId) {
3688 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3689 if (ime != null) {
3690 return ime.second;
3691 } else {
3692 return null;
3693 }
3694 }
3695
3696 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3697 List<Pair<String, ArrayList<String>>> enabledImes =
3698 getEnabledInputMethodsAndSubtypeListLocked();
3699 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003700 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003701 final String imeInTheHistory = imeAndSubtype.first;
3702 // If imeId is empty, returns the first IME and subtype in the history
3703 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3704 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003705 final String subtypeHashCode =
3706 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3707 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003708 if (!TextUtils.isEmpty(subtypeHashCode)) {
3709 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003710 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003711 }
3712 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3713 }
3714 }
3715 }
3716 if (DEBUG) {
3717 Slog.d(TAG, "No enabled IME found in the history");
3718 }
3719 return null;
3720 }
3721
satokdf31ae62011-01-15 06:19:44 +09003722 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003723 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3724 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3725 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003726 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003727 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003728 if (explicitlyEnabledSubtypes.size() == 0) {
3729 // If there are no explicitly enabled subtypes, applicable subtypes are
3730 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003731 // If IME is enabled and no subtypes are enabled, applicable subtypes
3732 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003733 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003734 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003735 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003736 if (implicitlySelectedSubtypes != null) {
3737 final int N = implicitlySelectedSubtypes.size();
3738 for (int i = 0; i < N; ++i) {
3739 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3740 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3741 return subtypeHashCode;
3742 }
3743 }
3744 }
3745 }
3746 } else {
satokf6cafb62011-01-17 16:29:02 +09003747 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003748 if (s.equals(subtypeHashCode)) {
3749 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003750 try {
3751 final int hashCode = Integer.valueOf(subtypeHashCode);
3752 // Check whether the subtype id is valid or not
3753 if (isValidSubtypeId(imi, hashCode)) {
3754 return s;
3755 } else {
3756 return NOT_A_SUBTYPE_ID_STR;
3757 }
3758 } catch (NumberFormatException e) {
3759 return NOT_A_SUBTYPE_ID_STR;
3760 }
satokdf31ae62011-01-15 06:19:44 +09003761 }
satok723a27e2010-11-11 14:58:11 +09003762 }
3763 }
3764 // If imeId was enabled but subtypeId was disabled.
3765 return NOT_A_SUBTYPE_ID_STR;
3766 }
3767 }
3768 // If both imeId and subtypeId are disabled, return null
3769 return null;
3770 }
3771
3772 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3773 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3774 final String subtypeHistoryStr = getSubtypeHistoryStr();
3775 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3776 return imsList;
3777 }
3778 mInputMethodSplitter.setString(subtypeHistoryStr);
3779 while (mInputMethodSplitter.hasNext()) {
3780 String nextImsStr = mInputMethodSplitter.next();
3781 mSubtypeSplitter.setString(nextImsStr);
3782 if (mSubtypeSplitter.hasNext()) {
3783 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3784 // The first element is ime id.
3785 String imeId = mSubtypeSplitter.next();
3786 while (mSubtypeSplitter.hasNext()) {
3787 subtypeId = mSubtypeSplitter.next();
3788 break;
3789 }
3790 imsList.add(new Pair<String, String>(imeId, subtypeId));
3791 }
3792 }
3793 return imsList;
3794 }
3795
3796 private String getSubtypeHistoryStr() {
3797 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003798 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3799 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003800 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003801 return Settings.Secure.getStringForUser(
3802 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003803 }
3804
3805 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003806 if (DEBUG) {
3807 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3808 + mCurrentUserId);
3809 }
3810 Settings.Secure.putStringForUser(
3811 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003812 }
3813
3814 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003815 if (DEBUG) {
3816 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3817 + mCurrentUserId);
3818 }
3819 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3820 subtypeId, mCurrentUserId);
3821 }
3822
3823 public String getDisabledSystemInputMethods() {
3824 return Settings.Secure.getStringForUser(
3825 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3826 }
3827
3828 public String getSelectedInputMethod() {
3829 if (DEBUG) {
3830 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3831 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3832 + ", " + mCurrentUserId);
3833 }
3834 return Settings.Secure.getStringForUser(
3835 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3836 }
3837
3838 public int getSelectedInputMethodSubtypeHashCode() {
3839 try {
3840 return Settings.Secure.getIntForUser(
3841 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3842 } catch (SettingNotFoundException e) {
3843 return NOT_A_SUBTYPE_ID;
3844 }
3845 }
3846
3847 public int getCurrentUserId() {
3848 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003849 }
satokd87c2592010-09-29 11:52:06 +09003850 }
3851
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003852 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003853 private static class InputMethodFileManager {
3854 private static final String SYSTEM_PATH = "system";
3855 private static final String INPUT_METHOD_PATH = "inputmethod";
3856 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3857 private static final String NODE_SUBTYPES = "subtypes";
3858 private static final String NODE_SUBTYPE = "subtype";
3859 private static final String NODE_IMI = "imi";
3860 private static final String ATTR_ID = "id";
3861 private static final String ATTR_LABEL = "label";
3862 private static final String ATTR_ICON = "icon";
3863 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3864 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3865 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3866 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3867 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3868 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003869 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003870 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003871 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003872 if (methodMap == null) {
3873 throw new NullPointerException("methodMap is null");
3874 }
3875 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003876 final File systemDir = userId == UserHandle.USER_OWNER
3877 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3878 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003879 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3880 if (!inputMethodDir.mkdirs()) {
3881 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3882 }
3883 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3884 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3885 if (!subtypeFile.exists()) {
3886 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003887 writeAdditionalInputMethodSubtypes(
3888 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003889 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003890 readAdditionalInputMethodSubtypes(
3891 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003892 }
3893 }
3894
3895 private void deleteAllInputMethodSubtypes(String imiId) {
3896 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003897 mAdditionalSubtypesMap.remove(imiId);
3898 writeAdditionalInputMethodSubtypes(
3899 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003900 }
3901 }
3902
3903 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003904 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003905 synchronized (mMethodMap) {
3906 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3907 final int N = additionalSubtypes.length;
3908 for (int i = 0; i < N; ++i) {
3909 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003910 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003911 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003912 } else {
3913 Slog.w(TAG, "Duplicated subtype definition found: "
3914 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003915 }
3916 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003917 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3918 writeAdditionalInputMethodSubtypes(
3919 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003920 }
3921 }
3922
3923 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3924 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003925 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003926 }
3927 }
3928
3929 private static void writeAdditionalInputMethodSubtypes(
3930 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3931 HashMap<String, InputMethodInfo> methodMap) {
3932 // Safety net for the case that this function is called before methodMap is set.
3933 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3934 FileOutputStream fos = null;
3935 try {
3936 fos = subtypesFile.startWrite();
3937 final XmlSerializer out = new FastXmlSerializer();
3938 out.setOutput(fos, "utf-8");
3939 out.startDocument(null, true);
3940 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3941 out.startTag(null, NODE_SUBTYPES);
3942 for (String imiId : allSubtypes.keySet()) {
3943 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3944 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3945 continue;
3946 }
3947 out.startTag(null, NODE_IMI);
3948 out.attribute(null, ATTR_ID, imiId);
3949 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3950 final int N = subtypesList.size();
3951 for (int i = 0; i < N; ++i) {
3952 final InputMethodSubtype subtype = subtypesList.get(i);
3953 out.startTag(null, NODE_SUBTYPE);
3954 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3955 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3956 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3957 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3958 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3959 out.attribute(null, ATTR_IS_AUXILIARY,
3960 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3961 out.endTag(null, NODE_SUBTYPE);
3962 }
3963 out.endTag(null, NODE_IMI);
3964 }
3965 out.endTag(null, NODE_SUBTYPES);
3966 out.endDocument();
3967 subtypesFile.finishWrite(fos);
3968 } catch (java.io.IOException e) {
3969 Slog.w(TAG, "Error writing subtypes", e);
3970 if (fos != null) {
3971 subtypesFile.failWrite(fos);
3972 }
3973 }
3974 }
3975
3976 private static void readAdditionalInputMethodSubtypes(
3977 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3978 if (allSubtypes == null || subtypesFile == null) return;
3979 allSubtypes.clear();
3980 FileInputStream fis = null;
3981 try {
3982 fis = subtypesFile.openRead();
3983 final XmlPullParser parser = Xml.newPullParser();
3984 parser.setInput(fis, null);
3985 int type = parser.getEventType();
3986 // Skip parsing until START_TAG
3987 while ((type = parser.next()) != XmlPullParser.START_TAG
3988 && type != XmlPullParser.END_DOCUMENT) {}
3989 String firstNodeName = parser.getName();
3990 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3991 throw new XmlPullParserException("Xml doesn't start with subtypes");
3992 }
3993 final int depth =parser.getDepth();
3994 String currentImiId = null;
3995 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3996 while (((type = parser.next()) != XmlPullParser.END_TAG
3997 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3998 if (type != XmlPullParser.START_TAG)
3999 continue;
4000 final String nodeName = parser.getName();
4001 if (NODE_IMI.equals(nodeName)) {
4002 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4003 if (TextUtils.isEmpty(currentImiId)) {
4004 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4005 continue;
4006 }
4007 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4008 allSubtypes.put(currentImiId, tempSubtypesArray);
4009 } else if (NODE_SUBTYPE.equals(nodeName)) {
4010 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4011 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4012 continue;
4013 }
4014 final int icon = Integer.valueOf(
4015 parser.getAttributeValue(null, ATTR_ICON));
4016 final int label = Integer.valueOf(
4017 parser.getAttributeValue(null, ATTR_LABEL));
4018 final String imeSubtypeLocale =
4019 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4020 final String imeSubtypeMode =
4021 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4022 final String imeSubtypeExtraValue =
4023 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004024 final boolean isAuxiliary = "1".equals(String.valueOf(
4025 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004026 final InputMethodSubtype subtype =
4027 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4028 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4029 tempSubtypesArray.add(subtype);
4030 }
4031 }
4032 } catch (XmlPullParserException e) {
4033 Slog.w(TAG, "Error reading subtypes: " + e);
4034 return;
4035 } catch (java.io.IOException e) {
4036 Slog.w(TAG, "Error reading subtypes: " + e);
4037 return;
4038 } catch (NumberFormatException e) {
4039 Slog.w(TAG, "Error reading subtypes: " + e);
4040 return;
4041 } finally {
4042 if (fis != null) {
4043 try {
4044 fis.close();
4045 } catch (java.io.IOException e1) {
4046 Slog.w(TAG, "Failed to close.");
4047 }
4048 }
4049 }
4050 }
4051 }
4052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004054 // Utilities for debug
4055 private static String getStackTrace() {
4056 final StringBuilder sb = new StringBuilder();
4057 try {
4058 throw new RuntimeException();
4059 } catch (RuntimeException e) {
4060 final StackTraceElement[] frames = e.getStackTrace();
4061 // Start at 1 because the first frame is here and we don't care about it
4062 for (int j = 1; j < frames.length; ++j) {
4063 sb.append(frames[j].toString() + "\n");
4064 }
4065 }
4066 return sb.toString();
4067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 @Override
4070 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4071 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4072 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4075 + Binder.getCallingPid()
4076 + ", uid=" + Binder.getCallingUid());
4077 return;
4078 }
4079
4080 IInputMethod method;
4081 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 synchronized (mMethodMap) {
4086 p.println("Current Input Method Manager state:");
4087 int N = mMethodList.size();
4088 p.println(" Input Methods:");
4089 for (int i=0; i<N; i++) {
4090 InputMethodInfo info = mMethodList.get(i);
4091 p.println(" InputMethod #" + i + ":");
4092 info.dump(p, " ");
4093 }
4094 p.println(" Clients:");
4095 for (ClientState ci : mClients.values()) {
4096 p.println(" Client " + ci + ":");
4097 p.println(" client=" + ci.client);
4098 p.println(" inputContext=" + ci.inputContext);
4099 p.println(" sessionRequested=" + ci.sessionRequested);
4100 p.println(" curSession=" + ci.curSession);
4101 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004102 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004104 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4105 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4107 + " mBoundToMethod=" + mBoundToMethod);
4108 p.println(" mCurToken=" + mCurToken);
4109 p.println(" mCurIntent=" + mCurIntent);
4110 method = mCurMethod;
4111 p.println(" mCurMethod=" + mCurMethod);
4112 p.println(" mEnabledSession=" + mEnabledSession);
4113 p.println(" mShowRequested=" + mShowRequested
4114 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4115 + " mShowForced=" + mShowForced
4116 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004117 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004119
Jeff Brownb88102f2010-09-08 11:49:43 -07004120 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 pw.flush();
4123 try {
4124 client.client.asBinder().dump(fd, args);
4125 } catch (RemoteException e) {
4126 p.println("Input method client dead: " + e);
4127 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004128 } else {
4129 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004131
Jeff Brownb88102f2010-09-08 11:49:43 -07004132 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 pw.flush();
4135 try {
4136 method.asBinder().dump(fd, args);
4137 } catch (RemoteException e) {
4138 p.println("Input method service dead: " + e);
4139 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004140 } else {
4141 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 }
4143 }
4144}