blob: c33fc71ed4db58b467212979706ab2e4ce7cedb5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070020import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090030import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090037import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090040import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090041import android.app.Notification;
42import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070043import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090044import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.ComponentName;
46import android.content.ContentResolver;
47import android.content.Context;
48import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.DialogInterface.OnCancelListener;
50import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090051import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070053import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090054import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.ResolveInfo;
58import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070059import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.res.Resources;
61import android.content.res.TypedArray;
62import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080063import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090065import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
68import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090069import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Parcel;
73import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080074import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.ServiceManager;
76import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090080import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090082import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070083import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090085import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090086import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.PrintWriterPrinter;
88import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090089import android.util.Slog;
90import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090092import android.view.LayoutInflater;
93import android.view.View;
94import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090096import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.inputmethod.InputBinding;
98import android.view.inputmethod.InputMethod;
99import android.view.inputmethod.InputMethodInfo;
100import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900101import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900102import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900103import android.widget.CompoundButton;
104import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900106import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
satoke7c6998e2011-06-03 17:57:59 +0900109import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900111import java.io.FileInputStream;
112import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.io.IOException;
114import java.io.PrintWriter;
115import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900116import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900117import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900119import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900121import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900122import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124/**
125 * This class provides a system service that manages input methods.
126 */
127public class InputMethodManagerService extends IInputMethodManager.Stub
128 implements ServiceConnection, Handler.Callback {
129 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700130 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900133 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900134 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900135 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_UNBIND_INPUT = 1000;
138 static final int MSG_BIND_INPUT = 1010;
139 static final int MSG_SHOW_SOFT_INPUT = 1020;
140 static final int MSG_HIDE_SOFT_INPUT = 1030;
141 static final int MSG_ATTACH_TOKEN = 1040;
142 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final int MSG_START_INPUT = 2000;
145 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final int MSG_UNBIND_METHOD = 3000;
148 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700149 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
satok01038492012-04-09 21:08:27 +0900151 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
satokf9f01002011-05-19 21:31:50 +0900155 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
156
satokab751aa2010-09-14 19:17:36 +0900157 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900158 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900159 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
160 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900161 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900162 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
163 "EnabledWhenDefaultIsNotAsciiCapable";
164 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900165 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800168 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900170 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final IWindowManager mIWindowManager;
173 final HandlerCaller mCaller;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900174 private InputMethodFileManager mFileManager;
175 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900176 private final HardKeyboardListener mHardKeyboardListener;
177 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // All known input methods. mMethodMap also serves as the global
182 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900183 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
184 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900185 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
186 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700188 // Used to bring IME service up to visible adjustment while it is being shown.
189 final ServiceConnection mVisibleConnection = new ServiceConnection() {
190 @Override public void onServiceConnected(ComponentName name, IBinder service) {
191 }
192
193 @Override public void onServiceDisconnected(ComponentName name) {
194 }
195 };
196 boolean mVisibleBound = false;
197
satok7cfc0ed2011-06-20 21:29:36 +0900198 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700199 private NotificationManager mNotificationManager;
200 private KeyguardManager mKeyguardManager;
201 private StatusBarManagerService mStatusBar;
202 private Notification mImeSwitcherNotification;
203 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900204 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900205 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900206 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 class SessionState {
209 final ClientState client;
210 final IInputMethod method;
211 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 @Override
214 public String toString() {
215 return "SessionState{uid " + client.uid + " pid " + client.pid
216 + " method " + Integer.toHexString(
217 System.identityHashCode(method))
218 + " session " + Integer.toHexString(
219 System.identityHashCode(session))
220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
224 IInputMethodSession _session) {
225 client = _client;
226 method = _method;
227 session = _session;
228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 class ClientState {
232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * Set to true if our ServiceConnection is currently actively bound to
315 * a service (whether or not we have gotten its IBinder back yet).
316 */
317 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * Set if the client has asked for the input method to be shown.
321 */
322 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set if we were explicitly told to show the input method.
326 */
327 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Set if we were forced to be shown.
331 */
332 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Set if we last told the input method to show itself.
336 */
337 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The Intent used to connect to the current input method.
341 */
342 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The token we have made for the currently active input method, to
346 * identify it in the future.
347 */
348 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * If non-null, this is the input method service we are currently connected
352 * to.
353 */
354 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Time that we last initiated a bind to the input method, to determine
358 * if we should try to disconnect and reconnect to it.
359 */
360 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * Have we called mCurMethod.bindInput()?
364 */
365 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Currently enabled session. Only touched by service thread, not
369 * protected by a lock.
370 */
371 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * True if the screen is on. The value is true initially.
375 */
376 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
Joe Onorato857fd9b2011-01-27 15:08:35 -0800378 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
379 int mImeWindowVis;
380
Ken Wakasa05dbb652011-08-22 15:22:43 +0900381 private AlertDialog.Builder mDialogBuilder;
382 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900383 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private InputMethodInfo[] mIms;
385 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900386 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900387 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
388 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500389 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 class SettingsObserver extends ContentObserver {
392 SettingsObserver(Handler handler) {
393 super(handler);
394 ContentResolver resolver = mContext.getContentResolver();
395 resolver.registerContentObserver(Settings.Secure.getUriFor(
396 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900397 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900398 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900400 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 @Override public void onChange(boolean selfChange) {
404 synchronized (mMethodMap) {
405 updateFromSettingsLocked();
406 }
407 }
408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
411 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700413 if (mCurClient != null && mCurClient.client != null) {
414 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
415 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900418
419 @Override
420 public void onReceive(Context context, Intent intent) {
421 final String action = intent.getAction();
422 if (Intent.ACTION_SCREEN_ON.equals(action)) {
423 mScreenOn = true;
424 refreshImeWindowVisibilityLocked();
425 updateActive();
426 return;
427 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
428 mScreenOn = false;
429 setImeWindowVisibilityStatusHiddenLocked();
430 updateActive();
431 return;
432 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
433 hideInputMethodMenu();
434 // No need to updateActive
435 return;
436 } else {
437 Slog.w(TAG, "Unexpected intent " + intent);
438 }
439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443 private boolean isChangingPackagesOfCurrentUser() {
444 final int userId = getChangingUserId();
445 final boolean retval = userId == mSettings.getCurrentUserId();
446 if (DEBUG) {
447 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
448 }
449 return retval;
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900454 if (!isChangingPackagesOfCurrentUser()) {
455 return false;
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900458 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 final int N = mMethodList.size();
460 if (curInputMethodId != null) {
461 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800462 InputMethodInfo imi = mMethodList.get(i);
463 if (imi.getId().equals(curInputMethodId)) {
464 for (String pkg : packages) {
465 if (imi.getPackageName().equals(pkg)) {
466 if (!doit) {
467 return true;
468 }
satok723a27e2010-11-11 14:58:11 +0900469 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800470 chooseNewDefaultIMELocked();
471 return true;
472 }
473 }
474 }
475 }
476 }
477 }
478 return false;
479 }
480
481 @Override
482 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900483 if (!isChangingPackagesOfCurrentUser()) {
484 return;
485 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800486 synchronized (mMethodMap) {
487 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900488 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 final int N = mMethodList.size();
490 if (curInputMethodId != null) {
491 for (int i=0; i<N; i++) {
492 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900493 final String imiId = imi.getId();
494 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800495 curIm = imi;
496 }
satoke7c6998e2011-06-03 17:57:59 +0900497
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900499 if (isPackageModified(imi.getPackageName())) {
500 mFileManager.deleteAllInputMethodSubtypes(imiId);
501 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800502 if (change == PACKAGE_TEMPORARY_CHANGE
503 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800504 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 + imi.getComponent());
506 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 }
509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800510
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 buildInputMethodListLocked(mMethodList, mMethodMap);
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800514
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800515 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800516 int change = isPackageDisappearing(curIm.getPackageName());
517 if (change == PACKAGE_TEMPORARY_CHANGE
518 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 ServiceInfo si = null;
520 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900521 si = mIPackageManager.getServiceInfo(
522 curIm.getComponent(), 0, mSettings.getCurrentUserId());
523 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 }
525 if (si == null) {
526 // Uh oh, current input method is no longer around!
527 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900529 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800530 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800531 changed = true;
532 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800533 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900534 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 }
536 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800538 }
satokab751aa2010-09-14 19:17:36 +0900539
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 if (curIm == null) {
541 // We currently don't have a default input method... is
542 // one now available?
543 changed = chooseNewDefaultIMELocked();
544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800545
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800546 if (changed) {
547 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 }
550 }
551 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800552
Jean Chalarde0d32a62011-10-20 20:36:07 +0900553 private static class MethodCallback extends IInputMethodCallback.Stub {
554 private final IInputMethod mMethod;
555 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800556
Jean Chalarde0d32a62011-10-20 20:36:07 +0900557 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900559 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800561
satoke7c6998e2011-06-03 17:57:59 +0900562 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 public void finishedEvent(int seq, boolean handled) throws RemoteException {
564 }
565
satoke7c6998e2011-06-03 17:57:59 +0900566 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900568 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800571
satok01038492012-04-09 21:08:27 +0900572 private class HardKeyboardListener
573 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
574 @Override
575 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
576 mHandler.sendMessage(mHandler.obtainMessage(
577 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
578 }
579
580 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
581 if (DEBUG) {
582 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
583 + enabled);
584 }
585 synchronized(mMethodMap) {
586 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
587 && mSwitchingDialog.isShowing()) {
588 mSwitchingDialogTitleView.findViewById(
589 com.android.internal.R.id.hard_keyboard_section).setVisibility(
590 available ? View.VISIBLE : View.GONE);
591 }
592 }
593 }
594 }
595
596 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900597 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800599 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mHandler = new Handler(this);
601 mIWindowManager = IWindowManager.Stub.asInterface(
602 ServiceManager.getService(Context.WINDOW_SERVICE));
603 mCaller = new HandlerCaller(context, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900604 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 public void executeMessage(Message msg) {
606 handleMessage(msg);
607 }
608 });
satok01038492012-04-09 21:08:27 +0900609 mWindowManagerService = windowManager;
610 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900611
satok7cfc0ed2011-06-20 21:29:36 +0900612 mImeSwitcherNotification = new Notification();
613 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
614 mImeSwitcherNotification.when = 0;
615 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
616 mImeSwitcherNotification.tickerText = null;
617 mImeSwitcherNotification.defaults = 0; // please be quiet
618 mImeSwitcherNotification.sound = null;
619 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400620
621 // Tag this notification specially so SystemUI knows it's important
622 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
623
satok7cfc0ed2011-06-20 21:29:36 +0900624 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900625 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900626
627 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900628
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900629 final IntentFilter broadcastFilter = new IntentFilter();
630 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
631 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
632 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
633 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800634
satok7cfc0ed2011-06-20 21:29:36 +0900635 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900636 int userId = 0;
637 try {
638 ActivityManagerNative.getDefault().registerUserSwitchObserver(
639 new IUserSwitchObserver.Stub() {
640 @Override
641 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900642 synchronized(mMethodMap) {
643 switchUserLocked(newUserId);
644 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900645 if (reply != null) {
646 try {
647 reply.sendResult(null);
648 } catch (RemoteException e) {
649 }
650 }
651 }
652
653 @Override
654 public void onUserSwitchComplete(int newUserId) throws RemoteException {
655 }
656 });
657 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
658 } catch (RemoteException e) {
659 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
660 }
661 mMyPackageMonitor.register(mContext, null, true);
satok913a8922010-08-26 21:53:41 +0900662
satokd87c2592010-09-29 11:52:06 +0900663 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900664 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900665 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900666 mFileManager = new InputMethodFileManager(mMethodMap, userId);
667 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900668
669 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900670 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900671 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900674 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675
satok0a1bcf42012-05-16 19:26:31 +0900676 if (!mImeSelectedOnBoot) {
677 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900678 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 mSettingsObserver = new SettingsObserver(mHandler);
682 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900683
684 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
685 // according to the new system locale.
686 final IntentFilter filter = new IntentFilter();
687 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
688 mContext.registerReceiver(
689 new BroadcastReceiver() {
690 @Override
691 public void onReceive(Context context, Intent intent) {
692 synchronized(mMethodMap) {
693 checkCurrentLocaleChangedLocked();
694 }
695 }
696 }, filter);
697 }
698
satok5b927c432012-05-01 20:09:34 +0900699 private void resetDefaultImeLocked(Context context) {
700 // Do not reset the default (current) IME when it is a 3rd-party IME
701 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
702 return;
703 }
704
705 InputMethodInfo defIm = null;
706 for (InputMethodInfo imi : mMethodList) {
707 if (defIm == null) {
708 if (isValidSystemDefaultIme(imi, context)) {
709 defIm = imi;
710 Slog.i(TAG, "Selected default: " + imi.getId());
711 }
712 }
713 }
714 if (defIm == null && mMethodList.size() > 0) {
715 defIm = getMostApplicableDefaultIMELocked();
716 Slog.i(TAG, "No default found, using " + defIm.getId());
717 }
718 if (defIm != null) {
719 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
720 }
721 }
722
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900723 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
724 if (!mSystemReady) {
725 // not system ready
726 return;
727 }
728 final Locale newLocale = mRes.getConfiguration().locale;
729 if (!updateOnlyWhenLocaleChanged
730 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
731 if (!updateOnlyWhenLocaleChanged) {
732 hideCurrentInputLocked(0, null);
733 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700734 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900735 }
736 if (DEBUG) {
737 Slog.i(TAG, "Locale has been changed to " + newLocale);
738 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900739 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
740 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900741 buildInputMethodListLocked(mMethodList, mMethodMap);
742 if (!updateOnlyWhenLocaleChanged) {
743 final String selectedImiId = mSettings.getSelectedInputMethod();
744 if (TextUtils.isEmpty(selectedImiId)) {
745 // This is the first time of the user switch and
746 // set the current ime to the proper one.
747 resetDefaultImeLocked(mContext);
748 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900749 } else {
750 // If the locale is changed, needs to reset the default ime
751 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900752 }
753 updateFromSettingsLocked();
754 mLastSystemLocale = newLocale;
755 if (!updateOnlyWhenLocaleChanged) {
756 try {
757 startInputInnerLocked();
758 } catch (RuntimeException e) {
759 Slog.w(TAG, "Unexpected exception", e);
760 }
761 }
762 }
763 }
764
765 private void checkCurrentLocaleChangedLocked() {
766 resetAllInternalStateLocked(true);
767 }
768
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900769 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900770 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900771 // InputMethodFileManager should be reset when the user is changed
772 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 resetAllInternalStateLocked(false);
774 }
775
satok0a1bcf42012-05-16 19:26:31 +0900776 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
777 if (!mSystemReady) {
778 return false;
779 }
satok5b927c432012-05-01 20:09:34 +0900780 if (!isSystemIme(imi)) {
781 return false;
782 }
783 if (imi.getIsDefaultResourceId() != 0) {
784 try {
785 Resources res = context.createPackageContext(
786 imi.getPackageName(), 0).getResources();
787 if (res.getBoolean(imi.getIsDefaultResourceId())
788 && containsSubtypeOf(imi, context.getResources().getConfiguration().
789 locale.getLanguage())) {
790 return true;
791 }
792 } catch (PackageManager.NameNotFoundException ex) {
793 } catch (Resources.NotFoundException ex) {
794 }
795 }
796 if (imi.getSubtypeCount() == 0) {
797 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
798 }
799 return false;
800 }
801
802 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
803 if (!isSystemIme(imi)) {
804 return false;
805 }
806 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
807 }
808
809 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
810 final int N = imi.getSubtypeCount();
811 for (int i = 0; i < N; ++i) {
812 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
813 return true;
814 }
815 }
816 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
819 @Override
820 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
821 throws RemoteException {
822 try {
823 return super.onTransact(code, data, reply, flags);
824 } catch (RuntimeException e) {
825 // The input method manager only throws security exceptions, so let's
826 // log all others.
827 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830 throw e;
831 }
832 }
833
Dianne Hackborn661cd522011-08-22 00:26:20 -0700834 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700835 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900836 if (DEBUG) {
837 Slog.d(TAG, "--- systemReady");
838 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700839 if (!mSystemReady) {
840 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900841 mKeyguardManager =
842 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700843 mNotificationManager = (NotificationManager)
844 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
845 mStatusBar = statusBar;
846 statusBar.setIconVisibility("ime", false);
847 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900848 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
849 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900850 if (mShowOngoingImeSwitcherForPhones) {
851 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
852 mHardKeyboardListener);
853 }
satok0a1bcf42012-05-16 19:26:31 +0900854 buildInputMethodListLocked(mMethodList, mMethodMap);
855 if (!mImeSelectedOnBoot) {
856 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
857 checkCurrentLocaleChangedLocked();
858 }
859 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700860 try {
861 startInputInnerLocked();
862 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800863 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700864 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700865 }
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
satok15452a42011-10-28 17:58:28 +0900869 private void setImeWindowVisibilityStatusHiddenLocked() {
870 mImeWindowVis = 0;
871 updateImeWindowStatusLocked();
872 }
873
satok3afd6c02011-11-18 08:38:19 +0900874 private void refreshImeWindowVisibilityLocked() {
875 final Configuration conf = mRes.getConfiguration();
876 final boolean haveHardKeyboard = conf.keyboard
877 != Configuration.KEYBOARD_NOKEYS;
878 final boolean hardKeyShown = haveHardKeyboard
879 && conf.hardKeyboardHidden
880 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500881 final boolean isScreenLocked =
882 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
883 final boolean isScreenSecurelyLocked =
884 isScreenLocked && mKeyguardManager.isKeyguardSecure();
885 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
886 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900887 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
888 updateImeWindowStatusLocked();
889 }
890
satok15452a42011-10-28 17:58:28 +0900891 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900892 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700893 }
894
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900895 // ---------------------------------------------------------------------------------------
896 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
897 // 1) it comes from the system process
898 // 2) the calling process' user id is identical to the current user id IMMS thinks.
899 private boolean calledFromValidUser() {
900 final int uid = Binder.getCallingUid();
901 final int userId = UserHandle.getUserId(uid);
902 if (DEBUG) {
903 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
904 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
905 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900906 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900907 }
908 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
909 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900910 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900911
912 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
913 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
914 // must not manage background users' states in any functions.
915 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
916 // by a token.
917 if (mContext.checkCallingOrSelfPermission(
918 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
919 == PackageManager.PERMISSION_GRANTED) {
920 if (DEBUG) {
921 Slog.d(TAG, "--- Access granted because the calling process has "
922 + "the INTERACT_ACROSS_USERS_FULL permission");
923 }
924 return true;
925 }
926 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
927 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900928 }
929
930 private boolean bindCurrentInputMethodService(
931 Intent service, ServiceConnection conn, int flags) {
932 if (service == null || conn == null) {
933 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
934 return false;
935 }
936 return mContext.bindService(service, conn, flags, mSettings.getCurrentUserId());
937 }
938
satoke7c6998e2011-06-03 17:57:59 +0900939 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900941 // TODO: Make this work even for non-current users?
942 if (!calledFromValidUser()) {
943 return Collections.emptyList();
944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 synchronized (mMethodMap) {
946 return new ArrayList<InputMethodInfo>(mMethodList);
947 }
948 }
949
satoke7c6998e2011-06-03 17:57:59 +0900950 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900952 // TODO: Make this work even for non-current users?
953 if (!calledFromValidUser()) {
954 return Collections.emptyList();
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900957 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959 }
960
satokbb4aa062011-01-19 21:40:27 +0900961 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
962 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
963 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
964 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900965 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900966 enabledInputMethodAndSubtypes.put(
967 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
968 }
969 return enabledInputMethodAndSubtypes;
970 }
971
972 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
973 boolean allowsImplicitlySelectedSubtypes) {
974 if (imi == null && mCurMethodId != null) {
975 imi = mMethodMap.get(mCurMethodId);
976 }
satok7265d9b2011-02-14 15:47:30 +0900977 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900978 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900979 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900980 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900981 }
satok7265d9b2011-02-14 15:47:30 +0900982 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900983 }
984
satoke7c6998e2011-06-03 17:57:59 +0900985 @Override
satok16331c82010-12-20 23:48:46 +0900986 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
987 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900988 // TODO: Make this work even for non-current users?
989 if (!calledFromValidUser()) {
990 return Collections.emptyList();
991 }
satok67ddf9c2010-11-17 09:45:54 +0900992 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900993 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900994 }
995 }
996
satoke7c6998e2011-06-03 17:57:59 +0900997 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 public void addClient(IInputMethodClient client,
999 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001000 if (!calledFromValidUser()) {
1001 return;
1002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 synchronized (mMethodMap) {
1004 mClients.put(client.asBinder(), new ClientState(client,
1005 inputContext, uid, pid));
1006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
satoke7c6998e2011-06-03 17:57:59 +09001009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001011 if (!calledFromValidUser()) {
1012 return;
1013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 synchronized (mMethodMap) {
1015 mClients.remove(client.asBinder());
1016 }
1017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 void executeOrSendMessage(IInterface target, Message msg) {
1020 if (target.asBinder() instanceof Binder) {
1021 mCaller.sendMessage(msg);
1022 } else {
1023 handleMessage(msg);
1024 msg.recycle();
1025 }
1026 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001027
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001028 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001030 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 + mCurClient.client.asBinder());
1032 if (mBoundToMethod) {
1033 mBoundToMethod = false;
1034 if (mCurMethod != null) {
1035 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1036 MSG_UNBIND_INPUT, mCurMethod));
1037 }
1038 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001039
1040 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1041 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1043 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1044 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project10592532009-03-18 17:39:46 -07001047 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 private int getImeShowFlags() {
1052 int flags = 0;
1053 if (mShowForced) {
1054 flags |= InputMethod.SHOW_FORCED
1055 | InputMethod.SHOW_EXPLICIT;
1056 } else if (mShowExplicitlyRequested) {
1057 flags |= InputMethod.SHOW_EXPLICIT;
1058 }
1059 return flags;
1060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 private int getAppShowFlags() {
1063 int flags = 0;
1064 if (mShowForced) {
1065 flags |= InputMethodManager.SHOW_FORCED;
1066 } else if (!mShowExplicitlyRequested) {
1067 flags |= InputMethodManager.SHOW_IMPLICIT;
1068 }
1069 return flags;
1070 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001071
Dianne Hackborn7663d802012-02-24 13:08:49 -08001072 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 if (!mBoundToMethod) {
1074 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1075 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1076 mBoundToMethod = true;
1077 }
1078 final SessionState session = mCurClient.curSession;
1079 if (initial) {
1080 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1081 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1082 } else {
1083 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1084 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1085 }
1086 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001087 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001088 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001090 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001094 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 // If no method is currently selected, do nothing.
1096 if (mCurMethodId == null) {
1097 return mNoBinding;
1098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 ClientState cs = mClients.get(client.asBinder());
1101 if (cs == null) {
1102 throw new IllegalArgumentException("unknown client "
1103 + client.asBinder());
1104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 try {
1107 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1108 // Check with the window manager to make sure this client actually
1109 // has a window with focus. If not, reject. This is thread safe
1110 // because if the focus changes some time before or after, the
1111 // next client receiving focus that has any interest in input will
1112 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1115 return null;
1116 }
1117 } catch (RemoteException e) {
1118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001119
Dianne Hackborn7663d802012-02-24 13:08:49 -08001120 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1121 }
1122
1123 InputBindResult startInputUncheckedLocked(ClientState cs,
1124 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1125 // If no method is currently selected, do nothing.
1126 if (mCurMethodId == null) {
1127 return mNoBinding;
1128 }
1129
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001130 if (mCurClient == null) {
1131 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1132 if (DEBUG) {
1133 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1134 }
1135 }
1136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 if (mCurClient != cs) {
1138 // If the client is changing, we need to switch over to the new
1139 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001140 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001141 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 + cs.client.asBinder());
1143
1144 // If the screen is on, inform the new client it is active
1145 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001146 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1147 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Bump up the sequence for this client and attach it.
1152 mCurSeq++;
1153 if (mCurSeq <= 0) mCurSeq = 1;
1154 mCurClient = cs;
1155 mCurInputContext = inputContext;
1156 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 // Check if the input method is changing.
1159 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1160 if (cs.curSession != null) {
1161 // Fast case: if we are already connected to the input method,
1162 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001163 return attachNewInputLocked(
1164 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 }
1166 if (mHaveConnection) {
1167 if (mCurMethod != null) {
1168 if (!cs.sessionRequested) {
1169 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001170 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1172 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001173 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 // Return to client, and we will get back with it when
1176 // we have had a session made for it.
1177 return new InputBindResult(null, mCurId, mCurSeq);
1178 } else if (SystemClock.uptimeMillis()
1179 < (mLastBindTime+TIME_TO_RECONNECT)) {
1180 // In this case we have connected to the service, but
1181 // don't yet have its interface. If it hasn't been too
1182 // long since we did the connection, we'll return to
1183 // the client and wait to get the service interface so
1184 // we can report back. If it has been too long, we want
1185 // to fall through so we can try a disconnect/reconnect
1186 // to see if we can get back in touch with the service.
1187 return new InputBindResult(null, mCurId, mCurSeq);
1188 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001189 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1190 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001195 return startInputInnerLocked();
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001198 InputBindResult startInputInnerLocked() {
1199 if (mCurMethodId == null) {
1200 return mNoBinding;
1201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001202
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001203 if (!mSystemReady) {
1204 // If the system is not yet ready, we shouldn't be running third
1205 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001206 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1210 if (info == null) {
1211 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001214 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1217 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001218 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1219 com.android.internal.R.string.input_method_binding_label);
1220 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1221 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001222 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001223 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 mLastBindTime = SystemClock.uptimeMillis();
1225 mHaveConnection = true;
1226 mCurId = info.getId();
1227 mCurToken = new Binder();
1228 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001229 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 mIWindowManager.addWindowToken(mCurToken,
1231 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1232 } catch (RemoteException e) {
1233 }
1234 return new InputBindResult(null, mCurId, mCurSeq);
1235 } else {
1236 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001237 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 + mCurIntent);
1239 }
1240 return null;
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
satoke7c6998e2011-06-03 17:57:59 +09001243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001245 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001246 if (!calledFromValidUser()) {
1247 return null;
1248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 synchronized (mMethodMap) {
1250 final long ident = Binder.clearCallingIdentity();
1251 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001252 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 } finally {
1254 Binder.restoreCallingIdentity(ident);
1255 }
1256 }
1257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258
satoke7c6998e2011-06-03 17:57:59 +09001259 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 public void finishInput(IInputMethodClient client) {
1261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001262
satoke7c6998e2011-06-03 17:57:59 +09001263 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 public void onServiceConnected(ComponentName name, IBinder service) {
1265 synchronized (mMethodMap) {
1266 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1267 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001268 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001269 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001270 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001271 return;
1272 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001273 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001274 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1275 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001277 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001278 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001280 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001281 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
1284 }
1285 }
1286
1287 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1288 synchronized (mMethodMap) {
1289 if (mCurMethod != null && method != null
1290 && mCurMethod.asBinder() == method.asBinder()) {
1291 if (mCurClient != null) {
1292 mCurClient.curSession = new SessionState(mCurClient,
1293 method, session);
1294 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001295 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 if (res.method != null) {
1297 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1298 MSG_BIND_METHOD, mCurClient.client, res));
1299 }
1300 }
1301 }
1302 }
1303 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001304
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001305 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001306 if (mVisibleBound) {
1307 mContext.unbindService(mVisibleConnection);
1308 mVisibleBound = false;
1309 }
1310
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001311 if (mHaveConnection) {
1312 mContext.unbindService(this);
1313 mHaveConnection = false;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 if (mCurToken != null) {
1317 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001319 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001320 // The current IME is shown. Hence an IME switch (transition) is happening.
1321 mWindowManagerService.saveLastInputMethodWindowForTransition();
1322 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 mIWindowManager.removeWindowToken(mCurToken);
1324 } catch (RemoteException e) {
1325 }
1326 mCurToken = null;
1327 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001328
The Android Open Source Project10592532009-03-18 17:39:46 -07001329 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001330 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001332 if (reportToClient && mCurClient != null) {
1333 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1334 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1335 }
1336 }
1337
Devin Taylor0c33ed22010-02-23 13:26:46 -06001338 private void finishSession(SessionState sessionState) {
1339 if (sessionState != null && sessionState.session != null) {
1340 try {
1341 sessionState.session.finishSession();
1342 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001343 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001344 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001345 }
1346 }
1347 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001348
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001349 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (mCurMethod != null) {
1351 for (ClientState cs : mClients.values()) {
1352 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001353 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 cs.curSession = null;
1355 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001356
1357 finishSession(mEnabledSession);
1358 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 mCurMethod = null;
1360 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001361 if (mStatusBar != null) {
1362 mStatusBar.setIconVisibility("ime", false);
1363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
satoke7c6998e2011-06-03 17:57:59 +09001366 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public void onServiceDisconnected(ComponentName name) {
1368 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001369 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 + " mCurIntent=" + mCurIntent);
1371 if (mCurMethod != null && mCurIntent != null
1372 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001373 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // We consider this to be a new bind attempt, since the system
1375 // should now try to restart the service for us.
1376 mLastBindTime = SystemClock.uptimeMillis();
1377 mShowRequested = mInputShown;
1378 mInputShown = false;
1379 if (mCurClient != null) {
1380 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1381 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1382 }
1383 }
1384 }
1385 }
1386
satokf9f01002011-05-19 21:31:50 +09001387 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001389 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 long ident = Binder.clearCallingIdentity();
1391 try {
1392 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001393 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 return;
1395 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 synchronized (mMethodMap) {
1398 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001399 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001400 if (mStatusBar != null) {
1401 mStatusBar.setIconVisibility("ime", false);
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001405 CharSequence contentDescription = null;
1406 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001407 // Use PackageManager to load label
1408 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001409 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001410 mIPackageManager.getApplicationInfo(packageName, 0,
1411 mSettings.getCurrentUserId()));
1412 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001413 /* ignore */
1414 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001415 if (mStatusBar != null) {
1416 mStatusBar.setIcon("ime", packageName, iconId, 0,
1417 contentDescription != null
1418 ? contentDescription.toString() : null);
1419 mStatusBar.setIconVisibility("ime", true);
1420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 }
1422 }
1423 } finally {
1424 Binder.restoreCallingIdentity(ident);
1425 }
1426 }
1427
satok7cfc0ed2011-06-20 21:29:36 +09001428 private boolean needsToShowImeSwitchOngoingNotification() {
1429 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001430 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001431 synchronized (mMethodMap) {
1432 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1433 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001434 if (N > 2) return true;
1435 if (N < 1) return false;
1436 int nonAuxCount = 0;
1437 int auxCount = 0;
1438 InputMethodSubtype nonAuxSubtype = null;
1439 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001440 for(int i = 0; i < N; ++i) {
1441 final InputMethodInfo imi = imis.get(i);
1442 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1443 imi, true);
1444 final int subtypeCount = subtypes.size();
1445 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001446 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001447 } else {
1448 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001449 final InputMethodSubtype subtype = subtypes.get(j);
1450 if (!subtype.isAuxiliary()) {
1451 ++nonAuxCount;
1452 nonAuxSubtype = subtype;
1453 } else {
1454 ++auxCount;
1455 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001456 }
1457 }
1458 }
satok7cfc0ed2011-06-20 21:29:36 +09001459 }
satokb6359412011-06-28 17:47:41 +09001460 if (nonAuxCount > 1 || auxCount > 1) {
1461 return true;
1462 } else if (nonAuxCount == 1 && auxCount == 1) {
1463 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001464 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1465 || auxSubtype.overridesImplicitlyEnabledSubtype()
1466 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001467 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1468 return false;
1469 }
1470 return true;
1471 }
1472 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001473 }
satok7cfc0ed2011-06-20 21:29:36 +09001474 }
1475
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001476 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001477 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001478 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001479 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001480 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001481 try {
1482 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001484 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001485 return;
1486 }
1487
1488 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001489 mImeWindowVis = vis;
1490 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001491 if (mStatusBar != null) {
1492 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1493 }
satok7cfc0ed2011-06-20 21:29:36 +09001494 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001495 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1496 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001497 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001498 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001499 final CharSequence title = mRes.getText(
1500 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001501 final CharSequence imiLabel = imi.loadLabel(pm);
1502 final CharSequence summary = mCurrentSubtype != null
1503 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1504 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1505 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001506 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001507 : imiLabel;
1508
satok7cfc0ed2011-06-20 21:29:36 +09001509 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001510 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001511 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001512 if (DEBUG) {
1513 Slog.d(TAG, "--- show notification: label = " + imiLabel
1514 + ", summary = " + summary);
1515 }
1516 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001517 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001518 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001519 mNotificationShown = true;
1520 }
satok7cfc0ed2011-06-20 21:29:36 +09001521 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001522 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001523 if (DEBUG) {
1524 Slog.d(TAG, "--- hide notification");
1525 }
1526 mNotificationManager.cancelAsUser(null,
1527 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001528 mNotificationShown = false;
1529 }
1530 }
satok06487a52010-10-29 11:37:18 +09001531 }
1532 } finally {
1533 Binder.restoreCallingIdentity(ident);
1534 }
1535 }
1536
satoke7c6998e2011-06-03 17:57:59 +09001537 @Override
satokf9f01002011-05-19 21:31:50 +09001538 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001539 if (!calledFromValidUser()) {
1540 return;
1541 }
satokf9f01002011-05-19 21:31:50 +09001542 synchronized (mMethodMap) {
1543 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1544 for (int i = 0; i < spans.length; ++i) {
1545 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001546 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001547 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001548 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001549 }
1550 }
1551 }
1552 }
1553
satoke7c6998e2011-06-03 17:57:59 +09001554 @Override
satokf9f01002011-05-19 21:31:50 +09001555 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001556 if (!calledFromValidUser()) {
1557 return false;
1558 }
satokf9f01002011-05-19 21:31:50 +09001559 synchronized (mMethodMap) {
1560 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1561 // TODO: Do not send the intent if the process of the targetImi is already dead.
1562 if (targetImi != null) {
1563 final String[] suggestions = span.getSuggestions();
1564 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001565 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001566 final Intent intent = new Intent();
1567 // Ensures that only a class in the original IME package will receive the
1568 // notification.
satok42c5a162011-05-26 16:46:14 +09001569 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001570 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1571 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1572 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1573 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001574 final long ident = Binder.clearCallingIdentity();
1575 try {
1576 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1577 } finally {
1578 Binder.restoreCallingIdentity(ident);
1579 }
satokf9f01002011-05-19 21:31:50 +09001580 return true;
1581 }
1582 }
1583 return false;
1584 }
1585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001587 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1588 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1589 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1590 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001591 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001592 // There is no input method selected, try to choose new applicable input method.
1593 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001595 }
1596 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 try {
satokab751aa2010-09-14 19:17:36 +09001598 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001601 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001602 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 }
satokf3db1af2010-11-23 13:34:33 +09001604 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001605 } else {
1606 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001607 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001608 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
1610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001611
satokab751aa2010-09-14 19:17:36 +09001612 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 InputMethodInfo info = mMethodMap.get(id);
1614 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001615 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
satokd81e9502012-05-21 12:58:45 +09001618 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001620 final int subtypeCount = info.getSubtypeCount();
1621 if (subtypeCount <= 0) {
1622 return;
satokcd7cd292010-11-20 15:46:23 +09001623 }
satokd81e9502012-05-21 12:58:45 +09001624 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1625 final InputMethodSubtype newSubtype;
1626 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1627 newSubtype = info.getSubtypeAt(subtypeId);
1628 } else {
1629 // If subtype is null, try to find the most applicable one from
1630 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001631 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001632 }
1633 if (newSubtype == null || oldSubtype == null) {
1634 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1635 + ", new subtype = " + newSubtype);
1636 return;
1637 }
1638 if (newSubtype != oldSubtype) {
1639 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1640 if (mCurMethod != null) {
1641 try {
1642 refreshImeWindowVisibilityLocked();
1643 mCurMethod.changeInputMethodSubtype(newSubtype);
1644 } catch (RemoteException e) {
1645 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001646 }
1647 }
1648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 return;
1650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
satokd81e9502012-05-21 12:58:45 +09001652 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 final long ident = Binder.clearCallingIdentity();
1654 try {
satokab751aa2010-09-14 19:17:36 +09001655 // Set a subtype to this input method.
1656 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001657 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1658 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1659 // because mCurMethodId is stored as a history in
1660 // setSelectedInputMethodAndSubtypeLocked().
1661 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662
1663 if (ActivityManagerNative.isSystemReady()) {
1664 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001665 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001667 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001669 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 } finally {
1671 Binder.restoreCallingIdentity(ident);
1672 }
1673 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001674
satok42c5a162011-05-26 16:46:14 +09001675 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001676 public boolean showSoftInput(IInputMethodClient client, int flags,
1677 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001678 if (!calledFromValidUser()) {
1679 return false;
1680 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001681 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 long ident = Binder.clearCallingIdentity();
1683 try {
1684 synchronized (mMethodMap) {
1685 if (mCurClient == null || client == null
1686 || mCurClient.client.asBinder() != client.asBinder()) {
1687 try {
1688 // We need to check if this is the current client with
1689 // focus in the window manager, to allow this call to
1690 // be made before input is started in it.
1691 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001692 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001693 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001696 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699
Joe Onorato8a9b2202010-02-26 18:56:32 -08001700 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001701 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703 } finally {
1704 Binder.restoreCallingIdentity(ident);
1705 }
1706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001707
The Android Open Source Project4df24232009-03-05 14:34:35 -08001708 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 mShowRequested = true;
1710 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1711 mShowExplicitlyRequested = true;
1712 }
1713 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1714 mShowExplicitlyRequested = true;
1715 mShowForced = true;
1716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001717
Dianne Hackborncc278702009-09-02 23:07:23 -07001718 if (!mSystemReady) {
1719 return false;
1720 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001721
The Android Open Source Project4df24232009-03-05 14:34:35 -08001722 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001724 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1725 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1726 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001728 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001729 bindCurrentInputMethodService(
1730 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001731 mVisibleBound = true;
1732 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001733 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001735 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 // The client has asked to have the input method shown, but
1737 // we have been sitting here too long with a connection to the
1738 // service and no interface received, so let's disconnect/connect
1739 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001740 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001742 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001744 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001745 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001746 } else {
1747 if (DEBUG) {
1748 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1749 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
The Android Open Source Project4df24232009-03-05 14:34:35 -08001753 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
satok42c5a162011-05-26 16:46:14 +09001756 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 public boolean hideSoftInput(IInputMethodClient client, int flags,
1758 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001759 if (!calledFromValidUser()) {
1760 return false;
1761 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001762 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 long ident = Binder.clearCallingIdentity();
1764 try {
1765 synchronized (mMethodMap) {
1766 if (mCurClient == null || client == null
1767 || mCurClient.client.asBinder() != client.asBinder()) {
1768 try {
1769 // We need to check if this is the current client with
1770 // focus in the window manager, to allow this call to
1771 // be made before input is started in it.
1772 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001773 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1774 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001775 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001776 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 }
1778 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001779 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001780 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
1782 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001783
Joe Onorato8a9b2202010-02-26 18:56:32 -08001784 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001785 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
1787 } finally {
1788 Binder.restoreCallingIdentity(ident);
1789 }
1790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1794 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001795 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001797 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001800 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001804 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001806 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1807 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1808 res = true;
1809 } else {
1810 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001812 if (mHaveConnection && mVisibleBound) {
1813 mContext.unbindService(mVisibleConnection);
1814 mVisibleBound = false;
1815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 mInputShown = false;
1817 mShowRequested = false;
1818 mShowExplicitlyRequested = false;
1819 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001820 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
satok42c5a162011-05-26 16:46:14 +09001823 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001824 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1825 int controlFlags, int softInputMode, int windowFlags,
1826 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001827 if (!calledFromValidUser()) {
1828 return null;
1829 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001830 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 long ident = Binder.clearCallingIdentity();
1832 try {
1833 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001834 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001835 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001837 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001838
Dianne Hackborn7663d802012-02-24 13:08:49 -08001839 ClientState cs = mClients.get(client.asBinder());
1840 if (cs == null) {
1841 throw new IllegalArgumentException("unknown client "
1842 + client.asBinder());
1843 }
1844
1845 try {
1846 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1847 // Check with the window manager to make sure this client actually
1848 // has a window with focus. If not, reject. This is thread safe
1849 // because if the focus changes some time before or after, the
1850 // next client receiving focus that has any interest in input will
1851 // be calling through here after that change happens.
1852 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1853 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1854 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001856 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001859 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001860 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001861 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001862 if (attribute != null) {
1863 return startInputUncheckedLocked(cs, inputContext, attribute,
1864 controlFlags);
1865 }
1866 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001867 }
1868 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001869
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001870 // Should we auto-show the IME even if the caller has not
1871 // specified what should be done with it?
1872 // We only do this automatically if the window can resize
1873 // to accommodate the IME (so what the user sees will give
1874 // them good context without input information being obscured
1875 // by the IME) or if running on a large screen where there
1876 // is more room for the target window + IME.
1877 final boolean doAutoShow =
1878 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1879 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1880 || mRes.getConfiguration().isLayoutSizeAtLeast(
1881 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001882 final boolean isTextEditor =
1883 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1884
1885 // We want to start input before showing the IME, but after closing
1886 // it. We want to do this after closing it to help the IME disappear
1887 // more quickly (not get stuck behind it initializing itself for the
1888 // new focused input, even if its window wants to hide the IME).
1889 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1892 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001893 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1895 // There is no focus view, and this window will
1896 // be behind any soft input window, so hide the
1897 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001898 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001899 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001901 } else if (isTextEditor && doAutoShow && (softInputMode &
1902 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 // There is a focus view, and we are navigating forward
1904 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001905 // We only do this automatically if the window can resize
1906 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001907 // them good context without input information being obscured
1908 // by the IME) or if running on a large screen where there
1909 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001910 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001911 if (attribute != null) {
1912 res = startInputUncheckedLocked(cs, inputContext, attribute,
1913 controlFlags);
1914 didStart = true;
1915 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001916 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 }
1918 break;
1919 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1920 // Do nothing.
1921 break;
1922 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1923 if ((softInputMode &
1924 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001925 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001926 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928 break;
1929 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001930 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001931 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 break;
1933 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1934 if ((softInputMode &
1935 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001936 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
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 }
1944 break;
1945 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001947 if (attribute != null) {
1948 res = startInputUncheckedLocked(cs, inputContext, attribute,
1949 controlFlags);
1950 didStart = true;
1951 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001952 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 break;
1954 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001955
1956 if (!didStart && attribute != null) {
1957 res = startInputUncheckedLocked(cs, inputContext, attribute,
1958 controlFlags);
1959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
1961 } finally {
1962 Binder.restoreCallingIdentity(ident);
1963 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001964
1965 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001967
satok42c5a162011-05-26 16:46:14 +09001968 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001970 if (!calledFromValidUser()) {
1971 return;
1972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 synchronized (mMethodMap) {
1974 if (mCurClient == null || client == null
1975 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001976 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001977 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979
satok440aab52010-11-25 09:43:11 +09001980 // Always call subtype picker, because subtype picker is a superset of input method
1981 // picker.
satokab751aa2010-09-14 19:17:36 +09001982 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1983 }
1984 }
1985
satok42c5a162011-05-26 16:46:14 +09001986 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001988 if (!calledFromValidUser()) {
1989 return;
1990 }
satok28203512010-11-24 11:06:49 +09001991 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
1992 }
1993
satok42c5a162011-05-26 16:46:14 +09001994 @Override
satok28203512010-11-24 11:06:49 +09001995 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001996 if (!calledFromValidUser()) {
1997 return;
1998 }
satok28203512010-11-24 11:06:49 +09001999 synchronized (mMethodMap) {
2000 if (subtype != null) {
2001 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
2002 mMethodMap.get(id), subtype.hashCode()));
2003 } else {
2004 setInputMethod(token, id);
2005 }
2006 }
satokab751aa2010-09-14 19:17:36 +09002007 }
2008
satok42c5a162011-05-26 16:46:14 +09002009 @Override
satokb416a71e2010-11-25 20:42:14 +09002010 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002011 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002012 if (!calledFromValidUser()) {
2013 return;
2014 }
satokb416a71e2010-11-25 20:42:14 +09002015 synchronized (mMethodMap) {
2016 if (mCurClient == null || client == null
2017 || mCurClient.client.asBinder() != client.asBinder()) {
2018 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2019 }
satok7fee71f2010-12-17 18:54:26 +09002020 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2021 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09002022 }
2023 }
2024
satok4fc87d62011-05-20 16:13:43 +09002025 @Override
satok735cf382010-11-11 20:40:09 +09002026 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002027 if (!calledFromValidUser()) {
2028 return false;
2029 }
satok735cf382010-11-11 20:40:09 +09002030 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002031 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002032 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002033 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002034 lastImi = mMethodMap.get(lastIme.first);
2035 } else {
2036 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002037 }
satok4fc87d62011-05-20 16:13:43 +09002038 String targetLastImiId = null;
2039 int subtypeId = NOT_A_SUBTYPE_ID;
2040 if (lastIme != null && lastImi != null) {
2041 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2042 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2043 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2044 : mCurrentSubtype.hashCode();
2045 // If the last IME is the same as the current IME and the last subtype is not
2046 // defined, there is no need to switch to the last IME.
2047 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2048 targetLastImiId = lastIme.first;
2049 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2050 }
2051 }
2052
2053 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2054 // This is a safety net. If the currentSubtype can't be added to the history
2055 // and the framework couldn't find the last ime, we will make the last ime be
2056 // the most applicable enabled keyboard subtype of the system imes.
2057 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2058 if (enabled != null) {
2059 final int N = enabled.size();
2060 final String locale = mCurrentSubtype == null
2061 ? mRes.getConfiguration().locale.toString()
2062 : mCurrentSubtype.getLocale();
2063 for (int i = 0; i < N; ++i) {
2064 final InputMethodInfo imi = enabled.get(i);
2065 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2066 InputMethodSubtype keyboardSubtype =
2067 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2068 SUBTYPE_MODE_KEYBOARD, locale, true);
2069 if (keyboardSubtype != null) {
2070 targetLastImiId = imi.getId();
2071 subtypeId = getSubtypeIdFromHashCode(
2072 imi, keyboardSubtype.hashCode());
2073 if(keyboardSubtype.getLocale().equals(locale)) {
2074 break;
2075 }
2076 }
2077 }
2078 }
2079 }
2080 }
2081
2082 if (!TextUtils.isEmpty(targetLastImiId)) {
2083 if (DEBUG) {
2084 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2085 + ", from: " + mCurMethodId + ", " + subtypeId);
2086 }
2087 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2088 return true;
2089 } else {
2090 return false;
2091 }
satok735cf382010-11-11 20:40:09 +09002092 }
2093 }
2094
satoke7c6998e2011-06-03 17:57:59 +09002095 @Override
satok688bd472012-02-09 20:09:17 +09002096 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002097 if (!calledFromValidUser()) {
2098 return false;
2099 }
satok688bd472012-02-09 20:09:17 +09002100 synchronized (mMethodMap) {
2101 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2102 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2103 if (nextSubtype == null) {
2104 return false;
2105 }
2106 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2107 return true;
2108 }
2109 }
2110
2111 @Override
satok68f1b782011-04-11 14:26:04 +09002112 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002113 if (!calledFromValidUser()) {
2114 return null;
2115 }
satok68f1b782011-04-11 14:26:04 +09002116 synchronized (mMethodMap) {
2117 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2118 // TODO: Handle the case of the last IME with no subtypes
2119 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2120 || TextUtils.isEmpty(lastIme.second)) return null;
2121 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2122 if (lastImi == null) return null;
2123 try {
2124 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002125 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2126 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2127 return null;
2128 }
2129 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002130 } catch (NumberFormatException e) {
2131 return null;
2132 }
2133 }
2134 }
2135
satoke7c6998e2011-06-03 17:57:59 +09002136 @Override
satokee5e77c2011-09-02 18:50:15 +09002137 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002138 if (!calledFromValidUser()) {
2139 return;
2140 }
satok91e88122011-07-18 11:11:42 +09002141 // By this IPC call, only a process which shares the same uid with the IME can add
2142 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002143 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002144 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002145 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002146 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002147 final String[] packageInfos;
2148 try {
2149 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2150 } catch (RemoteException e) {
2151 Slog.e(TAG, "Failed to get package infos");
2152 return;
2153 }
satok91e88122011-07-18 11:11:42 +09002154 if (packageInfos != null) {
2155 final int packageNum = packageInfos.length;
2156 for (int i = 0; i < packageNum; ++i) {
2157 if (packageInfos[i].equals(imi.getPackageName())) {
2158 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002159 final long ident = Binder.clearCallingIdentity();
2160 try {
2161 buildInputMethodListLocked(mMethodList, mMethodMap);
2162 } finally {
2163 Binder.restoreCallingIdentity(ident);
2164 }
satokee5e77c2011-09-02 18:50:15 +09002165 return;
satok91e88122011-07-18 11:11:42 +09002166 }
2167 }
2168 }
satoke7c6998e2011-06-03 17:57:59 +09002169 }
satokee5e77c2011-09-02 18:50:15 +09002170 return;
satoke7c6998e2011-06-03 17:57:59 +09002171 }
2172
satok28203512010-11-24 11:06:49 +09002173 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 synchronized (mMethodMap) {
2175 if (token == null) {
2176 if (mContext.checkCallingOrSelfPermission(
2177 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2178 != PackageManager.PERMISSION_GRANTED) {
2179 throw new SecurityException(
2180 "Using null token requires permission "
2181 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2182 }
2183 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002184 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2185 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 return;
2187 }
2188
satokc5933802011-08-31 21:26:04 +09002189 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 try {
satokab751aa2010-09-14 19:17:36 +09002191 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 } finally {
2193 Binder.restoreCallingIdentity(ident);
2194 }
2195 }
2196 }
2197
satok42c5a162011-05-26 16:46:14 +09002198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002200 if (!calledFromValidUser()) {
2201 return;
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 synchronized (mMethodMap) {
2204 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002205 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2206 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 return;
2208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 long ident = Binder.clearCallingIdentity();
2210 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002211 hideCurrentInputLocked(flags, null);
2212 } finally {
2213 Binder.restoreCallingIdentity(ident);
2214 }
2215 }
2216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002217
satok42c5a162011-05-26 16:46:14 +09002218 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002219 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002220 if (!calledFromValidUser()) {
2221 return;
2222 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002223 synchronized (mMethodMap) {
2224 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002225 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2226 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002227 return;
2228 }
2229 long ident = Binder.clearCallingIdentity();
2230 try {
2231 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 } finally {
2233 Binder.restoreCallingIdentity(ident);
2234 }
2235 }
2236 }
2237
2238 void setEnabledSessionInMainThread(SessionState session) {
2239 if (mEnabledSession != session) {
2240 if (mEnabledSession != null) {
2241 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002242 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 mEnabledSession.method.setSessionEnabled(
2244 mEnabledSession.session, false);
2245 } catch (RemoteException e) {
2246 }
2247 }
2248 mEnabledSession = session;
2249 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002250 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 session.method.setSessionEnabled(
2252 session.session, true);
2253 } catch (RemoteException e) {
2254 }
2255 }
2256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
satok42c5a162011-05-26 16:46:14 +09002258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002259 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002260 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 switch (msg.what) {
2262 case MSG_SHOW_IM_PICKER:
2263 showInputMethodMenu();
2264 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002265
satokab751aa2010-09-14 19:17:36 +09002266 case MSG_SHOW_IM_SUBTYPE_PICKER:
2267 showInputMethodSubtypeMenu();
2268 return true;
2269
satok47a44912010-10-06 16:03:58 +09002270 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002271 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002272 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002273 args.recycle();
satok217f5482010-12-15 05:19:19 +09002274 return true;
2275
2276 case MSG_SHOW_IM_CONFIG:
2277 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002278 return true;
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 case MSG_UNBIND_INPUT:
2283 try {
2284 ((IInputMethod)msg.obj).unbindInput();
2285 } catch (RemoteException e) {
2286 // There is nothing interesting about the method dying.
2287 }
2288 return true;
2289 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002290 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 try {
2292 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2293 } catch (RemoteException e) {
2294 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002295 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 return true;
2297 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002298 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002300 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2301 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 } catch (RemoteException e) {
2303 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002304 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 return true;
2306 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002307 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002309 ((IInputMethod)args.arg1).hideSoftInput(0,
2310 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 } catch (RemoteException e) {
2312 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002313 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 return true;
2315 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002316 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002318 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2320 } catch (RemoteException e) {
2321 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002322 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 return true;
2324 case MSG_CREATE_SESSION:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002325 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 try {
2327 ((IInputMethod)args.arg1).createSession(
2328 (IInputMethodCallback)args.arg2);
2329 } catch (RemoteException e) {
2330 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002331 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 try {
2338 SessionState session = (SessionState)args.arg1;
2339 setEnabledSessionInMainThread(session);
2340 session.method.startInput((IInputContext)args.arg2,
2341 (EditorInfo)args.arg3);
2342 } catch (RemoteException e) {
2343 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 return true;
2346 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002347 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 try {
2349 SessionState session = (SessionState)args.arg1;
2350 setEnabledSessionInMainThread(session);
2351 session.method.restartInput((IInputContext)args.arg2,
2352 (EditorInfo)args.arg3);
2353 } catch (RemoteException e) {
2354 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002355 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 case MSG_UNBIND_METHOD:
2361 try {
2362 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2363 } catch (RemoteException e) {
2364 // There is nothing interesting about the last client dying.
2365 }
2366 return true;
2367 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002368 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 try {
2370 ((IInputMethodClient)args.arg1).onBindMethod(
2371 (InputBindResult)args.arg2);
2372 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002373 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002375 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002377 case MSG_SET_ACTIVE:
2378 try {
2379 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2380 } catch (RemoteException e) {
2381 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2382 + ((ClientState)msg.obj).pid + " uid "
2383 + ((ClientState)msg.obj).uid);
2384 }
2385 return true;
satok01038492012-04-09 21:08:27 +09002386
2387 // --------------------------------------------------------------
2388 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2389 mHardKeyboardListener.handleHardKeyboardStatusChange(
2390 msg.arg1 == 1, msg.arg2 == 1);
2391 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 }
2393 return false;
2394 }
2395
satok5b927c432012-05-01 20:09:34 +09002396 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002397 return (inputMethod.getServiceInfo().applicationInfo.flags
2398 & ApplicationInfo.FLAG_SYSTEM) != 0;
2399 }
2400
Ken Wakasa586f0512011-01-20 22:31:01 +09002401 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2402 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2403 final int subtypeCount = imi.getSubtypeCount();
2404 for (int i = 0; i < subtypeCount; ++i) {
2405 subtypes.add(imi.getSubtypeAt(i));
2406 }
2407 return subtypes;
2408 }
2409
satoka86f5e42011-09-02 17:12:42 +09002410 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2411 InputMethodInfo imi, String mode) {
2412 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2413 final int subtypeCount = imi.getSubtypeCount();
2414 for (int i = 0; i < subtypeCount; ++i) {
2415 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2416 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2417 subtypes.add(subtype);
2418 }
2419 }
2420 return subtypes;
2421 }
2422
satokdc9ddae2011-10-06 12:22:36 +09002423 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002424 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002425 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002426 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002427 int i = enabled.size();
2428 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002429 while (i > 0) {
2430 i--;
satokdc9ddae2011-10-06 12:22:36 +09002431 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002432 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2433 return imi;
2434 }
2435 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2436 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002437 }
2438 }
satok0a1bcf42012-05-16 19:26:31 +09002439 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002440 }
2441 return null;
2442 }
2443
2444 private boolean chooseNewDefaultIMELocked() {
2445 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2446 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002447 if (DEBUG) {
2448 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2449 }
satok723a27e2010-11-11 14:58:11 +09002450 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002451 return true;
2452 }
2453
2454 return false;
2455 }
2456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2458 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002459 if (DEBUG) {
2460 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 list.clear();
2463 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002464
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002465 // Use for queryIntentServicesAsUser
2466 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002467 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002468 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002469 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002470 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002472 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002474 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002475
satoke7c6998e2011-06-03 17:57:59 +09002476 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2477 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 for (int i = 0; i < services.size(); ++i) {
2479 ResolveInfo ri = services.get(i);
2480 ServiceInfo si = ri.serviceInfo;
2481 ComponentName compName = new ComponentName(si.packageName, si.name);
2482 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2483 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002484 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 + ": it does not require the permission "
2486 + android.Manifest.permission.BIND_INPUT_METHOD);
2487 continue;
2488 }
2489
Joe Onorato8a9b2202010-02-26 18:56:32 -08002490 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491
2492 try {
satoke7c6998e2011-06-03 17:57:59 +09002493 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002495 final String id = p.getId();
2496 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497
satok5b927c432012-05-01 20:09:34 +09002498 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002499 // by default
2500 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002501 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002502 }
2503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002505 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002509 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002511 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 }
2513 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002514
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002515 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002516 if (!TextUtils.isEmpty(defaultImiId)) {
2517 if (!map.containsKey(defaultImiId)) {
2518 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2519 if (chooseNewDefaultIMELocked()) {
2520 updateFromSettingsLocked();
2521 }
2522 } else {
2523 // Double check that the default IME is certainly enabled.
2524 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002525 }
2526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002530
satokab751aa2010-09-14 19:17:36 +09002531 private void showInputMethodMenu() {
2532 showInputMethodMenuInternal(false);
2533 }
2534
2535 private void showInputMethodSubtypeMenu() {
2536 showInputMethodMenuInternal(true);
2537 }
2538
satok217f5482010-12-15 05:19:19 +09002539 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002540 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002541 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002542 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2543 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002544 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002545 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002546 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002547 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002548 }
2549
2550 private void showConfigureInputMethods() {
2551 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2552 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2553 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2554 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002555 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002556 }
2557
satok2c93efc2012-04-02 19:33:47 +09002558 private boolean isScreenLocked() {
2559 return mKeyguardManager != null
2560 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2561 }
satokab751aa2010-09-14 19:17:36 +09002562 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002563 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002566 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002567
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002568 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002569 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002570 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002571
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002572 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002573 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2574 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002575 if (immis == null || immis.size() == 0) {
2576 return;
2577 }
2578
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002579 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580
satok688bd472012-02-09 20:09:17 +09002581 final List<ImeSubtypeListItem> imList =
2582 mImListManager.getSortedInputMethodAndSubtypeList(
2583 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002584
satokc3690562012-01-10 20:14:43 +09002585 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002586 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002587 if (currentSubtype != null) {
2588 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2589 lastInputMethodSubtypeId =
2590 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2591 }
2592 }
2593
Ken Wakasa761eb372011-03-04 19:06:18 +09002594 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002595 mIms = new InputMethodInfo[N];
2596 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002597 int checkedItem = 0;
2598 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002599 final ImeSubtypeListItem item = imList.get(i);
2600 mIms[i] = item.mImi;
2601 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002602 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002603 int subtypeId = mSubtypeIds[i];
2604 if ((subtypeId == NOT_A_SUBTYPE_ID)
2605 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2606 || (subtypeId == lastInputMethodSubtypeId)) {
2607 checkedItem = i;
2608 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002611 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002612 com.android.internal.R.styleable.DialogPreference,
2613 com.android.internal.R.attr.alertDialogStyle, 0);
2614 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002615 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002616 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002617 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 }
2620 })
2621 .setIcon(a.getDrawable(
2622 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2623 a.recycle();
satok01038492012-04-09 21:08:27 +09002624 final LayoutInflater inflater =
2625 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2626 final View tv = inflater.inflate(
2627 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2628 mDialogBuilder.setCustomTitle(tv);
2629
2630 // Setup layout for a toggle switch of the hardware keyboard
2631 mSwitchingDialogTitleView = tv;
2632 mSwitchingDialogTitleView.findViewById(
2633 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2634 mWindowManagerService.isHardKeyboardAvailable() ?
2635 View.VISIBLE : View.GONE);
2636 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2637 com.android.internal.R.id.hard_keyboard_switch));
2638 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2639 hardKeySwitch.setOnCheckedChangeListener(
2640 new OnCheckedChangeListener() {
2641 @Override
2642 public void onCheckedChanged(
2643 CompoundButton buttonView, boolean isChecked) {
2644 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2645 }
2646 });
satokd87c2592010-09-29 11:52:06 +09002647
Ken Wakasa05dbb652011-08-22 15:22:43 +09002648 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2649 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2650 checkedItem);
2651
2652 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002653 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002654 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002655 public void onClick(DialogInterface dialog, int which) {
2656 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002657 if (mIms == null || mIms.length <= which
2658 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002659 return;
2660 }
2661 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002662 int subtypeId = mSubtypeIds[which];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002663 hideInputMethodMenu();
2664 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002665 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002666 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002667 subtypeId = NOT_A_SUBTYPE_ID;
2668 }
2669 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002670 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002673 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674
satokbc81b692011-08-26 16:22:22 +09002675 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002676 mDialogBuilder.setPositiveButton(
2677 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002678 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002679 @Override
satok7f35c8c2010-10-07 21:13:11 +09002680 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002681 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002682 }
2683 });
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002686 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 mSwitchingDialog.getWindow().setType(
2688 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002689 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2690 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002691 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 mSwitchingDialog.show();
2693 }
2694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002695
satok93d744d2012-05-09 17:14:08 +09002696 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002697 public final CharSequence mImeName;
2698 public final CharSequence mSubtypeName;
2699 public final InputMethodInfo mImi;
2700 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002701 private final boolean mIsSystemLocale;
2702 private final boolean mIsSystemLanguage;
2703
Ken Wakasa05dbb652011-08-22 15:22:43 +09002704 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002705 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002706 mImeName = imeName;
2707 mSubtypeName = subtypeName;
2708 mImi = imi;
2709 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002710 if (TextUtils.isEmpty(subtypeLocale)) {
2711 mIsSystemLocale = false;
2712 mIsSystemLanguage = false;
2713 } else {
2714 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2715 mIsSystemLanguage = mIsSystemLocale
2716 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2717 }
2718 }
2719
2720 @Override
2721 public int compareTo(ImeSubtypeListItem other) {
2722 if (TextUtils.isEmpty(mImeName)) {
2723 return 1;
2724 }
2725 if (TextUtils.isEmpty(other.mImeName)) {
2726 return -1;
2727 }
2728 if (!TextUtils.equals(mImeName, other.mImeName)) {
2729 return mImeName.toString().compareTo(other.mImeName.toString());
2730 }
2731 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2732 return 0;
2733 }
2734 if (mIsSystemLocale) {
2735 return -1;
2736 }
2737 if (other.mIsSystemLocale) {
2738 return 1;
2739 }
2740 if (mIsSystemLanguage) {
2741 return -1;
2742 }
2743 if (other.mIsSystemLanguage) {
2744 return 1;
2745 }
2746 if (TextUtils.isEmpty(mSubtypeName)) {
2747 return 1;
2748 }
2749 if (TextUtils.isEmpty(other.mSubtypeName)) {
2750 return -1;
2751 }
2752 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002753 }
2754 }
2755
2756 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2757 private final LayoutInflater mInflater;
2758 private final int mTextViewResourceId;
2759 private final List<ImeSubtypeListItem> mItemsList;
2760 private final int mCheckedItem;
2761 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2762 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2763 super(context, textViewResourceId, itemsList);
2764 mTextViewResourceId = textViewResourceId;
2765 mItemsList = itemsList;
2766 mCheckedItem = checkedItem;
2767 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2768 }
2769
2770 @Override
2771 public View getView(int position, View convertView, ViewGroup parent) {
2772 final View view = convertView != null ? convertView
2773 : mInflater.inflate(mTextViewResourceId, null);
2774 if (position < 0 || position >= mItemsList.size()) return view;
2775 final ImeSubtypeListItem item = mItemsList.get(position);
2776 final CharSequence imeName = item.mImeName;
2777 final CharSequence subtypeName = item.mSubtypeName;
2778 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2779 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2780 if (TextUtils.isEmpty(subtypeName)) {
2781 firstTextView.setText(imeName);
2782 secondTextView.setVisibility(View.GONE);
2783 } else {
2784 firstTextView.setText(subtypeName);
2785 secondTextView.setText(imeName);
2786 secondTextView.setVisibility(View.VISIBLE);
2787 }
2788 final RadioButton radioButton =
2789 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2790 radioButton.setChecked(position == mCheckedItem);
2791 return view;
2792 }
2793 }
2794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002796 synchronized (mMethodMap) {
2797 hideInputMethodMenuLocked();
2798 }
2799 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002800
The Android Open Source Project10592532009-03-18 17:39:46 -07002801 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002802 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803
The Android Open Source Project10592532009-03-18 17:39:46 -07002804 if (mSwitchingDialog != null) {
2805 mSwitchingDialog.dismiss();
2806 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002808
The Android Open Source Project10592532009-03-18 17:39:46 -07002809 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002810 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002814
satok42c5a162011-05-26 16:46:14 +09002815 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002817 // TODO: Make this work even for non-current users?
2818 if (!calledFromValidUser()) {
2819 return false;
2820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 synchronized (mMethodMap) {
2822 if (mContext.checkCallingOrSelfPermission(
2823 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2824 != PackageManager.PERMISSION_GRANTED) {
2825 throw new SecurityException(
2826 "Requires permission "
2827 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2828 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 long ident = Binder.clearCallingIdentity();
2831 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002832 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 } finally {
2834 Binder.restoreCallingIdentity(ident);
2835 }
2836 }
2837 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002838
2839 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2840 // Make sure this is a valid input method.
2841 InputMethodInfo imm = mMethodMap.get(id);
2842 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002843 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002844 }
2845
satokd87c2592010-09-29 11:52:06 +09002846 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2847 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848
satokd87c2592010-09-29 11:52:06 +09002849 if (enabled) {
2850 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2851 if (pair.first.equals(id)) {
2852 // We are enabling this input method, but it is already enabled.
2853 // Nothing to do. The previous state was enabled.
2854 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002855 }
2856 }
satokd87c2592010-09-29 11:52:06 +09002857 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2858 // Previous state was disabled.
2859 return false;
2860 } else {
2861 StringBuilder builder = new StringBuilder();
2862 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2863 builder, enabledInputMethodsList, id)) {
2864 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002865 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002866 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2867 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2868 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002869 }
2870 // Previous state was enabled.
2871 return true;
2872 } else {
2873 // We are disabling the input method but it is already disabled.
2874 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002875 return false;
2876 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002877 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002878 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002879
satok57ffc002011-01-25 00:11:47 +09002880 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2881 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002882 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002883 }
2884
satok723a27e2010-11-11 14:58:11 +09002885 private void saveCurrentInputMethodAndSubtypeToHistory() {
2886 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2887 if (mCurrentSubtype != null) {
2888 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2889 }
satok57ffc002011-01-25 00:11:47 +09002890 if (canAddToLastInputMethod(mCurrentSubtype)) {
2891 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2892 }
satokab751aa2010-09-14 19:17:36 +09002893 }
2894
satok723a27e2010-11-11 14:58:11 +09002895 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2896 boolean setSubtypeOnly) {
2897 // Update the history of InputMethod and Subtype
2898 saveCurrentInputMethodAndSubtypeToHistory();
2899
2900 // Set Subtype here
2901 if (imi == null || subtypeId < 0) {
2902 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002903 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002904 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002905 if (subtypeId < imi.getSubtypeCount()) {
2906 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2907 mSettings.putSelectedSubtype(subtype.hashCode());
2908 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002909 } else {
2910 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002911 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002912 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002913 }
satokab751aa2010-09-14 19:17:36 +09002914 }
satok723a27e2010-11-11 14:58:11 +09002915
satok4c0e7152012-06-20 20:08:44 +09002916 // Workaround.
2917 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2918 // IMEs are not recognized and considered uninstalled.
2919 // Actually, we can't move everything after SystemReady because
2920 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2921 // the default IME here and try cheking the default IME again in systemReady().
2922 // TODO: Do nothing before system ready and implement a separated logic for
2923 // the encryption lock screen.
2924 // TODO: ASEC should be ready before IMMS is instantiated.
2925 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002926 // Set InputMethod here
2927 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2928 }
2929 }
2930
2931 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2932 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2933 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2934 // newDefaultIme is empty when there is no candidate for the selected IME.
2935 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2936 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2937 if (subtypeHashCode != null) {
2938 try {
2939 lastSubtypeId = getSubtypeIdFromHashCode(
2940 imi, Integer.valueOf(subtypeHashCode));
2941 } catch (NumberFormatException e) {
2942 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2943 }
2944 }
2945 }
2946 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002947 }
2948
2949 private int getSelectedInputMethodSubtypeId(String id) {
2950 InputMethodInfo imi = mMethodMap.get(id);
2951 if (imi == null) {
2952 return NOT_A_SUBTYPE_ID;
2953 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002954 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2955 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002956 }
2957
satokfdf419e2012-05-08 16:52:08 +09002958 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2959 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2960 }
2961
2962 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002963 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002964 final int subtypeCount = imi.getSubtypeCount();
2965 for (int i = 0; i < subtypeCount; ++i) {
2966 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002967 if (subtypeHashCode == ims.hashCode()) {
2968 return i;
2969 }
satokab751aa2010-09-14 19:17:36 +09002970 }
2971 }
2972 return NOT_A_SUBTYPE_ID;
2973 }
2974
satoka86f5e42011-09-02 17:12:42 +09002975 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2976 Resources res, InputMethodInfo imi) {
2977 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002978 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002979 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002980 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002981 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002982 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002983 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002984 // scan overriding implicitly enabled subtypes.
2985 InputMethodSubtype subtype = subtypes.get(i);
2986 if (subtype.overridesImplicitlyEnabledSubtype()) {
2987 final String mode = subtype.getMode();
2988 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
2989 applicableModeAndSubtypesMap.put(mode, subtype);
2990 }
2991 }
2992 }
2993 if (applicableModeAndSubtypesMap.size() > 0) {
2994 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
2995 }
2996 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09002997 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09002998 final String locale = subtype.getLocale();
2999 final String mode = subtype.getMode();
3000 // When system locale starts with subtype's locale, that subtype will be applicable
3001 // for system locale
3002 // For instance, it's clearly applicable for cases like system locale = en_US and
3003 // subtype = en, but it is not necessarily considered applicable for cases like system
3004 // locale = en and subtype = en_US.
3005 // We just call systemLocale.startsWith(locale) in this function because there is no
3006 // need to find applicable subtypes aggressively unlike
3007 // findLastResortApplicableSubtypeLocked.
3008 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003009 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003010 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003011 if (applicableSubtype != null) {
3012 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3013 if (!systemLocale.equals(locale)) continue;
3014 }
satok3da92232011-01-11 22:46:30 +09003015 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003016 }
3017 }
3018 final InputMethodSubtype keyboardSubtype
3019 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3020 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3021 applicableModeAndSubtypesMap.values());
3022 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3023 for (int i = 0; i < N; ++i) {
3024 final InputMethodSubtype subtype = subtypes.get(i);
3025 final String mode = subtype.getMode();
3026 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3027 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3028 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003029 }
3030 }
3031 }
satokc3690562012-01-10 20:14:43 +09003032 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003033 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003034 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003035 if (lastResortKeyboardSubtype != null) {
3036 applicableSubtypes.add(lastResortKeyboardSubtype);
3037 }
3038 }
3039 return applicableSubtypes;
3040 }
3041
satok4e4569d2010-11-19 18:45:53 +09003042 /**
3043 * If there are no selected subtypes, tries finding the most applicable one according to the
3044 * given locale.
3045 * @param subtypes this function will search the most applicable subtype in subtypes
3046 * @param mode subtypes will be filtered by mode
3047 * @param locale subtypes will be filtered by locale
satok7599a7fb2010-12-22 13:45:23 +09003048 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3049 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003050 * @return the most applicable subtypeId
3051 */
satokdf31ae62011-01-15 06:19:44 +09003052 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3053 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7fb2010-12-22 13:45:23 +09003054 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003055 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003056 return null;
satok8fbb1e82010-11-02 23:15:58 +09003057 }
satok4e4569d2010-11-19 18:45:53 +09003058 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003059 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003060 }
satok8fbb1e82010-11-02 23:15:58 +09003061 final String language = locale.substring(0, 2);
3062 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003063 InputMethodSubtype applicableSubtype = null;
satok7599a7fb2010-12-22 13:45:23 +09003064 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003065 final int N = subtypes.size();
3066 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003067 InputMethodSubtype subtype = subtypes.get(i);
3068 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003069 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3070 // and all subtypes with all modes can be candidates.
3071 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7fb2010-12-22 13:45:23 +09003072 if (firstMatchedModeSubtype == null) {
3073 firstMatchedModeSubtype = subtype;
3074 }
satok9ef02832010-11-04 21:17:48 +09003075 if (locale.equals(subtypeLocale)) {
3076 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003077 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003078 break;
3079 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3080 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003081 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003082 partialMatchFound = true;
3083 }
satok8fbb1e82010-11-02 23:15:58 +09003084 }
3085 }
3086
satok7599a7fb2010-12-22 13:45:23 +09003087 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3088 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003089 }
3090
satok8fbb1e82010-11-02 23:15:58 +09003091 // The first subtype applicable to the system locale will be defined as the most applicable
3092 // subtype.
3093 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003094 if (applicableSubtype != null) {
3095 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3096 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3097 }
satok8fbb1e82010-11-02 23:15:58 +09003098 }
satokcd7cd292010-11-20 15:46:23 +09003099 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003100 }
3101
satok4e4569d2010-11-19 18:45:53 +09003102 // If there are no selected shortcuts, tries finding the most applicable ones.
3103 private Pair<InputMethodInfo, InputMethodSubtype>
3104 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3105 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3106 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003107 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003108 boolean foundInSystemIME = false;
3109
3110 // Search applicable subtype for each InputMethodInfo
3111 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09003112 final String imiId = imi.getId();
3113 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3114 continue;
3115 }
satokcd7cd292010-11-20 15:46:23 +09003116 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003117 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003118 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003119 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003120 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003121 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003122 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003123 }
satokdf31ae62011-01-15 06:19:44 +09003124 // 2. Search by the system locale from enabledSubtypes.
3125 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003126 if (subtype == null) {
3127 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003128 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003129 }
satoka86f5e42011-09-02 17:12:42 +09003130 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3131 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3132 final ArrayList<InputMethodSubtype> subtypesForSearch =
3133 overridingImplicitlyEnabledSubtypes.isEmpty()
3134 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09003135 // 4. Search by the current subtype's locale from all subtypes.
3136 if (subtype == null && mCurrentSubtype != null) {
3137 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003138 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09003139 }
3140 // 5. Search by the system locale from all subtypes.
3141 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003142 if (subtype == null) {
satok7599a7fb2010-12-22 13:45:23 +09003143 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003144 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003145 }
satokcd7cd292010-11-20 15:46:23 +09003146 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09003147 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003148 // The current input method is the most applicable IME.
3149 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003150 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003151 break;
satok7599a7fb2010-12-22 13:45:23 +09003152 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003153 // The system input method is 2nd applicable IME.
3154 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003155 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09003156 if ((imi.getServiceInfo().applicationInfo.flags
3157 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3158 foundInSystemIME = true;
3159 }
satok4e4569d2010-11-19 18:45:53 +09003160 }
3161 }
3162 }
3163 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003164 if (mostApplicableIMI != null) {
3165 Slog.w(TAG, "Most applicable shortcut input method was:"
3166 + mostApplicableIMI.getId());
3167 if (mostApplicableSubtype != null) {
3168 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3169 + "," + mostApplicableSubtype.getMode() + ","
3170 + mostApplicableSubtype.getLocale());
3171 }
3172 }
satok4e4569d2010-11-19 18:45:53 +09003173 }
satokcd7cd292010-11-20 15:46:23 +09003174 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003175 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003176 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003177 } else {
3178 return null;
3179 }
3180 }
3181
satokab751aa2010-09-14 19:17:36 +09003182 /**
3183 * @return Return the current subtype of this input method.
3184 */
satok42c5a162011-05-26 16:46:14 +09003185 @Override
satokab751aa2010-09-14 19:17:36 +09003186 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003187 // TODO: Make this work even for non-current users?
3188 if (!calledFromValidUser()) {
3189 return null;
3190 }
3191 synchronized (mMethodMap) {
3192 return getCurrentInputMethodSubtypeLocked();
3193 }
3194 }
3195
3196 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003197 if (mCurMethodId == null) {
3198 return null;
3199 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003200 final boolean subtypeIsSelected =
3201 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3202 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3203 if (imi == null || imi.getSubtypeCount() == 0) {
3204 return null;
satok4e4569d2010-11-19 18:45:53 +09003205 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003206 if (!subtypeIsSelected || mCurrentSubtype == null
3207 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3208 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3209 if (subtypeId == NOT_A_SUBTYPE_ID) {
3210 // If there are no selected subtypes, the framework will try to find
3211 // the most applicable subtype from explicitly or implicitly enabled
3212 // subtypes.
3213 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3214 getEnabledInputMethodSubtypeListLocked(imi, true);
3215 // If there is only one explicitly or implicitly enabled subtype,
3216 // just returns it.
3217 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3218 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3219 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3220 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3221 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3222 SUBTYPE_MODE_KEYBOARD, null, true);
3223 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003224 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003225 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3226 true);
satok4e4569d2010-11-19 18:45:53 +09003227 }
satok3ef8b292010-11-23 06:06:29 +09003228 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003229 } else {
3230 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003231 }
3232 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003233 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003234 }
3235
satokf3db1af2010-11-23 13:34:33 +09003236 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3237 InputMethodSubtype subtype) {
3238 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3239 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3240 } else {
3241 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3242 subtypes.add(subtype);
3243 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3244 }
3245 }
3246
satok4e4569d2010-11-19 18:45:53 +09003247 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003248 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003249 @Override
satok4e4569d2010-11-19 18:45:53 +09003250 public List getShortcutInputMethodsAndSubtypes() {
3251 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003252 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003253 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003254 // If there are no selected shortcut subtypes, the framework will try to find
3255 // the most applicable subtype from all subtypes whose mode is
3256 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003257 Pair<InputMethodInfo, InputMethodSubtype> info =
3258 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3259 SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003260 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003261 ret.add(info.first);
3262 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003263 }
satok3da92232011-01-11 22:46:30 +09003264 return ret;
satokf3db1af2010-11-23 13:34:33 +09003265 }
satokf3db1af2010-11-23 13:34:33 +09003266 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3267 ret.add(imi);
3268 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3269 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003270 }
3271 }
satokf3db1af2010-11-23 13:34:33 +09003272 return ret;
satok4e4569d2010-11-19 18:45:53 +09003273 }
3274 }
3275
satok42c5a162011-05-26 16:46:14 +09003276 @Override
satokb66d2872010-11-10 01:04:04 +09003277 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003278 // TODO: Make this work even for non-current users?
3279 if (!calledFromValidUser()) {
3280 return false;
3281 }
satokb66d2872010-11-10 01:04:04 +09003282 synchronized (mMethodMap) {
3283 if (subtype != null && mCurMethodId != null) {
3284 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3285 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3286 if (subtypeId != NOT_A_SUBTYPE_ID) {
3287 setInputMethodLocked(mCurMethodId, subtypeId);
3288 return true;
3289 }
3290 }
3291 return false;
3292 }
3293 }
3294
satok688bd472012-02-09 20:09:17 +09003295 private static class InputMethodAndSubtypeListManager {
3296 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003297 // Used to load label
satok688bd472012-02-09 20:09:17 +09003298 private final PackageManager mPm;
3299 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003300 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003301 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3302 mContext = context;
3303 mPm = context.getPackageManager();
3304 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003305 final Locale locale = context.getResources().getConfiguration().locale;
3306 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003307 }
3308
3309 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3310 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3311 new Comparator<InputMethodInfo>() {
3312 @Override
3313 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3314 if (imi2 == null) return 0;
3315 if (imi1 == null) return 1;
3316 if (mPm == null) {
3317 return imi1.getId().compareTo(imi2.getId());
3318 }
3319 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3320 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3321 return imiId1.toString().compareTo(imiId2.toString());
3322 }
3323 });
3324
3325 public ImeSubtypeListItem getNextInputMethod(
3326 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3327 if (imi == null) {
3328 return null;
3329 }
3330 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3331 if (imList.size() <= 1) {
3332 return null;
3333 }
3334 final int N = imList.size();
3335 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003336 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003337 : NOT_A_SUBTYPE_ID;
3338 for (int i = 0; i < N; ++i) {
3339 final ImeSubtypeListItem isli = imList.get(i);
3340 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3341 if (!onlyCurrentIme) {
3342 return imList.get((i + 1) % N);
3343 }
3344 for (int j = 0; j < N - 1; ++j) {
3345 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3346 if (candidate.mImi.equals(imi)) {
3347 return candidate;
3348 }
3349 }
3350 return null;
3351 }
3352 }
3353 return null;
3354 }
3355
3356 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3357 return getSortedInputMethodAndSubtypeList(true, false, false);
3358 }
3359
3360 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3361 boolean inputShown, boolean isScreenLocked) {
3362 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3363 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3364 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3365 if (immis == null || immis.size() == 0) {
3366 return Collections.emptyList();
3367 }
3368 mSortedImmis.clear();
3369 mSortedImmis.putAll(immis);
3370 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3371 if (imi == null) continue;
3372 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3373 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3374 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3375 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3376 }
3377 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3378 final CharSequence imeLabel = imi.loadLabel(mPm);
3379 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3380 final int subtypeCount = imi.getSubtypeCount();
3381 if (DEBUG) {
3382 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3383 }
3384 for (int j = 0; j < subtypeCount; ++j) {
3385 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3386 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3387 // We show all enabled IMEs and subtypes when an IME is shown.
3388 if (enabledSubtypeSet.contains(subtypeHashCode)
3389 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3390 final CharSequence subtypeLabel =
3391 subtype.overridesImplicitlyEnabledSubtype() ? null
3392 : subtype.getDisplayName(mContext, imi.getPackageName(),
3393 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003394 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3395 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003396
3397 // Removing this subtype from enabledSubtypeSet because we no longer
3398 // need to add an entry of this subtype to imList to avoid duplicated
3399 // entries.
3400 enabledSubtypeSet.remove(subtypeHashCode);
3401 }
3402 }
3403 } else {
satok93d744d2012-05-09 17:14:08 +09003404 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3405 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003406 }
3407 }
satok93d744d2012-05-09 17:14:08 +09003408 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003409 return imList;
3410 }
3411 }
3412
satokd87c2592010-09-29 11:52:06 +09003413 /**
3414 * Utility class for putting and getting settings for InputMethod
3415 * TODO: Move all putters and getters of settings to this class.
3416 */
3417 private static class InputMethodSettings {
3418 // The string for enabled input method is saved as follows:
3419 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3420 private static final char INPUT_METHOD_SEPARATER = ':';
3421 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003422 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003423 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3424
satok723a27e2010-11-11 14:58:11 +09003425 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003426 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3427
satokdf31ae62011-01-15 06:19:44 +09003428 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003429 private final ContentResolver mResolver;
3430 private final HashMap<String, InputMethodInfo> mMethodMap;
3431 private final ArrayList<InputMethodInfo> mMethodList;
3432
3433 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003434 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003435
3436 private static void buildEnabledInputMethodsSettingString(
3437 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3438 String id = pair.first;
3439 ArrayList<String> subtypes = pair.second;
3440 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003441 // Inputmethod and subtypes are saved in the settings as follows:
3442 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3443 for (String subtypeId: subtypes) {
3444 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003445 }
3446 }
3447
3448 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003449 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3451 int userId) {
3452 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003453 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003454 mResolver = resolver;
3455 mMethodMap = methodMap;
3456 mMethodList = methodList;
3457 }
3458
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003459 public void setCurrentUserId(int userId) {
3460 if (DEBUG) {
3461 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3462 + userId + ", new ime = " + getSelectedInputMethod());
3463 }
3464 // IMMS settings are kept per user, so keep track of current user
3465 mCurrentUserId = userId;
3466 }
3467
satokd87c2592010-09-29 11:52:06 +09003468 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3469 return createEnabledInputMethodListLocked(
3470 getEnabledInputMethodsAndSubtypeListLocked());
3471 }
3472
satok7f35c8c2010-10-07 21:13:11 +09003473 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003474 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3475 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003476 getEnabledInputMethodsAndSubtypeListLocked());
3477 }
3478
satok67ddf9c2010-11-17 09:45:54 +09003479 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3480 InputMethodInfo imi) {
3481 List<Pair<String, ArrayList<String>>> imsList =
3482 getEnabledInputMethodsAndSubtypeListLocked();
3483 ArrayList<InputMethodSubtype> enabledSubtypes =
3484 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003485 if (imi != null) {
3486 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3487 InputMethodInfo info = mMethodMap.get(imsPair.first);
3488 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003489 final int subtypeCount = info.getSubtypeCount();
3490 for (int i = 0; i < subtypeCount; ++i) {
3491 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003492 for (String s: imsPair.second) {
3493 if (String.valueOf(ims.hashCode()).equals(s)) {
3494 enabledSubtypes.add(ims);
3495 }
satok67ddf9c2010-11-17 09:45:54 +09003496 }
3497 }
satok884ef9a2010-11-18 10:39:46 +09003498 break;
satok67ddf9c2010-11-17 09:45:54 +09003499 }
satok67ddf9c2010-11-17 09:45:54 +09003500 }
3501 }
3502 return enabledSubtypes;
3503 }
3504
satokd87c2592010-09-29 11:52:06 +09003505 // At the initial boot, the settings for input methods are not set,
3506 // so we need to enable IME in that case.
3507 public void enableAllIMEsIfThereIsNoEnabledIME() {
3508 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3509 StringBuilder sb = new StringBuilder();
3510 final int N = mMethodList.size();
3511 for (int i = 0; i < N; i++) {
3512 InputMethodInfo imi = mMethodList.get(i);
3513 Slog.i(TAG, "Adding: " + imi.getId());
3514 if (i > 0) sb.append(':');
3515 sb.append(imi.getId());
3516 }
3517 putEnabledInputMethodsStr(sb.toString());
3518 }
3519 }
3520
satokbb4aa062011-01-19 21:40:27 +09003521 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003522 ArrayList<Pair<String, ArrayList<String>>> imsList
3523 = new ArrayList<Pair<String, ArrayList<String>>>();
3524 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3525 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3526 return imsList;
3527 }
satok723a27e2010-11-11 14:58:11 +09003528 mInputMethodSplitter.setString(enabledInputMethodsStr);
3529 while (mInputMethodSplitter.hasNext()) {
3530 String nextImsStr = mInputMethodSplitter.next();
3531 mSubtypeSplitter.setString(nextImsStr);
3532 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003533 ArrayList<String> subtypeHashes = new ArrayList<String>();
3534 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003535 String imeId = mSubtypeSplitter.next();
3536 while (mSubtypeSplitter.hasNext()) {
3537 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003538 }
3539 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3540 }
3541 }
3542 return imsList;
3543 }
3544
3545 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3546 if (reloadInputMethodStr) {
3547 getEnabledInputMethodsStr();
3548 }
3549 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3550 // Add in the newly enabled input method.
3551 putEnabledInputMethodsStr(id);
3552 } else {
3553 putEnabledInputMethodsStr(
3554 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3555 }
3556 }
3557
3558 /**
3559 * Build and put a string of EnabledInputMethods with removing specified Id.
3560 * @return the specified id was removed or not.
3561 */
3562 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3563 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3564 boolean isRemoved = false;
3565 boolean needsAppendSeparator = false;
3566 for (Pair<String, ArrayList<String>> ims: imsList) {
3567 String curId = ims.first;
3568 if (curId.equals(id)) {
3569 // We are disabling this input method, and it is
3570 // currently enabled. Skip it to remove from the
3571 // new list.
3572 isRemoved = true;
3573 } else {
3574 if (needsAppendSeparator) {
3575 builder.append(INPUT_METHOD_SEPARATER);
3576 } else {
3577 needsAppendSeparator = true;
3578 }
3579 buildEnabledInputMethodsSettingString(builder, ims);
3580 }
3581 }
3582 if (isRemoved) {
3583 // Update the setting with the new list of input methods.
3584 putEnabledInputMethodsStr(builder.toString());
3585 }
3586 return isRemoved;
3587 }
3588
3589 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3590 List<Pair<String, ArrayList<String>>> imsList) {
3591 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3592 for (Pair<String, ArrayList<String>> ims: imsList) {
3593 InputMethodInfo info = mMethodMap.get(ims.first);
3594 if (info != null) {
3595 res.add(info);
3596 }
3597 }
3598 return res;
3599 }
3600
satok7f35c8c2010-10-07 21:13:11 +09003601 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003602 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003603 List<Pair<String, ArrayList<String>>> imsList) {
3604 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3605 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3606 for (Pair<String, ArrayList<String>> ims : imsList) {
3607 InputMethodInfo info = mMethodMap.get(ims.first);
3608 if (info != null) {
3609 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3610 }
3611 }
3612 return res;
3613 }
3614
satokd87c2592010-09-29 11:52:06 +09003615 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003616 Settings.Secure.putStringForUser(
3617 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003618 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003619 if (DEBUG) {
3620 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3621 }
satokd87c2592010-09-29 11:52:06 +09003622 }
3623
3624 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003625 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3626 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003627 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003628 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3629 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003630 }
satokd87c2592010-09-29 11:52:06 +09003631 return mEnabledInputMethodsStrCache;
3632 }
satok723a27e2010-11-11 14:58:11 +09003633
3634 private void saveSubtypeHistory(
3635 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3636 StringBuilder builder = new StringBuilder();
3637 boolean isImeAdded = false;
3638 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3639 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3640 newSubtypeId);
3641 isImeAdded = true;
3642 }
3643 for (Pair<String, String> ime: savedImes) {
3644 String imeId = ime.first;
3645 String subtypeId = ime.second;
3646 if (TextUtils.isEmpty(subtypeId)) {
3647 subtypeId = NOT_A_SUBTYPE_ID_STR;
3648 }
3649 if (isImeAdded) {
3650 builder.append(INPUT_METHOD_SEPARATER);
3651 } else {
3652 isImeAdded = true;
3653 }
3654 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3655 subtypeId);
3656 }
3657 // Remove the last INPUT_METHOD_SEPARATER
3658 putSubtypeHistoryStr(builder.toString());
3659 }
3660
3661 public void addSubtypeToHistory(String imeId, String subtypeId) {
3662 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3663 for (Pair<String, String> ime: subtypeHistory) {
3664 if (ime.first.equals(imeId)) {
3665 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003666 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003667 + ime.second);
3668 }
3669 // We should break here
3670 subtypeHistory.remove(ime);
3671 break;
3672 }
3673 }
satokbb4aa062011-01-19 21:40:27 +09003674 if (DEBUG) {
3675 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3676 }
satok723a27e2010-11-11 14:58:11 +09003677 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3678 }
3679
3680 private void putSubtypeHistoryStr(String str) {
3681 if (DEBUG) {
3682 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3683 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003684 Settings.Secure.putStringForUser(
3685 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003686 }
3687
3688 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3689 // Gets the first one from the history
3690 return getLastSubtypeForInputMethodLockedInternal(null);
3691 }
3692
3693 public String getLastSubtypeForInputMethodLocked(String imeId) {
3694 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3695 if (ime != null) {
3696 return ime.second;
3697 } else {
3698 return null;
3699 }
3700 }
3701
3702 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3703 List<Pair<String, ArrayList<String>>> enabledImes =
3704 getEnabledInputMethodsAndSubtypeListLocked();
3705 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003706 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003707 final String imeInTheHistory = imeAndSubtype.first;
3708 // If imeId is empty, returns the first IME and subtype in the history
3709 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3710 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003711 final String subtypeHashCode =
3712 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3713 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003714 if (!TextUtils.isEmpty(subtypeHashCode)) {
3715 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003716 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003717 }
3718 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3719 }
3720 }
3721 }
3722 if (DEBUG) {
3723 Slog.d(TAG, "No enabled IME found in the history");
3724 }
3725 return null;
3726 }
3727
satokdf31ae62011-01-15 06:19:44 +09003728 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003729 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3730 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3731 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003732 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003733 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003734 if (explicitlyEnabledSubtypes.size() == 0) {
3735 // If there are no explicitly enabled subtypes, applicable subtypes are
3736 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003737 // If IME is enabled and no subtypes are enabled, applicable subtypes
3738 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003739 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003740 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003741 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003742 if (implicitlySelectedSubtypes != null) {
3743 final int N = implicitlySelectedSubtypes.size();
3744 for (int i = 0; i < N; ++i) {
3745 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3746 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3747 return subtypeHashCode;
3748 }
3749 }
3750 }
3751 }
3752 } else {
satokf6cafb62011-01-17 16:29:02 +09003753 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003754 if (s.equals(subtypeHashCode)) {
3755 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003756 try {
3757 final int hashCode = Integer.valueOf(subtypeHashCode);
3758 // Check whether the subtype id is valid or not
3759 if (isValidSubtypeId(imi, hashCode)) {
3760 return s;
3761 } else {
3762 return NOT_A_SUBTYPE_ID_STR;
3763 }
3764 } catch (NumberFormatException e) {
3765 return NOT_A_SUBTYPE_ID_STR;
3766 }
satokdf31ae62011-01-15 06:19:44 +09003767 }
satok723a27e2010-11-11 14:58:11 +09003768 }
3769 }
3770 // If imeId was enabled but subtypeId was disabled.
3771 return NOT_A_SUBTYPE_ID_STR;
3772 }
3773 }
3774 // If both imeId and subtypeId are disabled, return null
3775 return null;
3776 }
3777
3778 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3779 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3780 final String subtypeHistoryStr = getSubtypeHistoryStr();
3781 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3782 return imsList;
3783 }
3784 mInputMethodSplitter.setString(subtypeHistoryStr);
3785 while (mInputMethodSplitter.hasNext()) {
3786 String nextImsStr = mInputMethodSplitter.next();
3787 mSubtypeSplitter.setString(nextImsStr);
3788 if (mSubtypeSplitter.hasNext()) {
3789 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3790 // The first element is ime id.
3791 String imeId = mSubtypeSplitter.next();
3792 while (mSubtypeSplitter.hasNext()) {
3793 subtypeId = mSubtypeSplitter.next();
3794 break;
3795 }
3796 imsList.add(new Pair<String, String>(imeId, subtypeId));
3797 }
3798 }
3799 return imsList;
3800 }
3801
3802 private String getSubtypeHistoryStr() {
3803 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003804 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3805 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003806 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003807 return Settings.Secure.getStringForUser(
3808 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003809 }
3810
3811 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003812 if (DEBUG) {
3813 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3814 + mCurrentUserId);
3815 }
3816 Settings.Secure.putStringForUser(
3817 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003818 }
3819
3820 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003821 if (DEBUG) {
3822 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3823 + mCurrentUserId);
3824 }
3825 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3826 subtypeId, mCurrentUserId);
3827 }
3828
3829 public String getDisabledSystemInputMethods() {
3830 return Settings.Secure.getStringForUser(
3831 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3832 }
3833
3834 public String getSelectedInputMethod() {
3835 if (DEBUG) {
3836 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3837 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3838 + ", " + mCurrentUserId);
3839 }
3840 return Settings.Secure.getStringForUser(
3841 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3842 }
3843
3844 public int getSelectedInputMethodSubtypeHashCode() {
3845 try {
3846 return Settings.Secure.getIntForUser(
3847 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3848 } catch (SettingNotFoundException e) {
3849 return NOT_A_SUBTYPE_ID;
3850 }
3851 }
3852
3853 public int getCurrentUserId() {
3854 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003855 }
satokd87c2592010-09-29 11:52:06 +09003856 }
3857
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003858 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003859 private static class InputMethodFileManager {
3860 private static final String SYSTEM_PATH = "system";
3861 private static final String INPUT_METHOD_PATH = "inputmethod";
3862 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3863 private static final String NODE_SUBTYPES = "subtypes";
3864 private static final String NODE_SUBTYPE = "subtype";
3865 private static final String NODE_IMI = "imi";
3866 private static final String ATTR_ID = "id";
3867 private static final String ATTR_LABEL = "label";
3868 private static final String ATTR_ICON = "icon";
3869 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3870 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3871 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3872 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3873 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3874 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003875 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003876 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003877 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003878 if (methodMap == null) {
3879 throw new NullPointerException("methodMap is null");
3880 }
3881 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003882 final File systemDir = userId == UserHandle.USER_OWNER
3883 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3884 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003885 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3886 if (!inputMethodDir.mkdirs()) {
3887 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3888 }
3889 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3890 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3891 if (!subtypeFile.exists()) {
3892 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003893 writeAdditionalInputMethodSubtypes(
3894 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003895 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003896 readAdditionalInputMethodSubtypes(
3897 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003898 }
3899 }
3900
3901 private void deleteAllInputMethodSubtypes(String imiId) {
3902 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003903 mAdditionalSubtypesMap.remove(imiId);
3904 writeAdditionalInputMethodSubtypes(
3905 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003906 }
3907 }
3908
3909 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003910 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003911 synchronized (mMethodMap) {
3912 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3913 final int N = additionalSubtypes.length;
3914 for (int i = 0; i < N; ++i) {
3915 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003916 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003917 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003918 } else {
3919 Slog.w(TAG, "Duplicated subtype definition found: "
3920 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003921 }
3922 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003923 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3924 writeAdditionalInputMethodSubtypes(
3925 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003926 }
3927 }
3928
3929 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3930 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003931 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003932 }
3933 }
3934
3935 private static void writeAdditionalInputMethodSubtypes(
3936 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3937 HashMap<String, InputMethodInfo> methodMap) {
3938 // Safety net for the case that this function is called before methodMap is set.
3939 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3940 FileOutputStream fos = null;
3941 try {
3942 fos = subtypesFile.startWrite();
3943 final XmlSerializer out = new FastXmlSerializer();
3944 out.setOutput(fos, "utf-8");
3945 out.startDocument(null, true);
3946 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3947 out.startTag(null, NODE_SUBTYPES);
3948 for (String imiId : allSubtypes.keySet()) {
3949 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3950 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3951 continue;
3952 }
3953 out.startTag(null, NODE_IMI);
3954 out.attribute(null, ATTR_ID, imiId);
3955 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3956 final int N = subtypesList.size();
3957 for (int i = 0; i < N; ++i) {
3958 final InputMethodSubtype subtype = subtypesList.get(i);
3959 out.startTag(null, NODE_SUBTYPE);
3960 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3961 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3962 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3963 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3964 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3965 out.attribute(null, ATTR_IS_AUXILIARY,
3966 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3967 out.endTag(null, NODE_SUBTYPE);
3968 }
3969 out.endTag(null, NODE_IMI);
3970 }
3971 out.endTag(null, NODE_SUBTYPES);
3972 out.endDocument();
3973 subtypesFile.finishWrite(fos);
3974 } catch (java.io.IOException e) {
3975 Slog.w(TAG, "Error writing subtypes", e);
3976 if (fos != null) {
3977 subtypesFile.failWrite(fos);
3978 }
3979 }
3980 }
3981
3982 private static void readAdditionalInputMethodSubtypes(
3983 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3984 if (allSubtypes == null || subtypesFile == null) return;
3985 allSubtypes.clear();
3986 FileInputStream fis = null;
3987 try {
3988 fis = subtypesFile.openRead();
3989 final XmlPullParser parser = Xml.newPullParser();
3990 parser.setInput(fis, null);
3991 int type = parser.getEventType();
3992 // Skip parsing until START_TAG
3993 while ((type = parser.next()) != XmlPullParser.START_TAG
3994 && type != XmlPullParser.END_DOCUMENT) {}
3995 String firstNodeName = parser.getName();
3996 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3997 throw new XmlPullParserException("Xml doesn't start with subtypes");
3998 }
3999 final int depth =parser.getDepth();
4000 String currentImiId = null;
4001 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4002 while (((type = parser.next()) != XmlPullParser.END_TAG
4003 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4004 if (type != XmlPullParser.START_TAG)
4005 continue;
4006 final String nodeName = parser.getName();
4007 if (NODE_IMI.equals(nodeName)) {
4008 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4009 if (TextUtils.isEmpty(currentImiId)) {
4010 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4011 continue;
4012 }
4013 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4014 allSubtypes.put(currentImiId, tempSubtypesArray);
4015 } else if (NODE_SUBTYPE.equals(nodeName)) {
4016 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4017 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4018 continue;
4019 }
4020 final int icon = Integer.valueOf(
4021 parser.getAttributeValue(null, ATTR_ICON));
4022 final int label = Integer.valueOf(
4023 parser.getAttributeValue(null, ATTR_LABEL));
4024 final String imeSubtypeLocale =
4025 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4026 final String imeSubtypeMode =
4027 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4028 final String imeSubtypeExtraValue =
4029 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004030 final boolean isAuxiliary = "1".equals(String.valueOf(
4031 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004032 final InputMethodSubtype subtype =
4033 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4034 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4035 tempSubtypesArray.add(subtype);
4036 }
4037 }
4038 } catch (XmlPullParserException e) {
4039 Slog.w(TAG, "Error reading subtypes: " + e);
4040 return;
4041 } catch (java.io.IOException e) {
4042 Slog.w(TAG, "Error reading subtypes: " + e);
4043 return;
4044 } catch (NumberFormatException e) {
4045 Slog.w(TAG, "Error reading subtypes: " + e);
4046 return;
4047 } finally {
4048 if (fis != null) {
4049 try {
4050 fis.close();
4051 } catch (java.io.IOException e1) {
4052 Slog.w(TAG, "Failed to close.");
4053 }
4054 }
4055 }
4056 }
4057 }
4058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004060 // Utilities for debug
4061 private static String getStackTrace() {
4062 final StringBuilder sb = new StringBuilder();
4063 try {
4064 throw new RuntimeException();
4065 } catch (RuntimeException e) {
4066 final StackTraceElement[] frames = e.getStackTrace();
4067 // Start at 1 because the first frame is here and we don't care about it
4068 for (int j = 1; j < frames.length; ++j) {
4069 sb.append(frames[j].toString() + "\n");
4070 }
4071 }
4072 return sb.toString();
4073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 @Override
4076 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4077 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4078 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4081 + Binder.getCallingPid()
4082 + ", uid=" + Binder.getCallingUid());
4083 return;
4084 }
4085
4086 IInputMethod method;
4087 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 synchronized (mMethodMap) {
4092 p.println("Current Input Method Manager state:");
4093 int N = mMethodList.size();
4094 p.println(" Input Methods:");
4095 for (int i=0; i<N; i++) {
4096 InputMethodInfo info = mMethodList.get(i);
4097 p.println(" InputMethod #" + i + ":");
4098 info.dump(p, " ");
4099 }
4100 p.println(" Clients:");
4101 for (ClientState ci : mClients.values()) {
4102 p.println(" Client " + ci + ":");
4103 p.println(" client=" + ci.client);
4104 p.println(" inputContext=" + ci.inputContext);
4105 p.println(" sessionRequested=" + ci.sessionRequested);
4106 p.println(" curSession=" + ci.curSession);
4107 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004108 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004110 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4111 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4113 + " mBoundToMethod=" + mBoundToMethod);
4114 p.println(" mCurToken=" + mCurToken);
4115 p.println(" mCurIntent=" + mCurIntent);
4116 method = mCurMethod;
4117 p.println(" mCurMethod=" + mCurMethod);
4118 p.println(" mEnabledSession=" + mEnabledSession);
4119 p.println(" mShowRequested=" + mShowRequested
4120 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4121 + " mShowForced=" + mShowForced
4122 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004123 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004125
Jeff Brownb88102f2010-09-08 11:49:43 -07004126 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 pw.flush();
4129 try {
4130 client.client.asBinder().dump(fd, args);
4131 } catch (RemoteException e) {
4132 p.println("Input method client dead: " + e);
4133 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004134 } else {
4135 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004137
Jeff Brownb88102f2010-09-08 11:49:43 -07004138 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 pw.flush();
4141 try {
4142 method.asBinder().dump(fd, args);
4143 } catch (RemoteException e) {
4144 p.println("Input method service dead: " + e);
4145 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004146 } else {
4147 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 }
4149 }
4150}