blob: e4b3ce46241efafde9171e4a444b179f0f0daa3a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 * use this file except in compliance with the License. You may obtain a copy of
5 * the License at
Doug Zongkerab5c49c2009-12-04 10:31:43 -08006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007 * http://www.apache.org/licenses/LICENSE-2.0
Doug Zongkerab5c49c2009-12-04 10:31:43 -08008 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and limitations under
13 * the License.
14 */
15
16package com.android.server;
17
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080018import com.android.internal.content.PackageMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070020import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090021import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.internal.view.IInputContext;
23import com.android.internal.view.IInputMethod;
24import com.android.internal.view.IInputMethodCallback;
25import com.android.internal.view.IInputMethodClient;
26import com.android.internal.view.IInputMethodManager;
27import com.android.internal.view.IInputMethodSession;
28import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080029import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090030import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
satoke7c6998e2011-06-03 17:57:59 +090032import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090037import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090040import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090041import android.app.Notification;
42import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070043import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090044import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.content.ComponentName;
46import android.content.ContentResolver;
47import android.content.Context;
48import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.DialogInterface.OnCancelListener;
50import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090051import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070053import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090054import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.pm.PackageManager;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070056import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.ResolveInfo;
58import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070059import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.res.Resources;
61import android.content.res.TypedArray;
62import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080063import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090065import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
68import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090069import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090071import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Parcel;
73import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080074import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.ServiceManager;
76import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090077import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.provider.Settings;
Amith Yamasanie861ec12010-03-24 21:39:27 -070079import android.provider.Settings.Secure;
satokab751aa2010-09-14 19:17:36 +090080import android.provider.Settings.SettingNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090082import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070083import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090085import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090086import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.PrintWriterPrinter;
88import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090089import android.util.Slog;
90import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.view.IWindowManager;
Ken Wakasa05dbb652011-08-22 15:22:43 +090092import android.view.LayoutInflater;
93import android.view.View;
94import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.WindowManager;
satokab751aa2010-09-14 19:17:36 +090096import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.inputmethod.InputBinding;
98import android.view.inputmethod.InputMethod;
99import android.view.inputmethod.InputMethodInfo;
100import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900101import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900102import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900103import android.widget.CompoundButton;
104import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900105import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900106import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
satoke7c6998e2011-06-03 17:57:59 +0900109import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900111import java.io.FileInputStream;
112import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.io.IOException;
114import java.io.PrintWriter;
115import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900116import java.util.Collections;
Ken Wakasa761eb372011-03-04 19:06:18 +0900117import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.util.HashMap;
satok7f35c8c2010-10-07 21:13:11 +0900119import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900121import java.util.Locale;
satok913a8922010-08-26 21:53:41 +0900122import java.util.TreeMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124/**
125 * This class provides a system service that manages input methods.
126 */
127public class InputMethodManagerService extends IInputMethodManager.Stub
128 implements ServiceConnection, Handler.Callback {
129 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700130 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 static final int MSG_SHOW_IM_PICKER = 1;
satokab751aa2010-09-14 19:17:36 +0900133 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 2;
satok47a44912010-10-06 16:03:58 +0900134 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 3;
satok217f5482010-12-15 05:19:19 +0900135 static final int MSG_SHOW_IM_CONFIG = 4;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 static final int MSG_UNBIND_INPUT = 1000;
138 static final int MSG_BIND_INPUT = 1010;
139 static final int MSG_SHOW_SOFT_INPUT = 1020;
140 static final int MSG_HIDE_SOFT_INPUT = 1030;
141 static final int MSG_ATTACH_TOKEN = 1040;
142 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 static final int MSG_START_INPUT = 2000;
145 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final int MSG_UNBIND_METHOD = 3000;
148 static final int MSG_BIND_METHOD = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700149 static final int MSG_SET_ACTIVE = 3020;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800150
satok01038492012-04-09 21:08:27 +0900151 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final long TIME_TO_RECONNECT = 10*1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800154
satokf9f01002011-05-19 21:31:50 +0900155 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
156
satokab751aa2010-09-14 19:17:36 +0900157 private static final int NOT_A_SUBTYPE_ID = -1;
satok723a27e2010-11-11 14:58:11 +0900158 private static final String NOT_A_SUBTYPE_ID_STR = String.valueOf(NOT_A_SUBTYPE_ID);
satok4e4569d2010-11-19 18:45:53 +0900159 private static final String SUBTYPE_MODE_KEYBOARD = "keyboard";
160 private static final String SUBTYPE_MODE_VOICE = "voice";
satokb6359412011-06-28 17:47:41 +0900161 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
satokc3690562012-01-10 20:14:43 +0900162 private static final String TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE =
163 "EnabledWhenDefaultIsNotAsciiCapable";
164 private static final String TAG_ASCII_CAPABLE = "AsciiCapable";
satok5b927c432012-05-01 20:09:34 +0900165 private static final Locale ENGLISH_LOCALE = new Locale("en");
satok4e4569d2010-11-19 18:45:53 +0900166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800168 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900170 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 final IWindowManager mIWindowManager;
173 final HandlerCaller mCaller;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900174 private InputMethodFileManager mFileManager;
175 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900176 private final HardKeyboardListener mHardKeyboardListener;
177 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 final InputBindResult mNoBinding = new InputBindResult(null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // All known input methods. mMethodMap also serves as the global
182 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900183 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
184 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900185 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
186 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800187
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700188 // Used to bring IME service up to visible adjustment while it is being shown.
189 final ServiceConnection mVisibleConnection = new ServiceConnection() {
190 @Override public void onServiceConnected(ComponentName name, IBinder service) {
191 }
192
193 @Override public void onServiceDisconnected(ComponentName name) {
194 }
195 };
196 boolean mVisibleBound = false;
197
satok7cfc0ed2011-06-20 21:29:36 +0900198 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700199 private NotificationManager mNotificationManager;
200 private KeyguardManager mKeyguardManager;
201 private StatusBarManagerService mStatusBar;
202 private Notification mImeSwitcherNotification;
203 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900204 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900205 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900206 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 class SessionState {
209 final ClientState client;
210 final IInputMethod method;
211 final IInputMethodSession session;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 @Override
214 public String toString() {
215 return "SessionState{uid " + client.uid + " pid " + client.pid
216 + " method " + Integer.toHexString(
217 System.identityHashCode(method))
218 + " session " + Integer.toHexString(
219 System.identityHashCode(session))
220 + "}";
221 }
222
223 SessionState(ClientState _client, IInputMethod _method,
224 IInputMethodSession _session) {
225 client = _client;
226 method = _method;
227 session = _session;
228 }
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 class ClientState {
232 final IInputMethodClient client;
233 final IInputContext inputContext;
234 final int uid;
235 final int pid;
236 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 boolean sessionRequested;
239 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
242 public String toString() {
243 return "ClientState{" + Integer.toHexString(
244 System.identityHashCode(this)) + " uid " + uid
245 + " pid " + pid + "}";
246 }
247
248 ClientState(IInputMethodClient _client, IInputContext _inputContext,
249 int _uid, int _pid) {
250 client = _client;
251 inputContext = _inputContext;
252 uid = _uid;
253 pid = _pid;
254 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
255 }
256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final HashMap<IBinder, ClientState> mClients
259 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700262 * Set once the system is ready to run third party code.
263 */
264 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800265
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * Id of the currently selected input method.
268 */
269 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 /**
272 * The current binding sequence number, incremented every time there is
273 * a new bind performed.
274 */
275 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * The client that is currently bound to an input method.
279 */
280 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700283 * The last window token that gained focus.
284 */
285 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 * The input context last provided by the current client.
289 */
290 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * The attributes last provided by the current client.
294 */
295 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 /**
298 * The input method ID of the input method service that we are currently
299 * connected to or in the process of connecting to.
300 */
301 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 /**
satokab751aa2010-09-14 19:17:36 +0900304 * The current subtype of the current input method.
305 */
306 private InputMethodSubtype mCurrentSubtype;
307
satok4e4569d2010-11-19 18:45:53 +0900308 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900309 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
310 mShortcutInputMethodsAndSubtypes =
311 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900312
313 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 * Set to true if our ServiceConnection is currently actively bound to
315 * a service (whether or not we have gotten its IBinder back yet).
316 */
317 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * Set if the client has asked for the input method to be shown.
321 */
322 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 /**
325 * Set if we were explicitly told to show the input method.
326 */
327 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Set if we were forced to be shown.
331 */
332 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
335 * Set if we last told the input method to show itself.
336 */
337 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
340 * The Intent used to connect to the current input method.
341 */
342 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 /**
345 * The token we have made for the currently active input method, to
346 * identify it in the future.
347 */
348 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 /**
351 * If non-null, this is the input method service we are currently connected
352 * to.
353 */
354 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
357 * Time that we last initiated a bind to the input method, to determine
358 * if we should try to disconnect and reconnect to it.
359 */
360 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
363 * Have we called mCurMethod.bindInput()?
364 */
365 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 /**
368 * Currently enabled session. Only touched by service thread, not
369 * protected by a lock.
370 */
371 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * True if the screen is on. The value is true initially.
375 */
376 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
Joe Onorato857fd9b2011-01-27 15:08:35 -0800378 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
379 int mImeWindowVis;
380
Ken Wakasa05dbb652011-08-22 15:22:43 +0900381 private AlertDialog.Builder mDialogBuilder;
382 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900383 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900384 private InputMethodInfo[] mIms;
385 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900386 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900387 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
388 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500389 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 class SettingsObserver extends ContentObserver {
392 SettingsObserver(Handler handler) {
393 super(handler);
394 ContentResolver resolver = mContext.getContentResolver();
395 resolver.registerContentObserver(Settings.Secure.getUriFor(
396 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900397 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900398 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
399 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900400 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 @Override public void onChange(boolean selfChange) {
404 synchronized (mMethodMap) {
405 updateFromSettingsLocked();
406 }
407 }
408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800409
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900410 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
411 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700413 if (mCurClient != null && mCurClient.client != null) {
414 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
415 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900418
419 @Override
420 public void onReceive(Context context, Intent intent) {
421 final String action = intent.getAction();
422 if (Intent.ACTION_SCREEN_ON.equals(action)) {
423 mScreenOn = true;
424 refreshImeWindowVisibilityLocked();
425 updateActive();
426 return;
427 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
428 mScreenOn = false;
429 setImeWindowVisibilityStatusHiddenLocked();
430 updateActive();
431 return;
432 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
433 hideInputMethodMenu();
434 // No need to updateActive
435 return;
436 } else {
437 Slog.w(TAG, "Unexpected intent " + intent);
438 }
439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800442 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900443 private boolean isChangingPackagesOfCurrentUser() {
444 final int userId = getChangingUserId();
445 final boolean retval = userId == mSettings.getCurrentUserId();
446 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900447 if (!retval) {
448 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
449 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900450 }
451 return retval;
452 }
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800455 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900456 if (!isChangingPackagesOfCurrentUser()) {
457 return false;
458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900460 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 final int N = mMethodList.size();
462 if (curInputMethodId != null) {
463 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 InputMethodInfo imi = mMethodList.get(i);
465 if (imi.getId().equals(curInputMethodId)) {
466 for (String pkg : packages) {
467 if (imi.getPackageName().equals(pkg)) {
468 if (!doit) {
469 return true;
470 }
satok723a27e2010-11-11 14:58:11 +0900471 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800472 chooseNewDefaultIMELocked();
473 return true;
474 }
475 }
476 }
477 }
478 }
479 }
480 return false;
481 }
482
483 @Override
484 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900485 if (!isChangingPackagesOfCurrentUser()) {
486 return;
487 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 synchronized (mMethodMap) {
489 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900490 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800491 final int N = mMethodList.size();
492 if (curInputMethodId != null) {
493 for (int i=0; i<N; i++) {
494 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900495 final String imiId = imi.getId();
496 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800497 curIm = imi;
498 }
satoke7c6998e2011-06-03 17:57:59 +0900499
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800500 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900501 if (isPackageModified(imi.getPackageName())) {
502 mFileManager.deleteAllInputMethodSubtypes(imiId);
503 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800504 if (change == PACKAGE_TEMPORARY_CHANGE
505 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800506 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800507 + imi.getComponent());
508 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510 }
511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800512
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800513 buildInputMethodListLocked(mMethodList, mMethodMap);
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800516
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800517 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 int change = isPackageDisappearing(curIm.getPackageName());
519 if (change == PACKAGE_TEMPORARY_CHANGE
520 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800521 ServiceInfo si = null;
522 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900523 si = mIPackageManager.getServiceInfo(
524 curIm.getComponent(), 0, mSettings.getCurrentUserId());
525 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800526 }
527 if (si == null) {
528 // Uh oh, current input method is no longer around!
529 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900531 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800532 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 changed = true;
534 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900536 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800537 }
538 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800539 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 }
satokab751aa2010-09-14 19:17:36 +0900541
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800542 if (curIm == null) {
543 // We currently don't have a default input method... is
544 // one now available?
545 changed = chooseNewDefaultIMELocked();
546 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 if (changed) {
549 updateFromSettingsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551 }
552 }
553 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800554
Jean Chalarde0d32a62011-10-20 20:36:07 +0900555 private static class MethodCallback extends IInputMethodCallback.Stub {
556 private final IInputMethod mMethod;
557 private final InputMethodManagerService mParentIMMS;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800558
Jean Chalarde0d32a62011-10-20 20:36:07 +0900559 MethodCallback(final IInputMethod method, final InputMethodManagerService imms) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 mMethod = method;
Jean Chalarde0d32a62011-10-20 20:36:07 +0900561 mParentIMMS = imms;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
satoke7c6998e2011-06-03 17:57:59 +0900564 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public void finishedEvent(int seq, boolean handled) throws RemoteException {
566 }
567
satoke7c6998e2011-06-03 17:57:59 +0900568 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 public void sessionCreated(IInputMethodSession session) throws RemoteException {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900570 mParentIMMS.onSessionCreated(mMethod, session);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 }
572 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800573
satok01038492012-04-09 21:08:27 +0900574 private class HardKeyboardListener
575 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
576 @Override
577 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
578 mHandler.sendMessage(mHandler.obtainMessage(
579 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
580 }
581
582 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
583 if (DEBUG) {
584 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
585 + enabled);
586 }
587 synchronized(mMethodMap) {
588 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
589 && mSwitchingDialog.isShowing()) {
590 mSwitchingDialogTitleView.findViewById(
591 com.android.internal.R.id.hard_keyboard_section).setVisibility(
592 available ? View.VISIBLE : View.GONE);
593 }
594 }
595 }
596 }
597
598 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900599 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800601 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 mHandler = new Handler(this);
603 mIWindowManager = IWindowManager.Stub.asInterface(
604 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800605 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900606 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 public void executeMessage(Message msg) {
608 handleMessage(msg);
609 }
Mita Yuned218c72012-12-06 17:18:25 -0800610 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900611 mWindowManagerService = windowManager;
612 mHardKeyboardListener = new HardKeyboardListener();
satok7cfc0ed2011-06-20 21:29:36 +0900613
satok7cfc0ed2011-06-20 21:29:36 +0900614 mImeSwitcherNotification = new Notification();
615 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
616 mImeSwitcherNotification.when = 0;
617 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
618 mImeSwitcherNotification.tickerText = null;
619 mImeSwitcherNotification.defaults = 0; // please be quiet
620 mImeSwitcherNotification.sound = null;
621 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400622
623 // Tag this notification specially so SystemUI knows it's important
624 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
625
satok7cfc0ed2011-06-20 21:29:36 +0900626 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900627 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900628
629 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900630
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900631 final IntentFilter broadcastFilter = new IntentFilter();
632 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
633 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
634 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
635 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800636
satok7cfc0ed2011-06-20 21:29:36 +0900637 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900638 int userId = 0;
639 try {
640 ActivityManagerNative.getDefault().registerUserSwitchObserver(
641 new IUserSwitchObserver.Stub() {
642 @Override
643 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900644 synchronized(mMethodMap) {
645 switchUserLocked(newUserId);
646 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 if (reply != null) {
648 try {
649 reply.sendResult(null);
650 } catch (RemoteException e) {
651 }
652 }
653 }
654
655 @Override
656 public void onUserSwitchComplete(int newUserId) throws RemoteException {
657 }
658 });
659 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
660 } catch (RemoteException e) {
661 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
662 }
satok81f8b7c2012-12-04 20:42:56 +0900663 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900664
satokd87c2592010-09-29 11:52:06 +0900665 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900666 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900667 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900668 mFileManager = new InputMethodFileManager(mMethodMap, userId);
669 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900670
671 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900672 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +0900673 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 buildInputMethodListLocked(mMethodList, mMethodMap);
satokd87c2592010-09-29 11:52:06 +0900676 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
satok0a1bcf42012-05-16 19:26:31 +0900678 if (!mImeSelectedOnBoot) {
679 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900680 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 mSettingsObserver = new SettingsObserver(mHandler);
684 updateFromSettingsLocked();
satok5b927c432012-05-01 20:09:34 +0900685
686 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
687 // according to the new system locale.
688 final IntentFilter filter = new IntentFilter();
689 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
690 mContext.registerReceiver(
691 new BroadcastReceiver() {
692 @Override
693 public void onReceive(Context context, Intent intent) {
694 synchronized(mMethodMap) {
695 checkCurrentLocaleChangedLocked();
696 }
697 }
698 }, filter);
699 }
700
satok5b927c432012-05-01 20:09:34 +0900701 private void resetDefaultImeLocked(Context context) {
702 // Do not reset the default (current) IME when it is a 3rd-party IME
703 if (mCurMethodId != null && !isSystemIme(mMethodMap.get(mCurMethodId))) {
704 return;
705 }
706
707 InputMethodInfo defIm = null;
708 for (InputMethodInfo imi : mMethodList) {
709 if (defIm == null) {
710 if (isValidSystemDefaultIme(imi, context)) {
711 defIm = imi;
712 Slog.i(TAG, "Selected default: " + imi.getId());
713 }
714 }
715 }
716 if (defIm == null && mMethodList.size() > 0) {
717 defIm = getMostApplicableDefaultIMELocked();
718 Slog.i(TAG, "No default found, using " + defIm.getId());
719 }
720 if (defIm != null) {
721 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
722 }
723 }
724
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900725 private void resetAllInternalStateLocked(boolean updateOnlyWhenLocaleChanged) {
726 if (!mSystemReady) {
727 // not system ready
728 return;
729 }
730 final Locale newLocale = mRes.getConfiguration().locale;
731 if (!updateOnlyWhenLocaleChanged
732 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
733 if (!updateOnlyWhenLocaleChanged) {
734 hideCurrentInputLocked(0, null);
735 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700736 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900737 }
738 if (DEBUG) {
739 Slog.i(TAG, "Locale has been changed to " + newLocale);
740 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900741 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
742 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900743 buildInputMethodListLocked(mMethodList, mMethodMap);
744 if (!updateOnlyWhenLocaleChanged) {
745 final String selectedImiId = mSettings.getSelectedInputMethod();
746 if (TextUtils.isEmpty(selectedImiId)) {
747 // This is the first time of the user switch and
748 // set the current ime to the proper one.
749 resetDefaultImeLocked(mContext);
750 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900751 } else {
752 // If the locale is changed, needs to reset the default ime
753 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 }
755 updateFromSettingsLocked();
756 mLastSystemLocale = newLocale;
757 if (!updateOnlyWhenLocaleChanged) {
758 try {
759 startInputInnerLocked();
760 } catch (RuntimeException e) {
761 Slog.w(TAG, "Unexpected exception", e);
762 }
763 }
764 }
765 }
766
767 private void checkCurrentLocaleChangedLocked() {
768 resetAllInternalStateLocked(true);
769 }
770
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900771 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900772 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900773 // InputMethodFileManager should be reset when the user is changed
774 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900775 resetAllInternalStateLocked(false);
776 }
777
satok0a1bcf42012-05-16 19:26:31 +0900778 private boolean isValidSystemDefaultIme(InputMethodInfo imi, Context context) {
779 if (!mSystemReady) {
780 return false;
781 }
satok5b927c432012-05-01 20:09:34 +0900782 if (!isSystemIme(imi)) {
783 return false;
784 }
785 if (imi.getIsDefaultResourceId() != 0) {
786 try {
787 Resources res = context.createPackageContext(
788 imi.getPackageName(), 0).getResources();
789 if (res.getBoolean(imi.getIsDefaultResourceId())
790 && containsSubtypeOf(imi, context.getResources().getConfiguration().
791 locale.getLanguage())) {
792 return true;
793 }
794 } catch (PackageManager.NameNotFoundException ex) {
795 } catch (Resources.NotFoundException ex) {
796 }
797 }
798 if (imi.getSubtypeCount() == 0) {
799 Slog.w(TAG, "Found no subtypes in a system IME: " + imi.getPackageName());
800 }
801 return false;
802 }
803
804 private static boolean isSystemImeThatHasEnglishSubtype(InputMethodInfo imi) {
805 if (!isSystemIme(imi)) {
806 return false;
807 }
808 return containsSubtypeOf(imi, ENGLISH_LOCALE.getLanguage());
809 }
810
811 private static boolean containsSubtypeOf(InputMethodInfo imi, String language) {
812 final int N = imi.getSubtypeCount();
813 for (int i = 0; i < N; ++i) {
814 if (imi.getSubtypeAt(i).getLocale().startsWith(language)) {
815 return true;
816 }
817 }
818 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820
821 @Override
822 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
823 throws RemoteException {
824 try {
825 return super.onTransact(code, data, reply, flags);
826 } catch (RuntimeException e) {
827 // The input method manager only throws security exceptions, so let's
828 // log all others.
829 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800830 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832 throw e;
833 }
834 }
835
Dianne Hackborn661cd522011-08-22 00:26:20 -0700836 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700837 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900838 if (DEBUG) {
839 Slog.d(TAG, "--- systemReady");
840 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700841 if (!mSystemReady) {
842 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900843 mKeyguardManager =
844 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700845 mNotificationManager = (NotificationManager)
846 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
847 mStatusBar = statusBar;
848 statusBar.setIconVisibility("ime", false);
849 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900850 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
851 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900852 if (mShowOngoingImeSwitcherForPhones) {
853 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
854 mHardKeyboardListener);
855 }
satok0a1bcf42012-05-16 19:26:31 +0900856 buildInputMethodListLocked(mMethodList, mMethodMap);
857 if (!mImeSelectedOnBoot) {
858 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
859 checkCurrentLocaleChangedLocked();
860 }
861 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700862 try {
863 startInputInnerLocked();
864 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800865 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700866 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700867 }
868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800870
satok15452a42011-10-28 17:58:28 +0900871 private void setImeWindowVisibilityStatusHiddenLocked() {
872 mImeWindowVis = 0;
873 updateImeWindowStatusLocked();
874 }
875
satok3afd6c02011-11-18 08:38:19 +0900876 private void refreshImeWindowVisibilityLocked() {
877 final Configuration conf = mRes.getConfiguration();
878 final boolean haveHardKeyboard = conf.keyboard
879 != Configuration.KEYBOARD_NOKEYS;
880 final boolean hardKeyShown = haveHardKeyboard
881 && conf.hardKeyboardHidden
882 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500883 final boolean isScreenLocked =
884 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
885 final boolean isScreenSecurelyLocked =
886 isScreenLocked && mKeyguardManager.isKeyguardSecure();
887 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
888 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900889 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
890 updateImeWindowStatusLocked();
891 }
892
satok15452a42011-10-28 17:58:28 +0900893 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900894 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700895 }
896
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900897 // ---------------------------------------------------------------------------------------
898 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
899 // 1) it comes from the system process
900 // 2) the calling process' user id is identical to the current user id IMMS thinks.
901 private boolean calledFromValidUser() {
902 final int uid = Binder.getCallingUid();
903 final int userId = UserHandle.getUserId(uid);
904 if (DEBUG) {
905 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
906 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
907 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900908 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900909 }
910 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
911 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900913
914 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
915 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
916 // must not manage background users' states in any functions.
917 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
918 // by a token.
919 if (mContext.checkCallingOrSelfPermission(
920 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
921 == PackageManager.PERMISSION_GRANTED) {
922 if (DEBUG) {
923 Slog.d(TAG, "--- Access granted because the calling process has "
924 + "the INTERACT_ACROSS_USERS_FULL permission");
925 }
926 return true;
927 }
928 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
929 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900930 }
931
932 private boolean bindCurrentInputMethodService(
933 Intent service, ServiceConnection conn, int flags) {
934 if (service == null || conn == null) {
935 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
936 return false;
937 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800938 return mContext.bindServiceAsUser(service, conn, flags,
939 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900940 }
941
satoke7c6998e2011-06-03 17:57:59 +0900942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900944 // TODO: Make this work even for non-current users?
945 if (!calledFromValidUser()) {
946 return Collections.emptyList();
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 synchronized (mMethodMap) {
949 return new ArrayList<InputMethodInfo>(mMethodList);
950 }
951 }
952
satoke7c6998e2011-06-03 17:57:59 +0900953 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 // TODO: Make this work even for non-current users?
956 if (!calledFromValidUser()) {
957 return Collections.emptyList();
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900960 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 }
963
satokbb4aa062011-01-19 21:40:27 +0900964 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
965 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
966 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
967 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900968 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900969 enabledInputMethodAndSubtypes.put(
970 imi, getEnabledInputMethodSubtypeListLocked(imi, true));
971 }
972 return enabledInputMethodAndSubtypes;
973 }
974
975 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(InputMethodInfo imi,
976 boolean allowsImplicitlySelectedSubtypes) {
977 if (imi == null && mCurMethodId != null) {
978 imi = mMethodMap.get(mCurMethodId);
979 }
satok7265d9b2011-02-14 15:47:30 +0900980 List<InputMethodSubtype> enabledSubtypes =
satokbb4aa062011-01-19 21:40:27 +0900981 mSettings.getEnabledInputMethodSubtypeListLocked(imi);
satok7265d9b2011-02-14 15:47:30 +0900982 if (allowsImplicitlySelectedSubtypes && enabledSubtypes.isEmpty()) {
satoka86f5e42011-09-02 17:12:42 +0900983 enabledSubtypes = getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokbb4aa062011-01-19 21:40:27 +0900984 }
satok7265d9b2011-02-14 15:47:30 +0900985 return InputMethodSubtype.sort(mContext, 0, imi, enabledSubtypes);
satokbb4aa062011-01-19 21:40:27 +0900986 }
987
satoke7c6998e2011-06-03 17:57:59 +0900988 @Override
satok16331c82010-12-20 23:48:46 +0900989 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
990 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 // TODO: Make this work even for non-current users?
992 if (!calledFromValidUser()) {
993 return Collections.emptyList();
994 }
satok67ddf9c2010-11-17 09:45:54 +0900995 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +0900996 return getEnabledInputMethodSubtypeListLocked(imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900997 }
998 }
999
satoke7c6998e2011-06-03 17:57:59 +09001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public void addClient(IInputMethodClient client,
1002 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001003 if (!calledFromValidUser()) {
1004 return;
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 synchronized (mMethodMap) {
1007 mClients.put(client.asBinder(), new ClientState(client,
1008 inputContext, uid, pid));
1009 }
1010 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001011
satoke7c6998e2011-06-03 17:57:59 +09001012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001014 if (!calledFromValidUser()) {
1015 return;
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 synchronized (mMethodMap) {
1018 mClients.remove(client.asBinder());
1019 }
1020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 void executeOrSendMessage(IInterface target, Message msg) {
1023 if (target.asBinder() instanceof Binder) {
1024 mCaller.sendMessage(msg);
1025 } else {
1026 handleMessage(msg);
1027 msg.recycle();
1028 }
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001031 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 + mCurClient.client.asBinder());
1035 if (mBoundToMethod) {
1036 mBoundToMethod = false;
1037 if (mCurMethod != null) {
1038 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1039 MSG_UNBIND_INPUT, mCurMethod));
1040 }
1041 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001042
1043 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1044 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1046 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1047 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Project10592532009-03-18 17:39:46 -07001050 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 private int getImeShowFlags() {
1055 int flags = 0;
1056 if (mShowForced) {
1057 flags |= InputMethod.SHOW_FORCED
1058 | InputMethod.SHOW_EXPLICIT;
1059 } else if (mShowExplicitlyRequested) {
1060 flags |= InputMethod.SHOW_EXPLICIT;
1061 }
1062 return flags;
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 private int getAppShowFlags() {
1066 int flags = 0;
1067 if (mShowForced) {
1068 flags |= InputMethodManager.SHOW_FORCED;
1069 } else if (!mShowExplicitlyRequested) {
1070 flags |= InputMethodManager.SHOW_IMPLICIT;
1071 }
1072 return flags;
1073 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
Dianne Hackborn7663d802012-02-24 13:08:49 -08001075 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 if (!mBoundToMethod) {
1077 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1078 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1079 mBoundToMethod = true;
1080 }
1081 final SessionState session = mCurClient.curSession;
1082 if (initial) {
1083 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1084 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1085 } else {
1086 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1087 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1088 }
1089 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001091 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001093 return new InputBindResult(session.session, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001097 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 // If no method is currently selected, do nothing.
1099 if (mCurMethodId == null) {
1100 return mNoBinding;
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 ClientState cs = mClients.get(client.asBinder());
1104 if (cs == null) {
1105 throw new IllegalArgumentException("unknown client "
1106 + client.asBinder());
1107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 try {
1110 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1111 // Check with the window manager to make sure this client actually
1112 // has a window with focus. If not, reject. This is thread safe
1113 // because if the focus changes some time before or after, the
1114 // next client receiving focus that has any interest in input will
1115 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1118 return null;
1119 }
1120 } catch (RemoteException e) {
1121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
Dianne Hackborn7663d802012-02-24 13:08:49 -08001123 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1124 }
1125
1126 InputBindResult startInputUncheckedLocked(ClientState cs,
1127 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1128 // If no method is currently selected, do nothing.
1129 if (mCurMethodId == null) {
1130 return mNoBinding;
1131 }
1132
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001133 if (mCurClient == null) {
1134 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1135 if (DEBUG) {
1136 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1137 }
1138 }
1139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (mCurClient != cs) {
1141 // If the client is changing, we need to switch over to the new
1142 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001143 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001144 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 + cs.client.asBinder());
1146
1147 // If the screen is on, inform the new client it is active
1148 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001149 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1150 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 // Bump up the sequence for this client and attach it.
1155 mCurSeq++;
1156 if (mCurSeq <= 0) mCurSeq = 1;
1157 mCurClient = cs;
1158 mCurInputContext = inputContext;
1159 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 // Check if the input method is changing.
1162 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1163 if (cs.curSession != null) {
1164 // Fast case: if we are already connected to the input method,
1165 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001166 return attachNewInputLocked(
1167 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 if (mHaveConnection) {
1170 if (mCurMethod != null) {
1171 if (!cs.sessionRequested) {
1172 cs.sessionRequested = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001173 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1175 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001176 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178 // Return to client, and we will get back with it when
1179 // we have had a session made for it.
1180 return new InputBindResult(null, mCurId, mCurSeq);
1181 } else if (SystemClock.uptimeMillis()
1182 < (mLastBindTime+TIME_TO_RECONNECT)) {
1183 // In this case we have connected to the service, but
1184 // don't yet have its interface. If it hasn't been too
1185 // long since we did the connection, we'll return to
1186 // the client and wait to get the service interface so
1187 // we can report back. If it has been too long, we want
1188 // to fall through so we can try a disconnect/reconnect
1189 // to see if we can get back in touch with the service.
1190 return new InputBindResult(null, mCurId, mCurSeq);
1191 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1193 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
1196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001197
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001198 return startInputInnerLocked();
1199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001201 InputBindResult startInputInnerLocked() {
1202 if (mCurMethodId == null) {
1203 return mNoBinding;
1204 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001206 if (!mSystemReady) {
1207 // If the system is not yet ready, we shouldn't be running third
1208 // party code.
Dianne Hackborncc278702009-09-02 23:07:23 -07001209 return new InputBindResult(null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1213 if (info == null) {
1214 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001217 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1220 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001221 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1222 com.android.internal.R.string.input_method_binding_label);
1223 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1224 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001225 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001226 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 mLastBindTime = SystemClock.uptimeMillis();
1228 mHaveConnection = true;
1229 mCurId = info.getId();
1230 mCurToken = new Binder();
1231 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001232 if (DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 mIWindowManager.addWindowToken(mCurToken,
1234 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1235 } catch (RemoteException e) {
1236 }
1237 return new InputBindResult(null, mCurId, mCurSeq);
1238 } else {
1239 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001240 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 + mCurIntent);
1242 }
1243 return null;
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
satoke7c6998e2011-06-03 17:57:59 +09001246 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001248 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001249 if (!calledFromValidUser()) {
1250 return null;
1251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 synchronized (mMethodMap) {
1253 final long ident = Binder.clearCallingIdentity();
1254 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001255 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 } finally {
1257 Binder.restoreCallingIdentity(ident);
1258 }
1259 }
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
satoke7c6998e2011-06-03 17:57:59 +09001262 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 public void finishInput(IInputMethodClient client) {
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
satoke7c6998e2011-06-03 17:57:59 +09001266 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 public void onServiceConnected(ComponentName name, IBinder service) {
1268 synchronized (mMethodMap) {
1269 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1270 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001271 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001272 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001273 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001274 return;
1275 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001276 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001277 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1278 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001280 if (DEBUG) Slog.v(TAG, "Creating first session while with client "
Dianne Hackborncc278702009-09-02 23:07:23 -07001281 + mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
Dianne Hackborncc278702009-09-02 23:07:23 -07001283 MSG_CREATE_SESSION, mCurMethod,
Jean Chalarde0d32a62011-10-20 20:36:07 +09001284 new MethodCallback(mCurMethod, this)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 }
1286 }
1287 }
1288 }
1289
1290 void onSessionCreated(IInputMethod method, IInputMethodSession session) {
1291 synchronized (mMethodMap) {
1292 if (mCurMethod != null && method != null
1293 && mCurMethod.asBinder() == method.asBinder()) {
1294 if (mCurClient != null) {
1295 mCurClient.curSession = new SessionState(mCurClient,
1296 method, session);
1297 mCurClient.sessionRequested = false;
Dianne Hackborn7663d802012-02-24 13:08:49 -08001298 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (res.method != null) {
1300 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1301 MSG_BIND_METHOD, mCurClient.client, res));
1302 }
1303 }
1304 }
1305 }
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001308 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001309 if (mVisibleBound) {
1310 mContext.unbindService(mVisibleConnection);
1311 mVisibleBound = false;
1312 }
1313
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001314 if (mHaveConnection) {
1315 mContext.unbindService(this);
1316 mHaveConnection = false;
1317 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 if (mCurToken != null) {
1320 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001321 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001322 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001323 // The current IME is shown. Hence an IME switch (transition) is happening.
1324 mWindowManagerService.saveLastInputMethodWindowForTransition();
1325 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001326 mIWindowManager.removeWindowToken(mCurToken);
1327 } catch (RemoteException e) {
1328 }
1329 mCurToken = null;
1330 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001331
The Android Open Source Project10592532009-03-18 17:39:46 -07001332 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001333 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001335 if (reportToClient && mCurClient != null) {
1336 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1337 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1338 }
1339 }
1340
Devin Taylor0c33ed22010-02-23 13:26:46 -06001341 private void finishSession(SessionState sessionState) {
1342 if (sessionState != null && sessionState.session != null) {
1343 try {
1344 sessionState.session.finishSession();
1345 } catch (RemoteException e) {
Jean-Baptiste Queru9d0f6df2010-03-29 12:55:09 -07001346 Slog.w(TAG, "Session failed to close due to remote exception", e);
satok15452a42011-10-28 17:58:28 +09001347 setImeWindowVisibilityStatusHiddenLocked();
Devin Taylor0c33ed22010-02-23 13:26:46 -06001348 }
1349 }
1350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001352 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 if (mCurMethod != null) {
1354 for (ClientState cs : mClients.values()) {
1355 cs.sessionRequested = false;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001356 finishSession(cs.curSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 cs.curSession = null;
1358 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001359
1360 finishSession(mEnabledSession);
1361 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 mCurMethod = null;
1363 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001364 if (mStatusBar != null) {
1365 mStatusBar.setIconVisibility("ime", false);
1366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368
satoke7c6998e2011-06-03 17:57:59 +09001369 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 public void onServiceDisconnected(ComponentName name) {
1371 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 + " mCurIntent=" + mCurIntent);
1374 if (mCurMethod != null && mCurIntent != null
1375 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001376 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 // We consider this to be a new bind attempt, since the system
1378 // should now try to restart the service for us.
1379 mLastBindTime = SystemClock.uptimeMillis();
1380 mShowRequested = mInputShown;
1381 mInputShown = false;
1382 if (mCurClient != null) {
1383 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1384 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1385 }
1386 }
1387 }
1388 }
1389
satokf9f01002011-05-19 21:31:50 +09001390 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001392 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 long ident = Binder.clearCallingIdentity();
1394 try {
1395 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001396 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 return;
1398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 synchronized (mMethodMap) {
1401 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001402 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001403 if (mStatusBar != null) {
1404 mStatusBar.setIconVisibility("ime", false);
1405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001407 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001408 CharSequence contentDescription = null;
1409 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001410 // Use PackageManager to load label
1411 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001412 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001413 mIPackageManager.getApplicationInfo(packageName, 0,
1414 mSettings.getCurrentUserId()));
1415 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001416 /* ignore */
1417 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001418 if (mStatusBar != null) {
1419 mStatusBar.setIcon("ime", packageName, iconId, 0,
1420 contentDescription != null
1421 ? contentDescription.toString() : null);
1422 mStatusBar.setIconVisibility("ime", true);
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
1425 }
1426 } finally {
1427 Binder.restoreCallingIdentity(ident);
1428 }
1429 }
1430
satok7cfc0ed2011-06-20 21:29:36 +09001431 private boolean needsToShowImeSwitchOngoingNotification() {
1432 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001433 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001434 synchronized (mMethodMap) {
1435 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1436 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001437 if (N > 2) return true;
1438 if (N < 1) return false;
1439 int nonAuxCount = 0;
1440 int auxCount = 0;
1441 InputMethodSubtype nonAuxSubtype = null;
1442 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001443 for(int i = 0; i < N; ++i) {
1444 final InputMethodInfo imi = imis.get(i);
1445 final List<InputMethodSubtype> subtypes = getEnabledInputMethodSubtypeListLocked(
1446 imi, true);
1447 final int subtypeCount = subtypes.size();
1448 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001449 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001450 } else {
1451 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001452 final InputMethodSubtype subtype = subtypes.get(j);
1453 if (!subtype.isAuxiliary()) {
1454 ++nonAuxCount;
1455 nonAuxSubtype = subtype;
1456 } else {
1457 ++auxCount;
1458 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001459 }
1460 }
1461 }
satok7cfc0ed2011-06-20 21:29:36 +09001462 }
satokb6359412011-06-28 17:47:41 +09001463 if (nonAuxCount > 1 || auxCount > 1) {
1464 return true;
1465 } else if (nonAuxCount == 1 && auxCount == 1) {
1466 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001467 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1468 || auxSubtype.overridesImplicitlyEnabledSubtype()
1469 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001470 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1471 return false;
1472 }
1473 return true;
1474 }
1475 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001476 }
satok7cfc0ed2011-06-20 21:29:36 +09001477 }
1478
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001479 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001480 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001481 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001482 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001484 try {
1485 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001486 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001487 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001488 return;
1489 }
1490
1491 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001492 mImeWindowVis = vis;
1493 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001494 if (mStatusBar != null) {
1495 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1496 }
satok7cfc0ed2011-06-20 21:29:36 +09001497 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001498 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1499 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001500 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001501 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001502 final CharSequence title = mRes.getText(
1503 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001504 final CharSequence imiLabel = imi.loadLabel(pm);
1505 final CharSequence summary = mCurrentSubtype != null
1506 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1507 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1508 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001509 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001510 : imiLabel;
1511
satok7cfc0ed2011-06-20 21:29:36 +09001512 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001513 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001514 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001515 if (DEBUG) {
1516 Slog.d(TAG, "--- show notification: label = " + imiLabel
1517 + ", summary = " + summary);
1518 }
1519 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001520 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001521 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001522 mNotificationShown = true;
1523 }
satok7cfc0ed2011-06-20 21:29:36 +09001524 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001525 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001526 if (DEBUG) {
1527 Slog.d(TAG, "--- hide notification");
1528 }
1529 mNotificationManager.cancelAsUser(null,
1530 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001531 mNotificationShown = false;
1532 }
1533 }
satok06487a52010-10-29 11:37:18 +09001534 }
1535 } finally {
1536 Binder.restoreCallingIdentity(ident);
1537 }
1538 }
1539
satoke7c6998e2011-06-03 17:57:59 +09001540 @Override
satokf9f01002011-05-19 21:31:50 +09001541 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001542 if (!calledFromValidUser()) {
1543 return;
1544 }
satokf9f01002011-05-19 21:31:50 +09001545 synchronized (mMethodMap) {
1546 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1547 for (int i = 0; i < spans.length; ++i) {
1548 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001549 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001550 mSecureSuggestionSpans.put(ss, currentImi);
satok42c5a162011-05-26 16:46:14 +09001551 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(ss);
satokf9f01002011-05-19 21:31:50 +09001552 }
1553 }
1554 }
1555 }
1556
satoke7c6998e2011-06-03 17:57:59 +09001557 @Override
satokf9f01002011-05-19 21:31:50 +09001558 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001559 if (!calledFromValidUser()) {
1560 return false;
1561 }
satokf9f01002011-05-19 21:31:50 +09001562 synchronized (mMethodMap) {
1563 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1564 // TODO: Do not send the intent if the process of the targetImi is already dead.
1565 if (targetImi != null) {
1566 final String[] suggestions = span.getSuggestions();
1567 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001568 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001569 final Intent intent = new Intent();
1570 // Ensures that only a class in the original IME package will receive the
1571 // notification.
satok42c5a162011-05-26 16:46:14 +09001572 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001573 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1574 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1575 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1576 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001577 final long ident = Binder.clearCallingIdentity();
1578 try {
1579 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1580 } finally {
1581 Binder.restoreCallingIdentity(ident);
1582 }
satokf9f01002011-05-19 21:31:50 +09001583 return true;
1584 }
1585 }
1586 return false;
1587 }
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 void updateFromSettingsLocked() {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001590 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1591 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1592 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1593 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001594 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001595 // There is no input method selected, try to choose new applicable input method.
1596 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001597 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001598 }
1599 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 try {
satokab751aa2010-09-14 19:17:36 +09001601 setInputMethodLocked(id, getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001603 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001604 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001605 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
satokf3db1af2010-11-23 13:34:33 +09001607 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 } else {
1609 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001610 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001611 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 }
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
satokab751aa2010-09-14 19:17:36 +09001615 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 InputMethodInfo info = mMethodMap.get(id);
1617 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001618 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001620
satokd81e9502012-05-21 12:58:45 +09001621 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001623 final int subtypeCount = info.getSubtypeCount();
1624 if (subtypeCount <= 0) {
1625 return;
satokcd7cd292010-11-20 15:46:23 +09001626 }
satokd81e9502012-05-21 12:58:45 +09001627 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1628 final InputMethodSubtype newSubtype;
1629 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1630 newSubtype = info.getSubtypeAt(subtypeId);
1631 } else {
1632 // If subtype is null, try to find the most applicable one from
1633 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001634 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001635 }
1636 if (newSubtype == null || oldSubtype == null) {
1637 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1638 + ", new subtype = " + newSubtype);
1639 return;
1640 }
1641 if (newSubtype != oldSubtype) {
1642 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1643 if (mCurMethod != null) {
1644 try {
1645 refreshImeWindowVisibilityLocked();
1646 mCurMethod.changeInputMethodSubtype(newSubtype);
1647 } catch (RemoteException e) {
1648 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001649 }
1650 }
1651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 return;
1653 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001654
satokd81e9502012-05-21 12:58:45 +09001655 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 final long ident = Binder.clearCallingIdentity();
1657 try {
satokab751aa2010-09-14 19:17:36 +09001658 // Set a subtype to this input method.
1659 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001660 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1661 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1662 // because mCurMethodId is stored as a history in
1663 // setSelectedInputMethodAndSubtypeLocked().
1664 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665
1666 if (ActivityManagerNative.isSystemReady()) {
1667 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001668 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001670 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001672 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 } finally {
1674 Binder.restoreCallingIdentity(ident);
1675 }
1676 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001677
satok42c5a162011-05-26 16:46:14 +09001678 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001679 public boolean showSoftInput(IInputMethodClient client, int flags,
1680 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001681 if (!calledFromValidUser()) {
1682 return false;
1683 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001684 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 long ident = Binder.clearCallingIdentity();
1686 try {
1687 synchronized (mMethodMap) {
1688 if (mCurClient == null || client == null
1689 || mCurClient.client.asBinder() != client.asBinder()) {
1690 try {
1691 // We need to check if this is the current client with
1692 // focus in the window manager, to allow this call to
1693 // be made before input is started in it.
1694 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001695 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
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 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001699 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001702
Joe Onorato8a9b2202010-02-26 18:56:32 -08001703 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001704 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 } finally {
1707 Binder.restoreCallingIdentity(ident);
1708 }
1709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001710
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 mShowRequested = true;
1713 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1714 mShowExplicitlyRequested = true;
1715 }
1716 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1717 mShowExplicitlyRequested = true;
1718 mShowForced = true;
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Dianne Hackborncc278702009-09-02 23:07:23 -07001721 if (!mSystemReady) {
1722 return false;
1723 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001724
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 if (mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001727 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1728 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1729 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001731 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001732 bindCurrentInputMethodService(
1733 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001734 mVisibleBound = true;
1735 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001736 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001738 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 // The client has asked to have the input method shown, but
1740 // we have been sitting here too long with a connection to the
1741 // service and no interface received, so let's disconnect/connect
1742 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001743 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001745 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001747 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001748 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001749 } else {
1750 if (DEBUG) {
1751 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1752 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001758
satok42c5a162011-05-26 16:46:14 +09001759 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001760 public boolean hideSoftInput(IInputMethodClient client, int flags,
1761 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001762 if (!calledFromValidUser()) {
1763 return false;
1764 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001765 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 long ident = Binder.clearCallingIdentity();
1767 try {
1768 synchronized (mMethodMap) {
1769 if (mCurClient == null || client == null
1770 || mCurClient.client.asBinder() != client.asBinder()) {
1771 try {
1772 // We need to check if this is the current client with
1773 // focus in the window manager, to allow this call to
1774 // be made before input is started in it.
1775 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001776 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1777 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001778 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001779 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
1781 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001782 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001783 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001786
Joe Onorato8a9b2202010-02-26 18:56:32 -08001787 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 } finally {
1791 Binder.restoreCallingIdentity(ident);
1792 }
1793 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001794
The Android Open Source Project4df24232009-03-05 14:34:35 -08001795 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1797 && (mShowExplicitlyRequested || mShowForced)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001798 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001800 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001803 if (DEBUG) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001805 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001807 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001809 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1810 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1811 res = true;
1812 } else {
1813 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001815 if (mHaveConnection && mVisibleBound) {
1816 mContext.unbindService(mVisibleConnection);
1817 mVisibleBound = false;
1818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 mInputShown = false;
1820 mShowRequested = false;
1821 mShowExplicitlyRequested = false;
1822 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001823 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001825
satok42c5a162011-05-26 16:46:14 +09001826 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001827 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1828 int controlFlags, int softInputMode, int windowFlags,
1829 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001830 // Needs to check the validity before clearing calling identity
1831 final boolean calledFromValidUser = calledFromValidUser();
1832
Dianne Hackborn7663d802012-02-24 13:08:49 -08001833 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 long ident = Binder.clearCallingIdentity();
1835 try {
1836 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001837 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001838 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001840 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001841
Dianne Hackborn7663d802012-02-24 13:08:49 -08001842 ClientState cs = mClients.get(client.asBinder());
1843 if (cs == null) {
1844 throw new IllegalArgumentException("unknown client "
1845 + client.asBinder());
1846 }
1847
1848 try {
1849 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1850 // Check with the window manager to make sure this client actually
1851 // has a window with focus. If not, reject. This is thread safe
1852 // because if the focus changes some time before or after, the
1853 // next client receiving focus that has any interest in input will
1854 // be calling through here after that change happens.
1855 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1856 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1857 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001859 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001861
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001862 if (!calledFromValidUser) {
1863 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1864 Slog.w(TAG, "If you want to interect with IME, you need "
1865 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1866 hideCurrentInputLocked(0, null);
1867 return null;
1868 }
1869
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001870 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001871 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001872 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001873 if (attribute != null) {
1874 return startInputUncheckedLocked(cs, inputContext, attribute,
1875 controlFlags);
1876 }
1877 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001878 }
1879 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001880
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001881 // Should we auto-show the IME even if the caller has not
1882 // specified what should be done with it?
1883 // We only do this automatically if the window can resize
1884 // to accommodate the IME (so what the user sees will give
1885 // them good context without input information being obscured
1886 // by the IME) or if running on a large screen where there
1887 // is more room for the target window + IME.
1888 final boolean doAutoShow =
1889 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1890 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1891 || mRes.getConfiguration().isLayoutSizeAtLeast(
1892 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001893 final boolean isTextEditor =
1894 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1895
1896 // We want to start input before showing the IME, but after closing
1897 // it. We want to do this after closing it to help the IME disappear
1898 // more quickly (not get stuck behind it initializing itself for the
1899 // new focused input, even if its window wants to hide the IME).
1900 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1903 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001904 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1906 // There is no focus view, and this window will
1907 // be behind any soft input window, so hide the
1908 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001909 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001910 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001912 } else if (isTextEditor && doAutoShow && (softInputMode &
1913 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 // There is a focus view, and we are navigating forward
1915 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001916 // We only do this automatically if the window can resize
1917 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001918 // them good context without input information being obscured
1919 // by the IME) or if running on a large screen where there
1920 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001921 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001922 if (attribute != null) {
1923 res = startInputUncheckedLocked(cs, inputContext, attribute,
1924 controlFlags);
1925 didStart = true;
1926 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001927 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929 break;
1930 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1931 // Do nothing.
1932 break;
1933 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
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 hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001937 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939 break;
1940 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001941 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001942 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 break;
1944 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1945 if ((softInputMode &
1946 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001947 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001948 if (attribute != null) {
1949 res = startInputUncheckedLocked(cs, inputContext, attribute,
1950 controlFlags);
1951 didStart = true;
1952 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001953 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
1955 break;
1956 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001957 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001958 if (attribute != null) {
1959 res = startInputUncheckedLocked(cs, inputContext, attribute,
1960 controlFlags);
1961 didStart = true;
1962 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001963 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 break;
1965 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001966
1967 if (!didStart && attribute != null) {
1968 res = startInputUncheckedLocked(cs, inputContext, attribute,
1969 controlFlags);
1970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972 } finally {
1973 Binder.restoreCallingIdentity(ident);
1974 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001975
1976 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001978
satok42c5a162011-05-26 16:46:14 +09001979 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001981 if (!calledFromValidUser()) {
1982 return;
1983 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 synchronized (mMethodMap) {
1985 if (mCurClient == null || client == null
1986 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09001987 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001988 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
1990
satok440aab52010-11-25 09:43:11 +09001991 // Always call subtype picker, because subtype picker is a superset of input method
1992 // picker.
satokab751aa2010-09-14 19:17:36 +09001993 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
1994 }
1995 }
1996
satok42c5a162011-05-26 16:46:14 +09001997 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001999 if (!calledFromValidUser()) {
2000 return;
2001 }
satok28203512010-11-24 11:06:49 +09002002 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2003 }
2004
satok42c5a162011-05-26 16:46:14 +09002005 @Override
satok28203512010-11-24 11:06:49 +09002006 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002007 if (!calledFromValidUser()) {
2008 return;
2009 }
satok28203512010-11-24 11:06:49 +09002010 synchronized (mMethodMap) {
2011 if (subtype != null) {
2012 setInputMethodWithSubtypeId(token, id, getSubtypeIdFromHashCode(
2013 mMethodMap.get(id), subtype.hashCode()));
2014 } else {
2015 setInputMethod(token, id);
2016 }
2017 }
satokab751aa2010-09-14 19:17:36 +09002018 }
2019
satok42c5a162011-05-26 16:46:14 +09002020 @Override
satokb416a712010-11-25 20:42:14 +09002021 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002022 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002023 if (!calledFromValidUser()) {
2024 return;
2025 }
satokb416a712010-11-25 20:42:14 +09002026 synchronized (mMethodMap) {
2027 if (mCurClient == null || client == null
2028 || mCurClient.client.asBinder() != client.asBinder()) {
2029 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2030 }
satok7fee71f2010-12-17 18:54:26 +09002031 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2032 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002033 }
2034 }
2035
satok4fc87d62011-05-20 16:13:43 +09002036 @Override
satok735cf382010-11-11 20:40:09 +09002037 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002038 if (!calledFromValidUser()) {
2039 return false;
2040 }
satok735cf382010-11-11 20:40:09 +09002041 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002042 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002043 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002044 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002045 lastImi = mMethodMap.get(lastIme.first);
2046 } else {
2047 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002048 }
satok4fc87d62011-05-20 16:13:43 +09002049 String targetLastImiId = null;
2050 int subtypeId = NOT_A_SUBTYPE_ID;
2051 if (lastIme != null && lastImi != null) {
2052 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2053 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2054 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2055 : mCurrentSubtype.hashCode();
2056 // If the last IME is the same as the current IME and the last subtype is not
2057 // defined, there is no need to switch to the last IME.
2058 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2059 targetLastImiId = lastIme.first;
2060 subtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2061 }
2062 }
2063
2064 if (TextUtils.isEmpty(targetLastImiId) && !canAddToLastInputMethod(mCurrentSubtype)) {
2065 // This is a safety net. If the currentSubtype can't be added to the history
2066 // and the framework couldn't find the last ime, we will make the last ime be
2067 // the most applicable enabled keyboard subtype of the system imes.
2068 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2069 if (enabled != null) {
2070 final int N = enabled.size();
2071 final String locale = mCurrentSubtype == null
2072 ? mRes.getConfiguration().locale.toString()
2073 : mCurrentSubtype.getLocale();
2074 for (int i = 0; i < N; ++i) {
2075 final InputMethodInfo imi = enabled.get(i);
2076 if (imi.getSubtypeCount() > 0 && isSystemIme(imi)) {
2077 InputMethodSubtype keyboardSubtype =
2078 findLastResortApplicableSubtypeLocked(mRes, getSubtypes(imi),
2079 SUBTYPE_MODE_KEYBOARD, locale, true);
2080 if (keyboardSubtype != null) {
2081 targetLastImiId = imi.getId();
2082 subtypeId = getSubtypeIdFromHashCode(
2083 imi, keyboardSubtype.hashCode());
2084 if(keyboardSubtype.getLocale().equals(locale)) {
2085 break;
2086 }
2087 }
2088 }
2089 }
2090 }
2091 }
2092
2093 if (!TextUtils.isEmpty(targetLastImiId)) {
2094 if (DEBUG) {
2095 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2096 + ", from: " + mCurMethodId + ", " + subtypeId);
2097 }
2098 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2099 return true;
2100 } else {
2101 return false;
2102 }
satok735cf382010-11-11 20:40:09 +09002103 }
2104 }
2105
satoke7c6998e2011-06-03 17:57:59 +09002106 @Override
satok688bd472012-02-09 20:09:17 +09002107 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 if (!calledFromValidUser()) {
2109 return false;
2110 }
satok688bd472012-02-09 20:09:17 +09002111 synchronized (mMethodMap) {
2112 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2113 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2114 if (nextSubtype == null) {
2115 return false;
2116 }
2117 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2118 return true;
2119 }
2120 }
2121
2122 @Override
satok68f1b782011-04-11 14:26:04 +09002123 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002124 if (!calledFromValidUser()) {
2125 return null;
2126 }
satok68f1b782011-04-11 14:26:04 +09002127 synchronized (mMethodMap) {
2128 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2129 // TODO: Handle the case of the last IME with no subtypes
2130 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2131 || TextUtils.isEmpty(lastIme.second)) return null;
2132 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2133 if (lastImi == null) return null;
2134 try {
2135 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
satok0e7d7d62011-07-05 13:28:06 +09002136 final int lastSubtypeId = getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
2137 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2138 return null;
2139 }
2140 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002141 } catch (NumberFormatException e) {
2142 return null;
2143 }
2144 }
2145 }
2146
satoke7c6998e2011-06-03 17:57:59 +09002147 @Override
satokee5e77c2011-09-02 18:50:15 +09002148 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002149 if (!calledFromValidUser()) {
2150 return;
2151 }
satok91e88122011-07-18 11:11:42 +09002152 // By this IPC call, only a process which shares the same uid with the IME can add
2153 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002154 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002155 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002156 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002157 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002158 final String[] packageInfos;
2159 try {
2160 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2161 } catch (RemoteException e) {
2162 Slog.e(TAG, "Failed to get package infos");
2163 return;
2164 }
satok91e88122011-07-18 11:11:42 +09002165 if (packageInfos != null) {
2166 final int packageNum = packageInfos.length;
2167 for (int i = 0; i < packageNum; ++i) {
2168 if (packageInfos[i].equals(imi.getPackageName())) {
2169 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002170 final long ident = Binder.clearCallingIdentity();
2171 try {
2172 buildInputMethodListLocked(mMethodList, mMethodMap);
2173 } finally {
2174 Binder.restoreCallingIdentity(ident);
2175 }
satokee5e77c2011-09-02 18:50:15 +09002176 return;
satok91e88122011-07-18 11:11:42 +09002177 }
2178 }
2179 }
satoke7c6998e2011-06-03 17:57:59 +09002180 }
satokee5e77c2011-09-02 18:50:15 +09002181 return;
satoke7c6998e2011-06-03 17:57:59 +09002182 }
2183
satok28203512010-11-24 11:06:49 +09002184 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 synchronized (mMethodMap) {
2186 if (token == null) {
2187 if (mContext.checkCallingOrSelfPermission(
2188 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2189 != PackageManager.PERMISSION_GRANTED) {
2190 throw new SecurityException(
2191 "Using null token requires permission "
2192 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2193 }
2194 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002195 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2196 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 return;
2198 }
2199
satokc5933802011-08-31 21:26:04 +09002200 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 try {
satokab751aa2010-09-14 19:17:36 +09002202 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203 } finally {
2204 Binder.restoreCallingIdentity(ident);
2205 }
2206 }
2207 }
2208
satok42c5a162011-05-26 16:46:14 +09002209 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002211 if (!calledFromValidUser()) {
2212 return;
2213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 synchronized (mMethodMap) {
2215 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002216 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2217 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 return;
2219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 long ident = Binder.clearCallingIdentity();
2221 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002222 hideCurrentInputLocked(flags, null);
2223 } finally {
2224 Binder.restoreCallingIdentity(ident);
2225 }
2226 }
2227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002228
satok42c5a162011-05-26 16:46:14 +09002229 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002230 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002231 if (!calledFromValidUser()) {
2232 return;
2233 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002234 synchronized (mMethodMap) {
2235 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002236 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2237 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002238 return;
2239 }
2240 long ident = Binder.clearCallingIdentity();
2241 try {
2242 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 } finally {
2244 Binder.restoreCallingIdentity(ident);
2245 }
2246 }
2247 }
2248
2249 void setEnabledSessionInMainThread(SessionState session) {
2250 if (mEnabledSession != session) {
2251 if (mEnabledSession != null) {
2252 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002253 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 mEnabledSession.method.setSessionEnabled(
2255 mEnabledSession.session, false);
2256 } catch (RemoteException e) {
2257 }
2258 }
2259 mEnabledSession = session;
2260 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002261 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 session.method.setSessionEnabled(
2263 session.session, true);
2264 } catch (RemoteException e) {
2265 }
2266 }
2267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002268
satok42c5a162011-05-26 16:46:14 +09002269 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002271 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 switch (msg.what) {
2273 case MSG_SHOW_IM_PICKER:
2274 showInputMethodMenu();
2275 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002276
satokab751aa2010-09-14 19:17:36 +09002277 case MSG_SHOW_IM_SUBTYPE_PICKER:
2278 showInputMethodSubtypeMenu();
2279 return true;
2280
satok47a44912010-10-06 16:03:58 +09002281 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002282 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002283 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002284 args.recycle();
satok217f5482010-12-15 05:19:19 +09002285 return true;
2286
2287 case MSG_SHOW_IM_CONFIG:
2288 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002289 return true;
2290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 case MSG_UNBIND_INPUT:
2294 try {
2295 ((IInputMethod)msg.obj).unbindInput();
2296 } catch (RemoteException e) {
2297 // There is nothing interesting about the method dying.
2298 }
2299 return true;
2300 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002301 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 try {
2303 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2304 } catch (RemoteException e) {
2305 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002306 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 return true;
2308 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002309 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002311 ((IInputMethod)args.arg1).showSoftInput(msg.arg1,
2312 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 } catch (RemoteException e) {
2314 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002315 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 return true;
2317 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002318 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002320 ((IInputMethod)args.arg1).hideSoftInput(0,
2321 (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 } catch (RemoteException e) {
2323 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002324 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 return true;
2326 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002327 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002329 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2331 } catch (RemoteException e) {
2332 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002333 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 return true;
2335 case MSG_CREATE_SESSION:
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 ((IInputMethod)args.arg1).createSession(
2339 (IInputMethodCallback)args.arg2);
2340 } catch (RemoteException e) {
2341 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002342 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 case MSG_START_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.startInput((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;
2357 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002358 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 try {
2360 SessionState session = (SessionState)args.arg1;
2361 setEnabledSessionInMainThread(session);
2362 session.method.restartInput((IInputContext)args.arg2,
2363 (EditorInfo)args.arg3);
2364 } catch (RemoteException e) {
2365 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002366 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 case MSG_UNBIND_METHOD:
2372 try {
2373 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2374 } catch (RemoteException e) {
2375 // There is nothing interesting about the last client dying.
2376 }
2377 return true;
2378 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002379 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002380 try {
2381 ((IInputMethodClient)args.arg1).onBindMethod(
2382 (InputBindResult)args.arg2);
2383 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002384 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002386 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002388 case MSG_SET_ACTIVE:
2389 try {
2390 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2391 } catch (RemoteException e) {
2392 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2393 + ((ClientState)msg.obj).pid + " uid "
2394 + ((ClientState)msg.obj).uid);
2395 }
2396 return true;
satok01038492012-04-09 21:08:27 +09002397
2398 // --------------------------------------------------------------
2399 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2400 mHardKeyboardListener.handleHardKeyboardStatusChange(
2401 msg.arg1 == 1, msg.arg2 == 1);
2402 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404 return false;
2405 }
2406
satok5b927c432012-05-01 20:09:34 +09002407 private static boolean isSystemIme(InputMethodInfo inputMethod) {
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002408 return (inputMethod.getServiceInfo().applicationInfo.flags
2409 & ApplicationInfo.FLAG_SYSTEM) != 0;
2410 }
2411
Ken Wakasa586f0512011-01-20 22:31:01 +09002412 private static ArrayList<InputMethodSubtype> getSubtypes(InputMethodInfo imi) {
2413 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2414 final int subtypeCount = imi.getSubtypeCount();
2415 for (int i = 0; i < subtypeCount; ++i) {
2416 subtypes.add(imi.getSubtypeAt(i));
2417 }
2418 return subtypes;
2419 }
2420
satoka86f5e42011-09-02 17:12:42 +09002421 private static ArrayList<InputMethodSubtype> getOverridingImplicitlyEnabledSubtypes(
2422 InputMethodInfo imi, String mode) {
2423 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
2424 final int subtypeCount = imi.getSubtypeCount();
2425 for (int i = 0; i < subtypeCount; ++i) {
2426 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
2427 if (subtype.overridesImplicitlyEnabledSubtype() && subtype.getMode().equals(mode)) {
2428 subtypes.add(subtype);
2429 }
2430 }
2431 return subtypes;
2432 }
2433
satokdc9ddae2011-10-06 12:22:36 +09002434 private InputMethodInfo getMostApplicableDefaultIMELocked() {
satokd87c2592010-09-29 11:52:06 +09002435 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002436 if (enabled != null && enabled.size() > 0) {
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002437 // We'd prefer to fall back on a system IME, since that is safer.
satok0a1bcf42012-05-16 19:26:31 +09002438 int i = enabled.size();
2439 int firstFoundSystemIme = -1;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002440 while (i > 0) {
2441 i--;
satokdc9ddae2011-10-06 12:22:36 +09002442 final InputMethodInfo imi = enabled.get(i);
satok0a1bcf42012-05-16 19:26:31 +09002443 if (isSystemImeThatHasEnglishSubtype(imi) && !imi.isAuxiliaryIme()) {
2444 return imi;
2445 }
2446 if (firstFoundSystemIme < 0 && isSystemIme(imi) && !imi.isAuxiliaryIme()) {
2447 firstFoundSystemIme = i;
Dianne Hackborn83e48f52010-03-23 23:03:25 -07002448 }
2449 }
satok0a1bcf42012-05-16 19:26:31 +09002450 return enabled.get(Math.max(firstFoundSystemIme, 0));
satokdc9ddae2011-10-06 12:22:36 +09002451 }
2452 return null;
2453 }
2454
2455 private boolean chooseNewDefaultIMELocked() {
2456 final InputMethodInfo imi = getMostApplicableDefaultIMELocked();
2457 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002458 if (DEBUG) {
2459 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2460 }
satok723a27e2010-11-11 14:58:11 +09002461 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002462 return true;
2463 }
2464
2465 return false;
2466 }
2467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
2469 HashMap<String, InputMethodInfo> map) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002470 if (DEBUG) {
2471 Slog.d(TAG, "--- re-buildInputMethodList " + ", \n ------ \n" + getStackTrace());
2472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 list.clear();
2474 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002475
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002476 // Use for queryIntentServicesAsUser
2477 final PackageManager pm = mContext.getPackageManager();
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002478 final Configuration config = mRes.getConfiguration();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002479 final boolean haveHardKeyboard = config.keyboard == Configuration.KEYBOARD_QWERTY;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002480 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002481 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002483 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 new Intent(InputMethod.SERVICE_INTERFACE),
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002485 PackageManager.GET_META_DATA, mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002486
satoke7c6998e2011-06-03 17:57:59 +09002487 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2488 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 for (int i = 0; i < services.size(); ++i) {
2490 ResolveInfo ri = services.get(i);
2491 ServiceInfo si = ri.serviceInfo;
2492 ComponentName compName = new ComponentName(si.packageName, si.name);
2493 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2494 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002495 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 + ": it does not require the permission "
2497 + android.Manifest.permission.BIND_INPUT_METHOD);
2498 continue;
2499 }
2500
Joe Onorato8a9b2202010-02-26 18:56:32 -08002501 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
2503 try {
satoke7c6998e2011-06-03 17:57:59 +09002504 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002506 final String id = p.getId();
2507 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508
satok5b927c432012-05-01 20:09:34 +09002509 // Valid system default IMEs and IMEs that have English subtypes are enabled
Satoshi Kataoka15451bd2012-11-16 13:17:54 +09002510 // by default
2511 if ((isValidSystemDefaultIme(p, mContext) || isSystemImeThatHasEnglishSubtype(p))) {
Amith Yamasanie861ec12010-03-24 21:39:27 -07002512 setInputMethodEnabledLocked(id, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002513 }
2514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002516 Slog.d(TAG, "Found a third-party input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002520 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002522 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 }
2524 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002525
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002526 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002527 if (!TextUtils.isEmpty(defaultImiId)) {
2528 if (!map.containsKey(defaultImiId)) {
2529 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2530 if (chooseNewDefaultIMELocked()) {
2531 updateFromSettingsLocked();
2532 }
2533 } else {
2534 // Double check that the default IME is certainly enabled.
2535 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002536 }
2537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002541
satokab751aa2010-09-14 19:17:36 +09002542 private void showInputMethodMenu() {
2543 showInputMethodMenuInternal(false);
2544 }
2545
2546 private void showInputMethodSubtypeMenu() {
2547 showInputMethodMenuInternal(true);
2548 }
2549
satok217f5482010-12-15 05:19:19 +09002550 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002551 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002552 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002553 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2554 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002555 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002556 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002557 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002558 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002559 }
2560
2561 private void showConfigureInputMethods() {
2562 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2563 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2564 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2565 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002566 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002567 }
2568
satok2c93efc2012-04-02 19:33:47 +09002569 private boolean isScreenLocked() {
2570 return mKeyguardManager != null
2571 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2572 }
satokab751aa2010-09-14 19:17:36 +09002573 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002574 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002577 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002578
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002579 final String lastInputMethodId = mSettings.getSelectedInputMethod();
satokab751aa2010-09-14 19:17:36 +09002580 int lastInputMethodSubtypeId = getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002581 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002582
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002583 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002584 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2585 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002586 if (immis == null || immis.size() == 0) {
2587 return;
2588 }
2589
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002590 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591
satok688bd472012-02-09 20:09:17 +09002592 final List<ImeSubtypeListItem> imList =
2593 mImListManager.getSortedInputMethodAndSubtypeList(
2594 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002595
satokc3690562012-01-10 20:14:43 +09002596 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002597 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002598 if (currentSubtype != null) {
2599 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
2600 lastInputMethodSubtypeId =
2601 getSubtypeIdFromHashCode(currentImi, currentSubtype.hashCode());
2602 }
2603 }
2604
Ken Wakasa761eb372011-03-04 19:06:18 +09002605 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002606 mIms = new InputMethodInfo[N];
2607 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002608 int checkedItem = 0;
2609 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002610 final ImeSubtypeListItem item = imList.get(i);
2611 mIms[i] = item.mImi;
2612 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002613 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002614 int subtypeId = mSubtypeIds[i];
2615 if ((subtypeId == NOT_A_SUBTYPE_ID)
2616 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2617 || (subtypeId == lastInputMethodSubtypeId)) {
2618 checkedItem = i;
2619 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002622 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002623 com.android.internal.R.styleable.DialogPreference,
2624 com.android.internal.R.attr.alertDialogStyle, 0);
2625 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002626 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002627 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002628 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002630 }
2631 })
2632 .setIcon(a.getDrawable(
2633 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2634 a.recycle();
satok01038492012-04-09 21:08:27 +09002635 final LayoutInflater inflater =
2636 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2637 final View tv = inflater.inflate(
2638 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2639 mDialogBuilder.setCustomTitle(tv);
2640
2641 // Setup layout for a toggle switch of the hardware keyboard
2642 mSwitchingDialogTitleView = tv;
2643 mSwitchingDialogTitleView.findViewById(
2644 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2645 mWindowManagerService.isHardKeyboardAvailable() ?
2646 View.VISIBLE : View.GONE);
2647 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2648 com.android.internal.R.id.hard_keyboard_switch));
2649 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2650 hardKeySwitch.setOnCheckedChangeListener(
2651 new OnCheckedChangeListener() {
2652 @Override
2653 public void onCheckedChanged(
2654 CompoundButton buttonView, boolean isChecked) {
2655 mWindowManagerService.setHardKeyboardEnabled(isChecked);
2656 }
2657 });
satokd87c2592010-09-29 11:52:06 +09002658
Ken Wakasa05dbb652011-08-22 15:22:43 +09002659 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2660 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2661 checkedItem);
2662
2663 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002664 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002665 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002666 public void onClick(DialogInterface dialog, int which) {
2667 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002668 if (mIms == null || mIms.length <= which
2669 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002670 return;
2671 }
2672 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002673 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002674 adapter.mCheckedItem = which;
2675 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002676 hideInputMethodMenu();
2677 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002678 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002679 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002680 subtypeId = NOT_A_SUBTYPE_ID;
2681 }
2682 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002683 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002686 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687
satokbc81b692011-08-26 16:22:22 +09002688 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002689 mDialogBuilder.setPositiveButton(
2690 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002691 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002692 @Override
satok7f35c8c2010-10-07 21:13:11 +09002693 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002694 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002695 }
2696 });
2697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002699 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 mSwitchingDialog.getWindow().setType(
2701 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002702 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2703 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002704 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 mSwitchingDialog.show();
2706 }
2707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002708
satok93d744d2012-05-09 17:14:08 +09002709 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002710 public final CharSequence mImeName;
2711 public final CharSequence mSubtypeName;
2712 public final InputMethodInfo mImi;
2713 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002714 private final boolean mIsSystemLocale;
2715 private final boolean mIsSystemLanguage;
2716
Ken Wakasa05dbb652011-08-22 15:22:43 +09002717 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002718 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002719 mImeName = imeName;
2720 mSubtypeName = subtypeName;
2721 mImi = imi;
2722 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002723 if (TextUtils.isEmpty(subtypeLocale)) {
2724 mIsSystemLocale = false;
2725 mIsSystemLanguage = false;
2726 } else {
2727 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2728 mIsSystemLanguage = mIsSystemLocale
2729 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2730 }
2731 }
2732
2733 @Override
2734 public int compareTo(ImeSubtypeListItem other) {
2735 if (TextUtils.isEmpty(mImeName)) {
2736 return 1;
2737 }
2738 if (TextUtils.isEmpty(other.mImeName)) {
2739 return -1;
2740 }
2741 if (!TextUtils.equals(mImeName, other.mImeName)) {
2742 return mImeName.toString().compareTo(other.mImeName.toString());
2743 }
2744 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2745 return 0;
2746 }
2747 if (mIsSystemLocale) {
2748 return -1;
2749 }
2750 if (other.mIsSystemLocale) {
2751 return 1;
2752 }
2753 if (mIsSystemLanguage) {
2754 return -1;
2755 }
2756 if (other.mIsSystemLanguage) {
2757 return 1;
2758 }
2759 if (TextUtils.isEmpty(mSubtypeName)) {
2760 return 1;
2761 }
2762 if (TextUtils.isEmpty(other.mSubtypeName)) {
2763 return -1;
2764 }
2765 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002766 }
2767 }
2768
2769 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2770 private final LayoutInflater mInflater;
2771 private final int mTextViewResourceId;
2772 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002773 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002774 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2775 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2776 super(context, textViewResourceId, itemsList);
2777 mTextViewResourceId = textViewResourceId;
2778 mItemsList = itemsList;
2779 mCheckedItem = checkedItem;
2780 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2781 }
2782
2783 @Override
2784 public View getView(int position, View convertView, ViewGroup parent) {
2785 final View view = convertView != null ? convertView
2786 : mInflater.inflate(mTextViewResourceId, null);
2787 if (position < 0 || position >= mItemsList.size()) return view;
2788 final ImeSubtypeListItem item = mItemsList.get(position);
2789 final CharSequence imeName = item.mImeName;
2790 final CharSequence subtypeName = item.mSubtypeName;
2791 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2792 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2793 if (TextUtils.isEmpty(subtypeName)) {
2794 firstTextView.setText(imeName);
2795 secondTextView.setVisibility(View.GONE);
2796 } else {
2797 firstTextView.setText(subtypeName);
2798 secondTextView.setText(imeName);
2799 secondTextView.setVisibility(View.VISIBLE);
2800 }
2801 final RadioButton radioButton =
2802 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2803 radioButton.setChecked(position == mCheckedItem);
2804 return view;
2805 }
2806 }
2807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002809 synchronized (mMethodMap) {
2810 hideInputMethodMenuLocked();
2811 }
2812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002813
The Android Open Source Project10592532009-03-18 17:39:46 -07002814 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002815 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816
The Android Open Source Project10592532009-03-18 17:39:46 -07002817 if (mSwitchingDialog != null) {
2818 mSwitchingDialog.dismiss();
2819 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002821
The Android Open Source Project10592532009-03-18 17:39:46 -07002822 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002823 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002827
satok42c5a162011-05-26 16:46:14 +09002828 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002830 // TODO: Make this work even for non-current users?
2831 if (!calledFromValidUser()) {
2832 return false;
2833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 synchronized (mMethodMap) {
2835 if (mContext.checkCallingOrSelfPermission(
2836 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2837 != PackageManager.PERMISSION_GRANTED) {
2838 throw new SecurityException(
2839 "Requires permission "
2840 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2841 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 long ident = Binder.clearCallingIdentity();
2844 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002845 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 } finally {
2847 Binder.restoreCallingIdentity(ident);
2848 }
2849 }
2850 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002851
2852 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2853 // Make sure this is a valid input method.
2854 InputMethodInfo imm = mMethodMap.get(id);
2855 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002856 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002857 }
2858
satokd87c2592010-09-29 11:52:06 +09002859 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2860 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002861
satokd87c2592010-09-29 11:52:06 +09002862 if (enabled) {
2863 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2864 if (pair.first.equals(id)) {
2865 // We are enabling this input method, but it is already enabled.
2866 // Nothing to do. The previous state was enabled.
2867 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002868 }
2869 }
satokd87c2592010-09-29 11:52:06 +09002870 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2871 // Previous state was disabled.
2872 return false;
2873 } else {
2874 StringBuilder builder = new StringBuilder();
2875 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2876 builder, enabledInputMethodsList, id)) {
2877 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002878 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002879 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2880 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2881 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002882 }
2883 // Previous state was enabled.
2884 return true;
2885 } else {
2886 // We are disabling the input method but it is already disabled.
2887 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002888 return false;
2889 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002890 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002891 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002892
satok57ffc002011-01-25 00:11:47 +09002893 private boolean canAddToLastInputMethod(InputMethodSubtype subtype) {
2894 if (subtype == null) return true;
satok9b415792011-05-30 12:37:52 +09002895 return !subtype.isAuxiliary();
satok57ffc002011-01-25 00:11:47 +09002896 }
2897
satok723a27e2010-11-11 14:58:11 +09002898 private void saveCurrentInputMethodAndSubtypeToHistory() {
2899 String subtypeId = NOT_A_SUBTYPE_ID_STR;
2900 if (mCurrentSubtype != null) {
2901 subtypeId = String.valueOf(mCurrentSubtype.hashCode());
2902 }
satok57ffc002011-01-25 00:11:47 +09002903 if (canAddToLastInputMethod(mCurrentSubtype)) {
2904 mSettings.addSubtypeToHistory(mCurMethodId, subtypeId);
2905 }
satokab751aa2010-09-14 19:17:36 +09002906 }
2907
satok723a27e2010-11-11 14:58:11 +09002908 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2909 boolean setSubtypeOnly) {
2910 // Update the history of InputMethod and Subtype
2911 saveCurrentInputMethodAndSubtypeToHistory();
2912
2913 // Set Subtype here
2914 if (imi == null || subtypeId < 0) {
2915 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002916 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002917 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002918 if (subtypeId < imi.getSubtypeCount()) {
2919 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2920 mSettings.putSelectedSubtype(subtype.hashCode());
2921 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002922 } else {
2923 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002924 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002925 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002926 }
satokab751aa2010-09-14 19:17:36 +09002927 }
satok723a27e2010-11-11 14:58:11 +09002928
satok4c0e7152012-06-20 20:08:44 +09002929 // Workaround.
2930 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2931 // IMEs are not recognized and considered uninstalled.
2932 // Actually, we can't move everything after SystemReady because
2933 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2934 // the default IME here and try cheking the default IME again in systemReady().
2935 // TODO: Do nothing before system ready and implement a separated logic for
2936 // the encryption lock screen.
2937 // TODO: ASEC should be ready before IMMS is instantiated.
2938 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002939 // Set InputMethod here
2940 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2941 }
2942 }
2943
2944 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2945 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2946 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2947 // newDefaultIme is empty when there is no candidate for the selected IME.
2948 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2949 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2950 if (subtypeHashCode != null) {
2951 try {
2952 lastSubtypeId = getSubtypeIdFromHashCode(
2953 imi, Integer.valueOf(subtypeHashCode));
2954 } catch (NumberFormatException e) {
2955 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2956 }
2957 }
2958 }
2959 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002960 }
2961
2962 private int getSelectedInputMethodSubtypeId(String id) {
2963 InputMethodInfo imi = mMethodMap.get(id);
2964 if (imi == null) {
2965 return NOT_A_SUBTYPE_ID;
2966 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002967 final int subtypeHashCode = mSettings.getSelectedInputMethodSubtypeHashCode();
2968 return getSubtypeIdFromHashCode(imi, subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09002969 }
2970
satokfdf419e2012-05-08 16:52:08 +09002971 private static boolean isValidSubtypeId(InputMethodInfo imi, int subtypeHashCode) {
2972 return getSubtypeIdFromHashCode(imi, subtypeHashCode) != NOT_A_SUBTYPE_ID;
2973 }
2974
2975 private static int getSubtypeIdFromHashCode(InputMethodInfo imi, int subtypeHashCode) {
satok28203512010-11-24 11:06:49 +09002976 if (imi != null) {
Ken Wakasa586f0512011-01-20 22:31:01 +09002977 final int subtypeCount = imi.getSubtypeCount();
2978 for (int i = 0; i < subtypeCount; ++i) {
2979 InputMethodSubtype ims = imi.getSubtypeAt(i);
satok28203512010-11-24 11:06:49 +09002980 if (subtypeHashCode == ims.hashCode()) {
2981 return i;
2982 }
satokab751aa2010-09-14 19:17:36 +09002983 }
2984 }
2985 return NOT_A_SUBTYPE_ID;
2986 }
2987
satoka86f5e42011-09-02 17:12:42 +09002988 private static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(
2989 Resources res, InputMethodInfo imi) {
2990 final List<InputMethodSubtype> subtypes = getSubtypes(imi);
satokdf31ae62011-01-15 06:19:44 +09002991 final String systemLocale = res.getConfiguration().locale.toString();
satok3da92232011-01-11 22:46:30 +09002992 if (TextUtils.isEmpty(systemLocale)) return new ArrayList<InputMethodSubtype>();
satok4a553e32011-10-03 17:05:50 +09002993 final HashMap<String, InputMethodSubtype> applicableModeAndSubtypesMap =
satok3da92232011-01-11 22:46:30 +09002994 new HashMap<String, InputMethodSubtype>();
satok16331c82010-12-20 23:48:46 +09002995 final int N = subtypes.size();
satok16331c82010-12-20 23:48:46 +09002996 for (int i = 0; i < N; ++i) {
satoka86f5e42011-09-02 17:12:42 +09002997 // scan overriding implicitly enabled subtypes.
2998 InputMethodSubtype subtype = subtypes.get(i);
2999 if (subtype.overridesImplicitlyEnabledSubtype()) {
3000 final String mode = subtype.getMode();
3001 if (!applicableModeAndSubtypesMap.containsKey(mode)) {
3002 applicableModeAndSubtypesMap.put(mode, subtype);
3003 }
3004 }
3005 }
3006 if (applicableModeAndSubtypesMap.size() > 0) {
3007 return new ArrayList<InputMethodSubtype>(applicableModeAndSubtypesMap.values());
3008 }
3009 for (int i = 0; i < N; ++i) {
satok4a553e32011-10-03 17:05:50 +09003010 final InputMethodSubtype subtype = subtypes.get(i);
satok3da92232011-01-11 22:46:30 +09003011 final String locale = subtype.getLocale();
3012 final String mode = subtype.getMode();
3013 // When system locale starts with subtype's locale, that subtype will be applicable
3014 // for system locale
3015 // For instance, it's clearly applicable for cases like system locale = en_US and
3016 // subtype = en, but it is not necessarily considered applicable for cases like system
3017 // locale = en and subtype = en_US.
3018 // We just call systemLocale.startsWith(locale) in this function because there is no
3019 // need to find applicable subtypes aggressively unlike
3020 // findLastResortApplicableSubtypeLocked.
3021 if (systemLocale.startsWith(locale)) {
satok4a553e32011-10-03 17:05:50 +09003022 final InputMethodSubtype applicableSubtype = applicableModeAndSubtypesMap.get(mode);
satok3da92232011-01-11 22:46:30 +09003023 // If more applicable subtypes are contained, skip.
satok4a553e32011-10-03 17:05:50 +09003024 if (applicableSubtype != null) {
3025 if (systemLocale.equals(applicableSubtype.getLocale())) continue;
3026 if (!systemLocale.equals(locale)) continue;
3027 }
satok3da92232011-01-11 22:46:30 +09003028 applicableModeAndSubtypesMap.put(mode, subtype);
satokc3690562012-01-10 20:14:43 +09003029 }
3030 }
3031 final InputMethodSubtype keyboardSubtype
3032 = applicableModeAndSubtypesMap.get(SUBTYPE_MODE_KEYBOARD);
3033 final ArrayList<InputMethodSubtype> applicableSubtypes = new ArrayList<InputMethodSubtype>(
3034 applicableModeAndSubtypesMap.values());
3035 if (keyboardSubtype != null && !keyboardSubtype.containsExtraValueKey(TAG_ASCII_CAPABLE)) {
3036 for (int i = 0; i < N; ++i) {
3037 final InputMethodSubtype subtype = subtypes.get(i);
3038 final String mode = subtype.getMode();
3039 if (SUBTYPE_MODE_KEYBOARD.equals(mode) && subtype.containsExtraValueKey(
3040 TAG_ENABLED_WHEN_DEFAULT_IS_NOT_ASCII_CAPABLE)) {
3041 applicableSubtypes.add(subtype);
satok16331c82010-12-20 23:48:46 +09003042 }
3043 }
3044 }
satokc3690562012-01-10 20:14:43 +09003045 if (keyboardSubtype == null) {
satok16331c82010-12-20 23:48:46 +09003046 InputMethodSubtype lastResortKeyboardSubtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003047 res, subtypes, SUBTYPE_MODE_KEYBOARD, systemLocale, true);
satok16331c82010-12-20 23:48:46 +09003048 if (lastResortKeyboardSubtype != null) {
3049 applicableSubtypes.add(lastResortKeyboardSubtype);
3050 }
3051 }
3052 return applicableSubtypes;
3053 }
3054
satok4e4569d2010-11-19 18:45:53 +09003055 /**
3056 * If there are no selected subtypes, tries finding the most applicable one according to the
3057 * given locale.
3058 * @param subtypes this function will search the most applicable subtype in subtypes
3059 * @param mode subtypes will be filtered by mode
3060 * @param locale subtypes will be filtered by locale
satok7599a7f2010-12-22 13:45:23 +09003061 * @param canIgnoreLocaleAsLastResort if this function can't find the most applicable subtype,
3062 * it will return the first subtype matched with mode
satok4e4569d2010-11-19 18:45:53 +09003063 * @return the most applicable subtypeId
3064 */
satokdf31ae62011-01-15 06:19:44 +09003065 private static InputMethodSubtype findLastResortApplicableSubtypeLocked(
3066 Resources res, List<InputMethodSubtype> subtypes, String mode, String locale,
satok7599a7f2010-12-22 13:45:23 +09003067 boolean canIgnoreLocaleAsLastResort) {
satok8fbb1e82010-11-02 23:15:58 +09003068 if (subtypes == null || subtypes.size() == 0) {
satokcd7cd292010-11-20 15:46:23 +09003069 return null;
satok8fbb1e82010-11-02 23:15:58 +09003070 }
satok4e4569d2010-11-19 18:45:53 +09003071 if (TextUtils.isEmpty(locale)) {
satokdf31ae62011-01-15 06:19:44 +09003072 locale = res.getConfiguration().locale.toString();
satok4e4569d2010-11-19 18:45:53 +09003073 }
satok8fbb1e82010-11-02 23:15:58 +09003074 final String language = locale.substring(0, 2);
3075 boolean partialMatchFound = false;
satokcd7cd292010-11-20 15:46:23 +09003076 InputMethodSubtype applicableSubtype = null;
satok7599a7f2010-12-22 13:45:23 +09003077 InputMethodSubtype firstMatchedModeSubtype = null;
satok16331c82010-12-20 23:48:46 +09003078 final int N = subtypes.size();
3079 for (int i = 0; i < N; ++i) {
satokcd7cd292010-11-20 15:46:23 +09003080 InputMethodSubtype subtype = subtypes.get(i);
3081 final String subtypeLocale = subtype.getLocale();
satokd8713432011-01-18 00:55:13 +09003082 // An applicable subtype should match "mode". If mode is null, mode will be ignored,
3083 // and all subtypes with all modes can be candidates.
3084 if (mode == null || subtypes.get(i).getMode().equalsIgnoreCase(mode)) {
satok7599a7f2010-12-22 13:45:23 +09003085 if (firstMatchedModeSubtype == null) {
3086 firstMatchedModeSubtype = subtype;
3087 }
satok9ef02832010-11-04 21:17:48 +09003088 if (locale.equals(subtypeLocale)) {
3089 // Exact match (e.g. system locale is "en_US" and subtype locale is "en_US")
satokcd7cd292010-11-20 15:46:23 +09003090 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003091 break;
3092 } else if (!partialMatchFound && subtypeLocale.startsWith(language)) {
3093 // Partial match (e.g. system locale is "en_US" and subtype locale is "en")
satokcd7cd292010-11-20 15:46:23 +09003094 applicableSubtype = subtype;
satok9ef02832010-11-04 21:17:48 +09003095 partialMatchFound = true;
3096 }
satok8fbb1e82010-11-02 23:15:58 +09003097 }
3098 }
3099
satok7599a7f2010-12-22 13:45:23 +09003100 if (applicableSubtype == null && canIgnoreLocaleAsLastResort) {
3101 return firstMatchedModeSubtype;
satok16331c82010-12-20 23:48:46 +09003102 }
3103
satok8fbb1e82010-11-02 23:15:58 +09003104 // The first subtype applicable to the system locale will be defined as the most applicable
3105 // subtype.
3106 if (DEBUG) {
satok16331c82010-12-20 23:48:46 +09003107 if (applicableSubtype != null) {
3108 Slog.d(TAG, "Applicable InputMethodSubtype was found: "
3109 + applicableSubtype.getMode() + "," + applicableSubtype.getLocale());
3110 }
satok8fbb1e82010-11-02 23:15:58 +09003111 }
satokcd7cd292010-11-20 15:46:23 +09003112 return applicableSubtype;
satok8fbb1e82010-11-02 23:15:58 +09003113 }
3114
satok4e4569d2010-11-19 18:45:53 +09003115 // If there are no selected shortcuts, tries finding the most applicable ones.
3116 private Pair<InputMethodInfo, InputMethodSubtype>
3117 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3118 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3119 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003120 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003121 boolean foundInSystemIME = false;
3122
3123 // Search applicable subtype for each InputMethodInfo
3124 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003125 final String imiId = imi.getId();
3126 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3127 continue;
3128 }
satokcd7cd292010-11-20 15:46:23 +09003129 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003130 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003131 getEnabledInputMethodSubtypeListLocked(imi, true);
satokdf31ae62011-01-15 06:19:44 +09003132 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003133 if (mCurrentSubtype != null) {
satokcd7cd292010-11-20 15:46:23 +09003134 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003135 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003136 }
satokdf31ae62011-01-15 06:19:44 +09003137 // 2. Search by the system locale from enabledSubtypes.
3138 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003139 if (subtype == null) {
3140 subtype = findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003141 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003142 }
satoka86f5e42011-09-02 17:12:42 +09003143 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
3144 getOverridingImplicitlyEnabledSubtypes(imi, mode);
3145 final ArrayList<InputMethodSubtype> subtypesForSearch =
3146 overridingImplicitlyEnabledSubtypes.isEmpty()
3147 ? getSubtypes(imi) : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003148 // 4. Search by the current subtype's locale from all subtypes.
3149 if (subtype == null && mCurrentSubtype != null) {
3150 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003151 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003152 }
3153 // 5. Search by the system locale from all subtypes.
3154 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003155 if (subtype == null) {
satok7599a7f2010-12-22 13:45:23 +09003156 subtype = findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003157 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003158 }
satokcd7cd292010-11-20 15:46:23 +09003159 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003160 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003161 // The current input method is the most applicable IME.
3162 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003163 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003164 break;
satok7599a7f2010-12-22 13:45:23 +09003165 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003166 // The system input method is 2nd applicable IME.
3167 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003168 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003169 if ((imi.getServiceInfo().applicationInfo.flags
3170 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3171 foundInSystemIME = true;
3172 }
satok4e4569d2010-11-19 18:45:53 +09003173 }
3174 }
3175 }
3176 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003177 if (mostApplicableIMI != null) {
3178 Slog.w(TAG, "Most applicable shortcut input method was:"
3179 + mostApplicableIMI.getId());
3180 if (mostApplicableSubtype != null) {
3181 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3182 + "," + mostApplicableSubtype.getMode() + ","
3183 + mostApplicableSubtype.getLocale());
3184 }
3185 }
satok4e4569d2010-11-19 18:45:53 +09003186 }
satokcd7cd292010-11-20 15:46:23 +09003187 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003188 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003189 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003190 } else {
3191 return null;
3192 }
3193 }
3194
satokab751aa2010-09-14 19:17:36 +09003195 /**
3196 * @return Return the current subtype of this input method.
3197 */
satok42c5a162011-05-26 16:46:14 +09003198 @Override
satokab751aa2010-09-14 19:17:36 +09003199 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003200 // TODO: Make this work even for non-current users?
3201 if (!calledFromValidUser()) {
3202 return null;
3203 }
3204 synchronized (mMethodMap) {
3205 return getCurrentInputMethodSubtypeLocked();
3206 }
3207 }
3208
3209 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003210 if (mCurMethodId == null) {
3211 return null;
3212 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003213 final boolean subtypeIsSelected =
3214 mSettings.getSelectedInputMethodSubtypeHashCode() != NOT_A_SUBTYPE_ID;
3215 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3216 if (imi == null || imi.getSubtypeCount() == 0) {
3217 return null;
satok4e4569d2010-11-19 18:45:53 +09003218 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003219 if (!subtypeIsSelected || mCurrentSubtype == null
3220 || !isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3221 int subtypeId = getSelectedInputMethodSubtypeId(mCurMethodId);
3222 if (subtypeId == NOT_A_SUBTYPE_ID) {
3223 // If there are no selected subtypes, the framework will try to find
3224 // the most applicable subtype from explicitly or implicitly enabled
3225 // subtypes.
3226 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
3227 getEnabledInputMethodSubtypeListLocked(imi, true);
3228 // If there is only one explicitly or implicitly enabled subtype,
3229 // just returns it.
3230 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3231 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3232 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
3233 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
3234 mRes, explicitlyOrImplicitlyEnabledSubtypes,
3235 SUBTYPE_MODE_KEYBOARD, null, true);
3236 if (mCurrentSubtype == null) {
satokfdf419e2012-05-08 16:52:08 +09003237 mCurrentSubtype = findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003238 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3239 true);
satok4e4569d2010-11-19 18:45:53 +09003240 }
satok3ef8b292010-11-23 06:06:29 +09003241 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003242 } else {
3243 mCurrentSubtype = getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003244 }
3245 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003246 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003247 }
3248
satokf3db1af2010-11-23 13:34:33 +09003249 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3250 InputMethodSubtype subtype) {
3251 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3252 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3253 } else {
3254 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3255 subtypes.add(subtype);
3256 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3257 }
3258 }
3259
satok4e4569d2010-11-19 18:45:53 +09003260 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003261 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003262 @Override
satok4e4569d2010-11-19 18:45:53 +09003263 public List getShortcutInputMethodsAndSubtypes() {
3264 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003265 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003266 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003267 // If there are no selected shortcut subtypes, the framework will try to find
3268 // the most applicable subtype from all subtypes whose mode is
3269 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003270 Pair<InputMethodInfo, InputMethodSubtype> info =
3271 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
3272 SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003273 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003274 ret.add(info.first);
3275 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003276 }
satok3da92232011-01-11 22:46:30 +09003277 return ret;
satokf3db1af2010-11-23 13:34:33 +09003278 }
satokf3db1af2010-11-23 13:34:33 +09003279 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3280 ret.add(imi);
3281 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3282 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003283 }
3284 }
satokf3db1af2010-11-23 13:34:33 +09003285 return ret;
satok4e4569d2010-11-19 18:45:53 +09003286 }
3287 }
3288
satok42c5a162011-05-26 16:46:14 +09003289 @Override
satokb66d2872010-11-10 01:04:04 +09003290 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003291 // TODO: Make this work even for non-current users?
3292 if (!calledFromValidUser()) {
3293 return false;
3294 }
satokb66d2872010-11-10 01:04:04 +09003295 synchronized (mMethodMap) {
3296 if (subtype != null && mCurMethodId != null) {
3297 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3298 int subtypeId = getSubtypeIdFromHashCode(imi, subtype.hashCode());
3299 if (subtypeId != NOT_A_SUBTYPE_ID) {
3300 setInputMethodLocked(mCurMethodId, subtypeId);
3301 return true;
3302 }
3303 }
3304 return false;
3305 }
3306 }
3307
satok688bd472012-02-09 20:09:17 +09003308 private static class InputMethodAndSubtypeListManager {
3309 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003310 // Used to load label
satok688bd472012-02-09 20:09:17 +09003311 private final PackageManager mPm;
3312 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003313 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003314 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3315 mContext = context;
3316 mPm = context.getPackageManager();
3317 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003318 final Locale locale = context.getResources().getConfiguration().locale;
3319 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003320 }
3321
3322 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3323 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3324 new Comparator<InputMethodInfo>() {
3325 @Override
3326 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3327 if (imi2 == null) return 0;
3328 if (imi1 == null) return 1;
3329 if (mPm == null) {
3330 return imi1.getId().compareTo(imi2.getId());
3331 }
3332 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3333 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3334 return imiId1.toString().compareTo(imiId2.toString());
3335 }
3336 });
3337
3338 public ImeSubtypeListItem getNextInputMethod(
3339 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3340 if (imi == null) {
3341 return null;
3342 }
3343 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3344 if (imList.size() <= 1) {
3345 return null;
3346 }
3347 final int N = imList.size();
3348 final int currentSubtypeId = subtype != null
satokfdf419e2012-05-08 16:52:08 +09003349 ? getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003350 : NOT_A_SUBTYPE_ID;
3351 for (int i = 0; i < N; ++i) {
3352 final ImeSubtypeListItem isli = imList.get(i);
3353 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3354 if (!onlyCurrentIme) {
3355 return imList.get((i + 1) % N);
3356 }
3357 for (int j = 0; j < N - 1; ++j) {
3358 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3359 if (candidate.mImi.equals(imi)) {
3360 return candidate;
3361 }
3362 }
3363 return null;
3364 }
3365 }
3366 return null;
3367 }
3368
3369 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3370 return getSortedInputMethodAndSubtypeList(true, false, false);
3371 }
3372
3373 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3374 boolean inputShown, boolean isScreenLocked) {
3375 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3376 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3377 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3378 if (immis == null || immis.size() == 0) {
3379 return Collections.emptyList();
3380 }
3381 mSortedImmis.clear();
3382 mSortedImmis.putAll(immis);
3383 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3384 if (imi == null) continue;
3385 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3386 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3387 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3388 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3389 }
3390 ArrayList<InputMethodSubtype> subtypes = getSubtypes(imi);
3391 final CharSequence imeLabel = imi.loadLabel(mPm);
3392 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3393 final int subtypeCount = imi.getSubtypeCount();
3394 if (DEBUG) {
3395 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3396 }
3397 for (int j = 0; j < subtypeCount; ++j) {
3398 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3399 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3400 // We show all enabled IMEs and subtypes when an IME is shown.
3401 if (enabledSubtypeSet.contains(subtypeHashCode)
3402 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3403 final CharSequence subtypeLabel =
3404 subtype.overridesImplicitlyEnabledSubtype() ? null
3405 : subtype.getDisplayName(mContext, imi.getPackageName(),
3406 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003407 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3408 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003409
3410 // Removing this subtype from enabledSubtypeSet because we no longer
3411 // need to add an entry of this subtype to imList to avoid duplicated
3412 // entries.
3413 enabledSubtypeSet.remove(subtypeHashCode);
3414 }
3415 }
3416 } else {
satok93d744d2012-05-09 17:14:08 +09003417 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3418 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003419 }
3420 }
satok93d744d2012-05-09 17:14:08 +09003421 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003422 return imList;
3423 }
3424 }
3425
satokd87c2592010-09-29 11:52:06 +09003426 /**
3427 * Utility class for putting and getting settings for InputMethod
3428 * TODO: Move all putters and getters of settings to this class.
3429 */
3430 private static class InputMethodSettings {
3431 // The string for enabled input method is saved as follows:
3432 // example: ("ime0;subtype0;subtype1;subtype2:ime1:ime2;subtype0")
3433 private static final char INPUT_METHOD_SEPARATER = ':';
3434 private static final char INPUT_METHOD_SUBTYPE_SEPARATER = ';';
satok723a27e2010-11-11 14:58:11 +09003435 private final TextUtils.SimpleStringSplitter mInputMethodSplitter =
satokd87c2592010-09-29 11:52:06 +09003436 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SEPARATER);
3437
satok723a27e2010-11-11 14:58:11 +09003438 private final TextUtils.SimpleStringSplitter mSubtypeSplitter =
satokd87c2592010-09-29 11:52:06 +09003439 new TextUtils.SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATER);
3440
satokdf31ae62011-01-15 06:19:44 +09003441 private final Resources mRes;
satokd87c2592010-09-29 11:52:06 +09003442 private final ContentResolver mResolver;
3443 private final HashMap<String, InputMethodInfo> mMethodMap;
3444 private final ArrayList<InputMethodInfo> mMethodList;
3445
3446 private String mEnabledInputMethodsStrCache;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003447 private int mCurrentUserId;
satokd87c2592010-09-29 11:52:06 +09003448
3449 private static void buildEnabledInputMethodsSettingString(
3450 StringBuilder builder, Pair<String, ArrayList<String>> pair) {
3451 String id = pair.first;
3452 ArrayList<String> subtypes = pair.second;
3453 builder.append(id);
satok57c767c2010-11-01 22:34:08 +09003454 // Inputmethod and subtypes are saved in the settings as follows:
3455 // ime0;subtype0;subtype1:ime1;subtype0:ime2:ime3;subtype0;subtype1
3456 for (String subtypeId: subtypes) {
3457 builder.append(INPUT_METHOD_SUBTYPE_SEPARATER).append(subtypeId);
satokd87c2592010-09-29 11:52:06 +09003458 }
3459 }
3460
3461 public InputMethodSettings(
satokdf31ae62011-01-15 06:19:44 +09003462 Resources res, ContentResolver resolver,
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003463 HashMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList,
3464 int userId) {
3465 setCurrentUserId(userId);
satokdf31ae62011-01-15 06:19:44 +09003466 mRes = res;
satokd87c2592010-09-29 11:52:06 +09003467 mResolver = resolver;
3468 mMethodMap = methodMap;
3469 mMethodList = methodList;
3470 }
3471
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003472 public void setCurrentUserId(int userId) {
3473 if (DEBUG) {
3474 Slog.d(TAG, "--- Swtich the current user from " + mCurrentUserId + " to "
3475 + userId + ", new ime = " + getSelectedInputMethod());
3476 }
3477 // IMMS settings are kept per user, so keep track of current user
3478 mCurrentUserId = userId;
3479 }
3480
satokd87c2592010-09-29 11:52:06 +09003481 public List<InputMethodInfo> getEnabledInputMethodListLocked() {
3482 return createEnabledInputMethodListLocked(
3483 getEnabledInputMethodsAndSubtypeListLocked());
3484 }
3485
satok7f35c8c2010-10-07 21:13:11 +09003486 public List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003487 getEnabledInputMethodAndSubtypeHashCodeListLocked() {
3488 return createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003489 getEnabledInputMethodsAndSubtypeListLocked());
3490 }
3491
satok67ddf9c2010-11-17 09:45:54 +09003492 public List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(
3493 InputMethodInfo imi) {
3494 List<Pair<String, ArrayList<String>>> imsList =
3495 getEnabledInputMethodsAndSubtypeListLocked();
3496 ArrayList<InputMethodSubtype> enabledSubtypes =
3497 new ArrayList<InputMethodSubtype>();
satok884ef9a2010-11-18 10:39:46 +09003498 if (imi != null) {
3499 for (Pair<String, ArrayList<String>> imsPair : imsList) {
3500 InputMethodInfo info = mMethodMap.get(imsPair.first);
3501 if (info != null && info.getId().equals(imi.getId())) {
Ken Wakasa586f0512011-01-20 22:31:01 +09003502 final int subtypeCount = info.getSubtypeCount();
3503 for (int i = 0; i < subtypeCount; ++i) {
3504 InputMethodSubtype ims = info.getSubtypeAt(i);
satok884ef9a2010-11-18 10:39:46 +09003505 for (String s: imsPair.second) {
3506 if (String.valueOf(ims.hashCode()).equals(s)) {
3507 enabledSubtypes.add(ims);
3508 }
satok67ddf9c2010-11-17 09:45:54 +09003509 }
3510 }
satok884ef9a2010-11-18 10:39:46 +09003511 break;
satok67ddf9c2010-11-17 09:45:54 +09003512 }
satok67ddf9c2010-11-17 09:45:54 +09003513 }
3514 }
3515 return enabledSubtypes;
3516 }
3517
satokd87c2592010-09-29 11:52:06 +09003518 // At the initial boot, the settings for input methods are not set,
3519 // so we need to enable IME in that case.
3520 public void enableAllIMEsIfThereIsNoEnabledIME() {
3521 if (TextUtils.isEmpty(getEnabledInputMethodsStr())) {
3522 StringBuilder sb = new StringBuilder();
3523 final int N = mMethodList.size();
3524 for (int i = 0; i < N; i++) {
3525 InputMethodInfo imi = mMethodList.get(i);
3526 Slog.i(TAG, "Adding: " + imi.getId());
3527 if (i > 0) sb.append(':');
3528 sb.append(imi.getId());
3529 }
3530 putEnabledInputMethodsStr(sb.toString());
3531 }
3532 }
3533
satokbb4aa062011-01-19 21:40:27 +09003534 private List<Pair<String, ArrayList<String>>> getEnabledInputMethodsAndSubtypeListLocked() {
satokd87c2592010-09-29 11:52:06 +09003535 ArrayList<Pair<String, ArrayList<String>>> imsList
3536 = new ArrayList<Pair<String, ArrayList<String>>>();
3537 final String enabledInputMethodsStr = getEnabledInputMethodsStr();
3538 if (TextUtils.isEmpty(enabledInputMethodsStr)) {
3539 return imsList;
3540 }
satok723a27e2010-11-11 14:58:11 +09003541 mInputMethodSplitter.setString(enabledInputMethodsStr);
3542 while (mInputMethodSplitter.hasNext()) {
3543 String nextImsStr = mInputMethodSplitter.next();
3544 mSubtypeSplitter.setString(nextImsStr);
3545 if (mSubtypeSplitter.hasNext()) {
satokd87c2592010-09-29 11:52:06 +09003546 ArrayList<String> subtypeHashes = new ArrayList<String>();
3547 // The first element is ime id.
satok723a27e2010-11-11 14:58:11 +09003548 String imeId = mSubtypeSplitter.next();
3549 while (mSubtypeSplitter.hasNext()) {
3550 subtypeHashes.add(mSubtypeSplitter.next());
satokd87c2592010-09-29 11:52:06 +09003551 }
3552 imsList.add(new Pair<String, ArrayList<String>>(imeId, subtypeHashes));
3553 }
3554 }
3555 return imsList;
3556 }
3557
3558 public void appendAndPutEnabledInputMethodLocked(String id, boolean reloadInputMethodStr) {
3559 if (reloadInputMethodStr) {
3560 getEnabledInputMethodsStr();
3561 }
3562 if (TextUtils.isEmpty(mEnabledInputMethodsStrCache)) {
3563 // Add in the newly enabled input method.
3564 putEnabledInputMethodsStr(id);
3565 } else {
3566 putEnabledInputMethodsStr(
3567 mEnabledInputMethodsStrCache + INPUT_METHOD_SEPARATER + id);
3568 }
3569 }
3570
3571 /**
3572 * Build and put a string of EnabledInputMethods with removing specified Id.
3573 * @return the specified id was removed or not.
3574 */
3575 public boolean buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3576 StringBuilder builder, List<Pair<String, ArrayList<String>>> imsList, String id) {
3577 boolean isRemoved = false;
3578 boolean needsAppendSeparator = false;
3579 for (Pair<String, ArrayList<String>> ims: imsList) {
3580 String curId = ims.first;
3581 if (curId.equals(id)) {
3582 // We are disabling this input method, and it is
3583 // currently enabled. Skip it to remove from the
3584 // new list.
3585 isRemoved = true;
3586 } else {
3587 if (needsAppendSeparator) {
3588 builder.append(INPUT_METHOD_SEPARATER);
3589 } else {
3590 needsAppendSeparator = true;
3591 }
3592 buildEnabledInputMethodsSettingString(builder, ims);
3593 }
3594 }
3595 if (isRemoved) {
3596 // Update the setting with the new list of input methods.
3597 putEnabledInputMethodsStr(builder.toString());
3598 }
3599 return isRemoved;
3600 }
3601
3602 private List<InputMethodInfo> createEnabledInputMethodListLocked(
3603 List<Pair<String, ArrayList<String>>> imsList) {
3604 final ArrayList<InputMethodInfo> res = new ArrayList<InputMethodInfo>();
3605 for (Pair<String, ArrayList<String>> ims: imsList) {
3606 InputMethodInfo info = mMethodMap.get(ims.first);
3607 if (info != null) {
3608 res.add(info);
3609 }
3610 }
3611 return res;
3612 }
3613
satok7f35c8c2010-10-07 21:13:11 +09003614 private List<Pair<InputMethodInfo, ArrayList<String>>>
satok67ddf9c2010-11-17 09:45:54 +09003615 createEnabledInputMethodAndSubtypeHashCodeListLocked(
satok7f35c8c2010-10-07 21:13:11 +09003616 List<Pair<String, ArrayList<String>>> imsList) {
3617 final ArrayList<Pair<InputMethodInfo, ArrayList<String>>> res
3618 = new ArrayList<Pair<InputMethodInfo, ArrayList<String>>>();
3619 for (Pair<String, ArrayList<String>> ims : imsList) {
3620 InputMethodInfo info = mMethodMap.get(ims.first);
3621 if (info != null) {
3622 res.add(new Pair<InputMethodInfo, ArrayList<String>>(info, ims.second));
3623 }
3624 }
3625 return res;
3626 }
3627
satokd87c2592010-09-29 11:52:06 +09003628 private void putEnabledInputMethodsStr(String str) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003629 Settings.Secure.putStringForUser(
3630 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, str, mCurrentUserId);
satokd87c2592010-09-29 11:52:06 +09003631 mEnabledInputMethodsStrCache = str;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003632 if (DEBUG) {
3633 Slog.d(TAG, "putEnabledInputMethodStr: " + str);
3634 }
satokd87c2592010-09-29 11:52:06 +09003635 }
3636
3637 private String getEnabledInputMethodsStr() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003638 mEnabledInputMethodsStrCache = Settings.Secure.getStringForUser(
3639 mResolver, Settings.Secure.ENABLED_INPUT_METHODS, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003640 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003641 Slog.d(TAG, "getEnabledInputMethodsStr: " + mEnabledInputMethodsStrCache
3642 + ", " + mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003643 }
satokd87c2592010-09-29 11:52:06 +09003644 return mEnabledInputMethodsStrCache;
3645 }
satok723a27e2010-11-11 14:58:11 +09003646
3647 private void saveSubtypeHistory(
3648 List<Pair<String, String>> savedImes, String newImeId, String newSubtypeId) {
3649 StringBuilder builder = new StringBuilder();
3650 boolean isImeAdded = false;
3651 if (!TextUtils.isEmpty(newImeId) && !TextUtils.isEmpty(newSubtypeId)) {
3652 builder.append(newImeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3653 newSubtypeId);
3654 isImeAdded = true;
3655 }
3656 for (Pair<String, String> ime: savedImes) {
3657 String imeId = ime.first;
3658 String subtypeId = ime.second;
3659 if (TextUtils.isEmpty(subtypeId)) {
3660 subtypeId = NOT_A_SUBTYPE_ID_STR;
3661 }
3662 if (isImeAdded) {
3663 builder.append(INPUT_METHOD_SEPARATER);
3664 } else {
3665 isImeAdded = true;
3666 }
3667 builder.append(imeId).append(INPUT_METHOD_SUBTYPE_SEPARATER).append(
3668 subtypeId);
3669 }
3670 // Remove the last INPUT_METHOD_SEPARATER
3671 putSubtypeHistoryStr(builder.toString());
3672 }
3673
3674 public void addSubtypeToHistory(String imeId, String subtypeId) {
3675 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
3676 for (Pair<String, String> ime: subtypeHistory) {
3677 if (ime.first.equals(imeId)) {
3678 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003679 Slog.v(TAG, "Subtype found in the history: " + imeId + ", "
satok723a27e2010-11-11 14:58:11 +09003680 + ime.second);
3681 }
3682 // We should break here
3683 subtypeHistory.remove(ime);
3684 break;
3685 }
3686 }
satokbb4aa062011-01-19 21:40:27 +09003687 if (DEBUG) {
3688 Slog.v(TAG, "Add subtype to the history: " + imeId + ", " + subtypeId);
3689 }
satok723a27e2010-11-11 14:58:11 +09003690 saveSubtypeHistory(subtypeHistory, imeId, subtypeId);
3691 }
3692
3693 private void putSubtypeHistoryStr(String str) {
3694 if (DEBUG) {
3695 Slog.d(TAG, "putSubtypeHistoryStr: " + str);
3696 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003697 Settings.Secure.putStringForUser(
3698 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, str, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003699 }
3700
3701 public Pair<String, String> getLastInputMethodAndSubtypeLocked() {
3702 // Gets the first one from the history
3703 return getLastSubtypeForInputMethodLockedInternal(null);
3704 }
3705
3706 public String getLastSubtypeForInputMethodLocked(String imeId) {
3707 Pair<String, String> ime = getLastSubtypeForInputMethodLockedInternal(imeId);
3708 if (ime != null) {
3709 return ime.second;
3710 } else {
3711 return null;
3712 }
3713 }
3714
3715 private Pair<String, String> getLastSubtypeForInputMethodLockedInternal(String imeId) {
3716 List<Pair<String, ArrayList<String>>> enabledImes =
3717 getEnabledInputMethodsAndSubtypeListLocked();
3718 List<Pair<String, String>> subtypeHistory = loadInputMethodAndSubtypeHistoryLocked();
satok4fc87d62011-05-20 16:13:43 +09003719 for (Pair<String, String> imeAndSubtype : subtypeHistory) {
satok723a27e2010-11-11 14:58:11 +09003720 final String imeInTheHistory = imeAndSubtype.first;
3721 // If imeId is empty, returns the first IME and subtype in the history
3722 if (TextUtils.isEmpty(imeId) || imeInTheHistory.equals(imeId)) {
3723 final String subtypeInTheHistory = imeAndSubtype.second;
satokdf31ae62011-01-15 06:19:44 +09003724 final String subtypeHashCode =
3725 getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(
3726 enabledImes, imeInTheHistory, subtypeInTheHistory);
satok723a27e2010-11-11 14:58:11 +09003727 if (!TextUtils.isEmpty(subtypeHashCode)) {
3728 if (DEBUG) {
satokbb4aa062011-01-19 21:40:27 +09003729 Slog.d(TAG, "Enabled subtype found in the history: " + subtypeHashCode);
satok723a27e2010-11-11 14:58:11 +09003730 }
3731 return new Pair<String, String>(imeInTheHistory, subtypeHashCode);
3732 }
3733 }
3734 }
3735 if (DEBUG) {
3736 Slog.d(TAG, "No enabled IME found in the history");
3737 }
3738 return null;
3739 }
3740
satokdf31ae62011-01-15 06:19:44 +09003741 private String getEnabledSubtypeHashCodeForInputMethodAndSubtypeLocked(List<Pair<String,
satok723a27e2010-11-11 14:58:11 +09003742 ArrayList<String>>> enabledImes, String imeId, String subtypeHashCode) {
3743 for (Pair<String, ArrayList<String>> enabledIme: enabledImes) {
3744 if (enabledIme.first.equals(imeId)) {
satokf6cafb62011-01-17 16:29:02 +09003745 final ArrayList<String> explicitlyEnabledSubtypes = enabledIme.second;
satokfdf419e2012-05-08 16:52:08 +09003746 final InputMethodInfo imi = mMethodMap.get(imeId);
satokf6cafb62011-01-17 16:29:02 +09003747 if (explicitlyEnabledSubtypes.size() == 0) {
3748 // If there are no explicitly enabled subtypes, applicable subtypes are
3749 // enabled implicitly.
satokdf31ae62011-01-15 06:19:44 +09003750 // If IME is enabled and no subtypes are enabled, applicable subtypes
3751 // are enabled implicitly, so needs to treat them to be enabled.
satoka86f5e42011-09-02 17:12:42 +09003752 if (imi != null && imi.getSubtypeCount() > 0) {
satokdf31ae62011-01-15 06:19:44 +09003753 List<InputMethodSubtype> implicitlySelectedSubtypes =
satoka86f5e42011-09-02 17:12:42 +09003754 getImplicitlyApplicableSubtypesLocked(mRes, imi);
satokdf31ae62011-01-15 06:19:44 +09003755 if (implicitlySelectedSubtypes != null) {
3756 final int N = implicitlySelectedSubtypes.size();
3757 for (int i = 0; i < N; ++i) {
3758 final InputMethodSubtype st = implicitlySelectedSubtypes.get(i);
3759 if (String.valueOf(st.hashCode()).equals(subtypeHashCode)) {
3760 return subtypeHashCode;
3761 }
3762 }
3763 }
3764 }
3765 } else {
satokf6cafb62011-01-17 16:29:02 +09003766 for (String s: explicitlyEnabledSubtypes) {
satokdf31ae62011-01-15 06:19:44 +09003767 if (s.equals(subtypeHashCode)) {
3768 // If both imeId and subtypeId are enabled, return subtypeId.
satokfdf419e2012-05-08 16:52:08 +09003769 try {
3770 final int hashCode = Integer.valueOf(subtypeHashCode);
3771 // Check whether the subtype id is valid or not
3772 if (isValidSubtypeId(imi, hashCode)) {
3773 return s;
3774 } else {
3775 return NOT_A_SUBTYPE_ID_STR;
3776 }
3777 } catch (NumberFormatException e) {
3778 return NOT_A_SUBTYPE_ID_STR;
3779 }
satokdf31ae62011-01-15 06:19:44 +09003780 }
satok723a27e2010-11-11 14:58:11 +09003781 }
3782 }
3783 // If imeId was enabled but subtypeId was disabled.
3784 return NOT_A_SUBTYPE_ID_STR;
3785 }
3786 }
3787 // If both imeId and subtypeId are disabled, return null
3788 return null;
3789 }
3790
3791 private List<Pair<String, String>> loadInputMethodAndSubtypeHistoryLocked() {
3792 ArrayList<Pair<String, String>> imsList = new ArrayList<Pair<String, String>>();
3793 final String subtypeHistoryStr = getSubtypeHistoryStr();
3794 if (TextUtils.isEmpty(subtypeHistoryStr)) {
3795 return imsList;
3796 }
3797 mInputMethodSplitter.setString(subtypeHistoryStr);
3798 while (mInputMethodSplitter.hasNext()) {
3799 String nextImsStr = mInputMethodSplitter.next();
3800 mSubtypeSplitter.setString(nextImsStr);
3801 if (mSubtypeSplitter.hasNext()) {
3802 String subtypeId = NOT_A_SUBTYPE_ID_STR;
3803 // The first element is ime id.
3804 String imeId = mSubtypeSplitter.next();
3805 while (mSubtypeSplitter.hasNext()) {
3806 subtypeId = mSubtypeSplitter.next();
3807 break;
3808 }
3809 imsList.add(new Pair<String, String>(imeId, subtypeId));
3810 }
3811 }
3812 return imsList;
3813 }
3814
3815 private String getSubtypeHistoryStr() {
3816 if (DEBUG) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003817 Slog.d(TAG, "getSubtypeHistoryStr: " + Settings.Secure.getStringForUser(
3818 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId));
satok723a27e2010-11-11 14:58:11 +09003819 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003820 return Settings.Secure.getStringForUser(
3821 mResolver, Settings.Secure.INPUT_METHODS_SUBTYPE_HISTORY, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003822 }
3823
3824 public void putSelectedInputMethod(String imeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003825 if (DEBUG) {
3826 Slog.d(TAG, "putSelectedInputMethodStr: " + imeId + ", "
3827 + mCurrentUserId);
3828 }
3829 Settings.Secure.putStringForUser(
3830 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, imeId, mCurrentUserId);
satok723a27e2010-11-11 14:58:11 +09003831 }
3832
3833 public void putSelectedSubtype(int subtypeId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003834 if (DEBUG) {
3835 Slog.d(TAG, "putSelectedInputMethodSubtypeStr: " + subtypeId + ", "
3836 + mCurrentUserId);
3837 }
3838 Settings.Secure.putIntForUser(mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
3839 subtypeId, mCurrentUserId);
3840 }
3841
3842 public String getDisabledSystemInputMethods() {
3843 return Settings.Secure.getStringForUser(
3844 mResolver, Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, mCurrentUserId);
3845 }
3846
3847 public String getSelectedInputMethod() {
3848 if (DEBUG) {
3849 Slog.d(TAG, "getSelectedInputMethodStr: " + Settings.Secure.getStringForUser(
3850 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId)
3851 + ", " + mCurrentUserId);
3852 }
3853 return Settings.Secure.getStringForUser(
3854 mResolver, Settings.Secure.DEFAULT_INPUT_METHOD, mCurrentUserId);
3855 }
3856
3857 public int getSelectedInputMethodSubtypeHashCode() {
3858 try {
3859 return Settings.Secure.getIntForUser(
3860 mResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, mCurrentUserId);
3861 } catch (SettingNotFoundException e) {
3862 return NOT_A_SUBTYPE_ID;
3863 }
3864 }
3865
3866 public int getCurrentUserId() {
3867 return mCurrentUserId;
satok723a27e2010-11-11 14:58:11 +09003868 }
satokd87c2592010-09-29 11:52:06 +09003869 }
3870
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003871 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003872 private static class InputMethodFileManager {
3873 private static final String SYSTEM_PATH = "system";
3874 private static final String INPUT_METHOD_PATH = "inputmethod";
3875 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3876 private static final String NODE_SUBTYPES = "subtypes";
3877 private static final String NODE_SUBTYPE = "subtype";
3878 private static final String NODE_IMI = "imi";
3879 private static final String ATTR_ID = "id";
3880 private static final String ATTR_LABEL = "label";
3881 private static final String ATTR_ICON = "icon";
3882 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3883 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3884 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3885 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3886 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3887 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003888 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003889 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003890 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003891 if (methodMap == null) {
3892 throw new NullPointerException("methodMap is null");
3893 }
3894 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003895 final File systemDir = userId == UserHandle.USER_OWNER
3896 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3897 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003898 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3899 if (!inputMethodDir.mkdirs()) {
3900 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3901 }
3902 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3903 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3904 if (!subtypeFile.exists()) {
3905 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003906 writeAdditionalInputMethodSubtypes(
3907 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003908 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003909 readAdditionalInputMethodSubtypes(
3910 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003911 }
3912 }
3913
3914 private void deleteAllInputMethodSubtypes(String imiId) {
3915 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003916 mAdditionalSubtypesMap.remove(imiId);
3917 writeAdditionalInputMethodSubtypes(
3918 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003919 }
3920 }
3921
3922 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003923 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003924 synchronized (mMethodMap) {
3925 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3926 final int N = additionalSubtypes.length;
3927 for (int i = 0; i < N; ++i) {
3928 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003929 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003930 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003931 } else {
3932 Slog.w(TAG, "Duplicated subtype definition found: "
3933 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003934 }
3935 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003936 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3937 writeAdditionalInputMethodSubtypes(
3938 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003939 }
3940 }
3941
3942 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3943 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003944 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003945 }
3946 }
3947
3948 private static void writeAdditionalInputMethodSubtypes(
3949 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3950 HashMap<String, InputMethodInfo> methodMap) {
3951 // Safety net for the case that this function is called before methodMap is set.
3952 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3953 FileOutputStream fos = null;
3954 try {
3955 fos = subtypesFile.startWrite();
3956 final XmlSerializer out = new FastXmlSerializer();
3957 out.setOutput(fos, "utf-8");
3958 out.startDocument(null, true);
3959 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3960 out.startTag(null, NODE_SUBTYPES);
3961 for (String imiId : allSubtypes.keySet()) {
3962 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3963 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3964 continue;
3965 }
3966 out.startTag(null, NODE_IMI);
3967 out.attribute(null, ATTR_ID, imiId);
3968 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3969 final int N = subtypesList.size();
3970 for (int i = 0; i < N; ++i) {
3971 final InputMethodSubtype subtype = subtypesList.get(i);
3972 out.startTag(null, NODE_SUBTYPE);
3973 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3974 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3975 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3976 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3977 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3978 out.attribute(null, ATTR_IS_AUXILIARY,
3979 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3980 out.endTag(null, NODE_SUBTYPE);
3981 }
3982 out.endTag(null, NODE_IMI);
3983 }
3984 out.endTag(null, NODE_SUBTYPES);
3985 out.endDocument();
3986 subtypesFile.finishWrite(fos);
3987 } catch (java.io.IOException e) {
3988 Slog.w(TAG, "Error writing subtypes", e);
3989 if (fos != null) {
3990 subtypesFile.failWrite(fos);
3991 }
3992 }
3993 }
3994
3995 private static void readAdditionalInputMethodSubtypes(
3996 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3997 if (allSubtypes == null || subtypesFile == null) return;
3998 allSubtypes.clear();
3999 FileInputStream fis = null;
4000 try {
4001 fis = subtypesFile.openRead();
4002 final XmlPullParser parser = Xml.newPullParser();
4003 parser.setInput(fis, null);
4004 int type = parser.getEventType();
4005 // Skip parsing until START_TAG
4006 while ((type = parser.next()) != XmlPullParser.START_TAG
4007 && type != XmlPullParser.END_DOCUMENT) {}
4008 String firstNodeName = parser.getName();
4009 if (!NODE_SUBTYPES.equals(firstNodeName)) {
4010 throw new XmlPullParserException("Xml doesn't start with subtypes");
4011 }
4012 final int depth =parser.getDepth();
4013 String currentImiId = null;
4014 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
4015 while (((type = parser.next()) != XmlPullParser.END_TAG
4016 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
4017 if (type != XmlPullParser.START_TAG)
4018 continue;
4019 final String nodeName = parser.getName();
4020 if (NODE_IMI.equals(nodeName)) {
4021 currentImiId = parser.getAttributeValue(null, ATTR_ID);
4022 if (TextUtils.isEmpty(currentImiId)) {
4023 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
4024 continue;
4025 }
4026 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
4027 allSubtypes.put(currentImiId, tempSubtypesArray);
4028 } else if (NODE_SUBTYPE.equals(nodeName)) {
4029 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
4030 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
4031 continue;
4032 }
4033 final int icon = Integer.valueOf(
4034 parser.getAttributeValue(null, ATTR_ICON));
4035 final int label = Integer.valueOf(
4036 parser.getAttributeValue(null, ATTR_LABEL));
4037 final String imeSubtypeLocale =
4038 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
4039 final String imeSubtypeMode =
4040 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
4041 final String imeSubtypeExtraValue =
4042 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09004043 final boolean isAuxiliary = "1".equals(String.valueOf(
4044 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09004045 final InputMethodSubtype subtype =
4046 new InputMethodSubtype(label, icon, imeSubtypeLocale,
4047 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
4048 tempSubtypesArray.add(subtype);
4049 }
4050 }
4051 } catch (XmlPullParserException e) {
4052 Slog.w(TAG, "Error reading subtypes: " + e);
4053 return;
4054 } catch (java.io.IOException e) {
4055 Slog.w(TAG, "Error reading subtypes: " + e);
4056 return;
4057 } catch (NumberFormatException e) {
4058 Slog.w(TAG, "Error reading subtypes: " + e);
4059 return;
4060 } finally {
4061 if (fis != null) {
4062 try {
4063 fis.close();
4064 } catch (java.io.IOException e1) {
4065 Slog.w(TAG, "Failed to close.");
4066 }
4067 }
4068 }
4069 }
4070 }
4071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004073 // Utilities for debug
4074 private static String getStackTrace() {
4075 final StringBuilder sb = new StringBuilder();
4076 try {
4077 throw new RuntimeException();
4078 } catch (RuntimeException e) {
4079 final StackTraceElement[] frames = e.getStackTrace();
4080 // Start at 1 because the first frame is here and we don't care about it
4081 for (int j = 1; j < frames.length; ++j) {
4082 sb.append(frames[j].toString() + "\n");
4083 }
4084 }
4085 return sb.toString();
4086 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 @Override
4089 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
4090 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4091 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
4094 + Binder.getCallingPid()
4095 + ", uid=" + Binder.getCallingUid());
4096 return;
4097 }
4098
4099 IInputMethod method;
4100 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004104 synchronized (mMethodMap) {
4105 p.println("Current Input Method Manager state:");
4106 int N = mMethodList.size();
4107 p.println(" Input Methods:");
4108 for (int i=0; i<N; i++) {
4109 InputMethodInfo info = mMethodList.get(i);
4110 p.println(" InputMethod #" + i + ":");
4111 info.dump(p, " ");
4112 }
4113 p.println(" Clients:");
4114 for (ClientState ci : mClients.values()) {
4115 p.println(" Client " + ci + ":");
4116 p.println(" client=" + ci.client);
4117 p.println(" inputContext=" + ci.inputContext);
4118 p.println(" sessionRequested=" + ci.sessionRequested);
4119 p.println(" curSession=" + ci.curSession);
4120 }
The Android Open Source Project10592532009-03-18 17:39:46 -07004121 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004123 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
4124 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
4126 + " mBoundToMethod=" + mBoundToMethod);
4127 p.println(" mCurToken=" + mCurToken);
4128 p.println(" mCurIntent=" + mCurIntent);
4129 method = mCurMethod;
4130 p.println(" mCurMethod=" + mCurMethod);
4131 p.println(" mEnabledSession=" + mEnabledSession);
4132 p.println(" mShowRequested=" + mShowRequested
4133 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
4134 + " mShowForced=" + mShowForced
4135 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07004136 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004138
Jeff Brownb88102f2010-09-08 11:49:43 -07004139 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 pw.flush();
4142 try {
4143 client.client.asBinder().dump(fd, args);
4144 } catch (RemoteException e) {
4145 p.println("Input method client dead: " + e);
4146 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004147 } else {
4148 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004150
Jeff Brownb88102f2010-09-08 11:49:43 -07004151 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 pw.flush();
4154 try {
4155 method.asBinder().dump(fd, args);
4156 } catch (RemoteException e) {
4157 p.println("Input method service dead: " + e);
4158 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004159 } else {
4160 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 }
4162 }
4163}