blob: c91685710a5095739a1c5e53a79934515ac7dc14 [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;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090019import com.android.internal.inputmethod.InputMethodUtils;
20import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070022import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090023import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import com.android.internal.view.IInputContext;
25import com.android.internal.view.IInputMethod;
Michael Wright52a53522013-03-14 10:59:38 -070026import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import com.android.internal.view.IInputMethodClient;
28import com.android.internal.view.IInputMethodManager;
29import com.android.internal.view.IInputMethodSession;
30import com.android.internal.view.InputBindResult;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080031import com.android.server.EventLogTags;
satok01038492012-04-09 21:08:27 +090032import com.android.server.wm.WindowManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
satoke7c6998e2011-06-03 17:57:59 +090034import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38import android.app.ActivityManagerNative;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090039import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.app.AlertDialog;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090041import android.app.IUserSwitchObserver;
satokf90a33e2011-07-19 11:55:52 +090042import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090043import android.app.Notification;
44import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070045import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090046import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.ComponentName;
48import android.content.ContentResolver;
49import android.content.Context;
50import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.DialogInterface.OnCancelListener;
52import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090053import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070055import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090056import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.content.pm.PackageManager;
58import android.content.pm.ResolveInfo;
59import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070060import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.content.res.Resources;
62import android.content.res.TypedArray;
63import android.database.ContentObserver;
Joe Onorato857fd9b2011-01-27 15:08:35 -080064import android.inputmethodservice.InputMethodService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Binder;
satoke7c6998e2011-06-03 17:57:59 +090066import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Handler;
68import android.os.IBinder;
69import android.os.IInterface;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090070import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Message;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090072import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Parcel;
74import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080075import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.ServiceManager;
77import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090078import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.provider.Settings;
80import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090081import android.text.style.SuggestionSpan;
Dianne Hackborn39606a02012-07-31 17:54:35 -070082import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090084import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090085import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.PrintWriterPrinter;
87import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +090088import android.util.Slog;
89import android.util.Xml;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -070091import android.view.InputChannel;
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
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900157 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900158 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900159
satok4e4569d2010-11-19 18:45:53 +0900160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800162 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900164 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 final IWindowManager mIWindowManager;
167 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700168 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900169 private InputMethodFileManager mFileManager;
170 private InputMethodAndSubtypeListManager mImListManager;
satok01038492012-04-09 21:08:27 +0900171 private final HardKeyboardListener mHardKeyboardListener;
172 private final WindowManagerService mWindowManagerService;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
Jeff Brownc28867a2013-03-26 15:42:39 -0700174 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 // All known input methods. mMethodMap also serves as the global
177 // lock for this class.
satokd87c2592010-09-29 11:52:06 +0900178 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<InputMethodInfo>();
179 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<String, InputMethodInfo>();
satokf9f01002011-05-19 21:31:50 +0900180 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
181 new LruCache<SuggestionSpan, InputMethodInfo>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800182
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700183 // Used to bring IME service up to visible adjustment while it is being shown.
184 final ServiceConnection mVisibleConnection = new ServiceConnection() {
185 @Override public void onServiceConnected(ComponentName name, IBinder service) {
186 }
187
188 @Override public void onServiceDisconnected(ComponentName name) {
189 }
190 };
191 boolean mVisibleBound = false;
192
satok7cfc0ed2011-06-20 21:29:36 +0900193 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700194 private NotificationManager mNotificationManager;
195 private KeyguardManager mKeyguardManager;
196 private StatusBarManagerService mStatusBar;
197 private Notification mImeSwitcherNotification;
198 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900199 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900200 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900201 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 class SessionState {
204 final ClientState client;
205 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700206
207 IInputMethodSession session;
208 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 @Override
211 public String toString() {
212 return "SessionState{uid " + client.uid + " pid " + client.pid
213 + " method " + Integer.toHexString(
214 System.identityHashCode(method))
215 + " session " + Integer.toHexString(
216 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700217 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 + "}";
219 }
220
221 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700222 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 client = _client;
224 method = _method;
225 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700226 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 }
228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
Jeff Brownc28867a2013-03-26 15:42:39 -0700230 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 final IInputMethodClient client;
232 final IInputContext inputContext;
233 final int uid;
234 final int pid;
235 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 boolean sessionRequested;
238 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 @Override
241 public String toString() {
242 return "ClientState{" + Integer.toHexString(
243 System.identityHashCode(this)) + " uid " + uid
244 + " pid " + pid + "}";
245 }
246
247 ClientState(IInputMethodClient _client, IInputContext _inputContext,
248 int _uid, int _pid) {
249 client = _client;
250 inputContext = _inputContext;
251 uid = _uid;
252 pid = _pid;
253 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
254 }
255 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 final HashMap<IBinder, ClientState> mClients
258 = new HashMap<IBinder, ClientState>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700261 * Set once the system is ready to run third party code.
262 */
263 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800264
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * Id of the currently selected input method.
267 */
268 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * The current binding sequence number, incremented every time there is
272 * a new bind performed.
273 */
274 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 /**
277 * The client that is currently bound to an input method.
278 */
279 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700282 * The last window token that gained focus.
283 */
284 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 * The input context last provided by the current client.
288 */
289 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 /**
292 * The attributes last provided by the current client.
293 */
294 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 /**
297 * The input method ID of the input method service that we are currently
298 * connected to or in the process of connecting to.
299 */
300 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 /**
satokab751aa2010-09-14 19:17:36 +0900303 * The current subtype of the current input method.
304 */
305 private InputMethodSubtype mCurrentSubtype;
306
satok4e4569d2010-11-19 18:45:53 +0900307 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900308 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
309 mShortcutInputMethodsAndSubtypes =
310 new HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>();
satokab751aa2010-09-14 19:17:36 +0900311
312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * Set to true if our ServiceConnection is currently actively bound to
314 * a service (whether or not we have gotten its IBinder back yet).
315 */
316 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * Set if the client has asked for the input method to be shown.
320 */
321 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * Set if we were explicitly told to show the input method.
325 */
326 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /**
329 * Set if we were forced to be shown.
330 */
331 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 /**
334 * Set if we last told the input method to show itself.
335 */
336 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 /**
339 * The Intent used to connect to the current input method.
340 */
341 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 /**
344 * The token we have made for the currently active input method, to
345 * identify it in the future.
346 */
347 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * If non-null, this is the input method service we are currently connected
351 * to.
352 */
353 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 /**
356 * Time that we last initiated a bind to the input method, to determine
357 * if we should try to disconnect and reconnect to it.
358 */
359 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
362 * Have we called mCurMethod.bindInput()?
363 */
364 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /**
367 * Currently enabled session. Only touched by service thread, not
368 * protected by a lock.
369 */
370 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
373 * True if the screen is on. The value is true initially.
374 */
375 boolean mScreenOn = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
Joe Onorato857fd9b2011-01-27 15:08:35 -0800377 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
378 int mImeWindowVis;
379
Ken Wakasa05dbb652011-08-22 15:22:43 +0900380 private AlertDialog.Builder mDialogBuilder;
381 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900382 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900383 private InputMethodInfo[] mIms;
384 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900385 private Locale mLastSystemLocale;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900386 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
387 private final IPackageManager mIPackageManager;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500388 private boolean mInputBoundToKeyguard;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 class SettingsObserver extends ContentObserver {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800391 String mLastEnabled = "";
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 SettingsObserver(Handler handler) {
394 super(handler);
395 ContentResolver resolver = mContext.getContentResolver();
396 resolver.registerContentObserver(Settings.Secure.getUriFor(
397 Settings.Secure.DEFAULT_INPUT_METHOD), false, this);
satokab751aa2010-09-14 19:17:36 +0900398 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokb6109bb2011-02-03 22:24:54 +0900399 Settings.Secure.ENABLED_INPUT_METHODS), false, this);
400 resolver.registerContentObserver(Settings.Secure.getUriFor(
satokab751aa2010-09-14 19:17:36 +0900401 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 @Override public void onChange(boolean selfChange) {
405 synchronized (mMethodMap) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800406 boolean enabledChanged = false;
407 String newEnabled = mSettings.getEnabledInputMethodsStr();
408 if (!mLastEnabled.equals(newEnabled)) {
409 mLastEnabled = newEnabled;
410 enabledChanged = true;
411 }
412 updateFromSettingsLocked(enabledChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414 }
415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900417 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
418 private void updateActive() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 // Inform the current client of the change in active status
Dianne Hackborna6e41342012-05-22 16:30:34 -0700420 if (mCurClient != null && mCurClient.client != null) {
421 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
422 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900425
426 @Override
427 public void onReceive(Context context, Intent intent) {
428 final String action = intent.getAction();
429 if (Intent.ACTION_SCREEN_ON.equals(action)) {
430 mScreenOn = true;
431 refreshImeWindowVisibilityLocked();
432 updateActive();
433 return;
434 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
435 mScreenOn = false;
436 setImeWindowVisibilityStatusHiddenLocked();
437 updateActive();
438 return;
439 } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
440 hideInputMethodMenu();
441 // No need to updateActive
442 return;
443 } else {
444 Slog.w(TAG, "Unexpected intent " + intent);
445 }
446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800448
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800449 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900450 private boolean isChangingPackagesOfCurrentUser() {
451 final int userId = getChangingUserId();
452 final boolean retval = userId == mSettings.getCurrentUserId();
453 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900454 if (!retval) {
455 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
456 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900457 }
458 return retval;
459 }
460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800462 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900463 if (!isChangingPackagesOfCurrentUser()) {
464 return false;
465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900467 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 final int N = mMethodList.size();
469 if (curInputMethodId != null) {
470 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800471 InputMethodInfo imi = mMethodList.get(i);
472 if (imi.getId().equals(curInputMethodId)) {
473 for (String pkg : packages) {
474 if (imi.getPackageName().equals(pkg)) {
475 if (!doit) {
476 return true;
477 }
satok723a27e2010-11-11 14:58:11 +0900478 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800479 chooseNewDefaultIMELocked();
480 return true;
481 }
482 }
483 }
484 }
485 }
486 }
487 return false;
488 }
489
490 @Override
491 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900492 if (!isChangingPackagesOfCurrentUser()) {
493 return;
494 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800495 synchronized (mMethodMap) {
496 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900497 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800498 final int N = mMethodList.size();
499 if (curInputMethodId != null) {
500 for (int i=0; i<N; i++) {
501 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900502 final String imiId = imi.getId();
503 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800504 curIm = imi;
505 }
satoke7c6998e2011-06-03 17:57:59 +0900506
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800507 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900508 if (isPackageModified(imi.getPackageName())) {
509 mFileManager.deleteAllInputMethodSubtypes(imiId);
510 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 if (change == PACKAGE_TEMPORARY_CHANGE
512 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800513 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514 + imi.getComponent());
515 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517 }
518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800519
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900520 buildInputMethodListLocked(
521 mMethodList, mMethodMap, false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800524
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800525 if (curIm != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800526 int change = isPackageDisappearing(curIm.getPackageName());
527 if (change == PACKAGE_TEMPORARY_CHANGE
528 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800529 ServiceInfo si = null;
530 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900531 si = mIPackageManager.getServiceInfo(
532 curIm.getComponent(), 0, mSettings.getCurrentUserId());
533 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800534 }
535 if (si == null) {
536 // Uh oh, current input method is no longer around!
537 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
satok15452a42011-10-28 17:58:28 +0900539 setImeWindowVisibilityStatusHiddenLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800541 changed = true;
542 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800543 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900544 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800545 }
546 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800547 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800548 }
satokab751aa2010-09-14 19:17:36 +0900549
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 if (curIm == null) {
551 // We currently don't have a default input method... is
552 // one now available?
553 changed = chooseNewDefaultIMELocked();
554 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800555
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800556 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800557 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 }
560 }
561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800562
Jeff Brownc28867a2013-03-26 15:42:39 -0700563 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900564 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700565 private final IInputMethod mMethod;
566 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800567
Jeff Brownc28867a2013-03-26 15:42:39 -0700568 MethodCallback(InputMethodManagerService imms, IInputMethod method,
569 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900570 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700571 mMethod = method;
572 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800574
satoke7c6998e2011-06-03 17:57:59 +0900575 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700576 public void sessionCreated(IInputMethodSession session) {
577 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
satok01038492012-04-09 21:08:27 +0900581 private class HardKeyboardListener
582 implements WindowManagerService.OnHardKeyboardStatusChangeListener {
583 @Override
584 public void onHardKeyboardStatusChange(boolean available, boolean enabled) {
585 mHandler.sendMessage(mHandler.obtainMessage(
586 MSG_HARD_KEYBOARD_SWITCH_CHANGED, available ? 1 : 0, enabled ? 1 : 0));
587 }
588
589 public void handleHardKeyboardStatusChange(boolean available, boolean enabled) {
590 if (DEBUG) {
591 Slog.w(TAG, "HardKeyboardStatusChanged: available = " + available + ", enabled = "
592 + enabled);
593 }
594 synchronized(mMethodMap) {
595 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
596 && mSwitchingDialog.isShowing()) {
597 mSwitchingDialogTitleView.findViewById(
598 com.android.internal.R.id.hard_keyboard_section).setVisibility(
599 available ? View.VISIBLE : View.GONE);
600 }
601 }
602 }
603 }
604
605 public InputMethodManagerService(Context context, WindowManagerService windowManager) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900606 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800608 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 mHandler = new Handler(this);
610 mIWindowManager = IWindowManager.Stub.asInterface(
611 ServiceManager.getService(Context.WINDOW_SERVICE));
Mita Yuned218c72012-12-06 17:18:25 -0800612 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900613 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public void executeMessage(Message msg) {
615 handleMessage(msg);
616 }
Mita Yuned218c72012-12-06 17:18:25 -0800617 }, true /*asyncHandler*/);
satok01038492012-04-09 21:08:27 +0900618 mWindowManagerService = windowManager;
619 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700620 mHasFeature = context.getPackageManager().hasSystemFeature(
621 PackageManager.FEATURE_INPUT_METHODS);
satok7cfc0ed2011-06-20 21:29:36 +0900622
satok7cfc0ed2011-06-20 21:29:36 +0900623 mImeSwitcherNotification = new Notification();
624 mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
625 mImeSwitcherNotification.when = 0;
626 mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
627 mImeSwitcherNotification.tickerText = null;
628 mImeSwitcherNotification.defaults = 0; // please be quiet
629 mImeSwitcherNotification.sound = null;
630 mImeSwitcherNotification.vibrate = null;
Daniel Sandler590d5152012-06-14 16:10:13 -0400631
632 // Tag this notification specially so SystemUI knows it's important
633 mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
634
satok7cfc0ed2011-06-20 21:29:36 +0900635 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900636 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900637
638 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900639
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900640 final IntentFilter broadcastFilter = new IntentFilter();
641 broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
642 broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
643 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
644 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
satok7cfc0ed2011-06-20 21:29:36 +0900646 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900647 int userId = 0;
648 try {
649 ActivityManagerNative.getDefault().registerUserSwitchObserver(
650 new IUserSwitchObserver.Stub() {
651 @Override
652 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900653 synchronized(mMethodMap) {
654 switchUserLocked(newUserId);
655 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900656 if (reply != null) {
657 try {
658 reply.sendResult(null);
659 } catch (RemoteException e) {
660 }
661 }
662 }
663
664 @Override
665 public void onUserSwitchComplete(int newUserId) throws RemoteException {
666 }
667 });
668 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
669 } catch (RemoteException e) {
670 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
671 }
satok81f8b7c2012-12-04 20:42:56 +0900672 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900673
satokd87c2592010-09-29 11:52:06 +0900674 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900675 mSettings = new InputMethodSettings(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900676 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900677 mFileManager = new InputMethodFileManager(mMethodMap, userId);
678 mImListManager = new InputMethodAndSubtypeListManager(context, this);
satok0a1bcf42012-05-16 19:26:31 +0900679
680 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900681 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900682 if (DEBUG) {
683 Slog.d(TAG, "Initial default ime = " + defaultImiId);
684 }
satok0a1bcf42012-05-16 19:26:31 +0900685 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
686
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900687 buildInputMethodListLocked(mMethodList, mMethodMap,
688 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satokd87c2592010-09-29 11:52:06 +0900689 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690
satok0a1bcf42012-05-16 19:26:31 +0900691 if (!mImeSelectedOnBoot) {
692 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
satok5b927c432012-05-01 20:09:34 +0900693 resetDefaultImeLocked(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 mSettingsObserver = new SettingsObserver(mHandler);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800697 updateFromSettingsLocked(true);
satok5b927c432012-05-01 20:09:34 +0900698
699 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
700 // according to the new system locale.
701 final IntentFilter filter = new IntentFilter();
702 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
703 mContext.registerReceiver(
704 new BroadcastReceiver() {
705 @Override
706 public void onReceive(Context context, Intent intent) {
707 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900708 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900709 }
710 }
711 }, filter);
712 }
713
satok5b927c432012-05-01 20:09:34 +0900714 private void resetDefaultImeLocked(Context context) {
715 // Do not reset the default (current) IME when it is a 3rd-party IME
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900716 if (mCurMethodId != null
717 && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900718 return;
719 }
720
721 InputMethodInfo defIm = null;
722 for (InputMethodInfo imi : mMethodList) {
723 if (defIm == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900724 if (InputMethodUtils.isValidSystemDefaultIme(
725 mSystemReady, imi, context)) {
satok5b927c432012-05-01 20:09:34 +0900726 defIm = imi;
727 Slog.i(TAG, "Selected default: " + imi.getId());
728 }
729 }
730 }
731 if (defIm == null && mMethodList.size() > 0) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900732 defIm = InputMethodUtils.getMostApplicableDefaultIME(
733 mSettings.getEnabledInputMethodListLocked());
satok5b927c432012-05-01 20:09:34 +0900734 Slog.i(TAG, "No default found, using " + defIm.getId());
735 }
736 if (defIm != null) {
737 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
738 }
739 }
740
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900741 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
742 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900743 if (!mSystemReady) {
744 // not system ready
745 return;
746 }
747 final Locale newLocale = mRes.getConfiguration().locale;
748 if (!updateOnlyWhenLocaleChanged
749 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
750 if (!updateOnlyWhenLocaleChanged) {
751 hideCurrentInputLocked(0, null);
752 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -0700753 unbindCurrentMethodLocked(true, false);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900754 }
755 if (DEBUG) {
756 Slog.i(TAG, "Locale has been changed to " + newLocale);
757 }
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900758 // InputMethodAndSubtypeListManager should be reset when the locale is changed.
759 mImListManager = new InputMethodAndSubtypeListManager(mContext, this);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900760 buildInputMethodListLocked(mMethodList, mMethodMap, resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900761 if (!updateOnlyWhenLocaleChanged) {
762 final String selectedImiId = mSettings.getSelectedInputMethod();
763 if (TextUtils.isEmpty(selectedImiId)) {
764 // This is the first time of the user switch and
765 // set the current ime to the proper one.
766 resetDefaultImeLocked(mContext);
767 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900768 } else {
769 // If the locale is changed, needs to reset the default ime
770 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900771 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800772 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900773 mLastSystemLocale = newLocale;
774 if (!updateOnlyWhenLocaleChanged) {
775 try {
776 startInputInnerLocked();
777 } catch (RuntimeException e) {
778 Slog.w(TAG, "Unexpected exception", e);
779 }
780 }
781 }
782 }
783
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900784 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900785 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
786 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900787 }
788
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900789 private void switchUserLocked(int newUserId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900790 mSettings.setCurrentUserId(newUserId);
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900791 // InputMethodFileManager should be reset when the user is changed
792 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900793 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900794 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900795 if (DEBUG) {
796 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
797 }
798 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900799 initialUserSwitch /* needsToResetDefaultIme */);
800 if (initialUserSwitch) {
801 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
802 mSettings.getEnabledInputMethodListLocked());
803 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900804 }
805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 @Override
807 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
808 throws RemoteException {
809 try {
810 return super.onTransact(code, data, reply, flags);
811 } catch (RuntimeException e) {
812 // The input method manager only throws security exceptions, so let's
813 // log all others.
814 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800815 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817 throw e;
818 }
819 }
820
Dianne Hackborn661cd522011-08-22 00:26:20 -0700821 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700822 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900823 if (DEBUG) {
824 Slog.d(TAG, "--- systemReady");
825 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700826 if (!mSystemReady) {
827 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900828 mKeyguardManager =
829 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700830 mNotificationManager = (NotificationManager)
831 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
832 mStatusBar = statusBar;
833 statusBar.setIconVisibility("ime", false);
834 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900835 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
836 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900837 if (mShowOngoingImeSwitcherForPhones) {
838 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
839 mHardKeyboardListener);
840 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900841 buildInputMethodListLocked(mMethodList, mMethodMap,
842 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900843 if (!mImeSelectedOnBoot) {
844 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900845 resetStateIfCurrentLocaleChangedLocked();
846 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
847 mContext.getPackageManager(),
848 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900849 }
850 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700851 try {
852 startInputInnerLocked();
853 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800854 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700855 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700856 }
857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800859
satok15452a42011-10-28 17:58:28 +0900860 private void setImeWindowVisibilityStatusHiddenLocked() {
861 mImeWindowVis = 0;
862 updateImeWindowStatusLocked();
863 }
864
satok3afd6c02011-11-18 08:38:19 +0900865 private void refreshImeWindowVisibilityLocked() {
866 final Configuration conf = mRes.getConfiguration();
867 final boolean haveHardKeyboard = conf.keyboard
868 != Configuration.KEYBOARD_NOKEYS;
869 final boolean hardKeyShown = haveHardKeyboard
870 && conf.hardKeyboardHidden
871 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500872 final boolean isScreenLocked =
873 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
874 final boolean isScreenSecurelyLocked =
875 isScreenLocked && mKeyguardManager.isKeyguardSecure();
876 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
877 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900878 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
879 updateImeWindowStatusLocked();
880 }
881
satok15452a42011-10-28 17:58:28 +0900882 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900883 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700884 }
885
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900886 // ---------------------------------------------------------------------------------------
887 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
888 // 1) it comes from the system process
889 // 2) the calling process' user id is identical to the current user id IMMS thinks.
890 private boolean calledFromValidUser() {
891 final int uid = Binder.getCallingUid();
892 final int userId = UserHandle.getUserId(uid);
893 if (DEBUG) {
894 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
895 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
896 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900897 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900898 }
899 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
900 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900901 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900902
903 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
904 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
905 // must not manage background users' states in any functions.
906 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
907 // by a token.
908 if (mContext.checkCallingOrSelfPermission(
909 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
910 == PackageManager.PERMISSION_GRANTED) {
911 if (DEBUG) {
912 Slog.d(TAG, "--- Access granted because the calling process has "
913 + "the INTERACT_ACROSS_USERS_FULL permission");
914 }
915 return true;
916 }
917 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
918 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900919 }
920
921 private boolean bindCurrentInputMethodService(
922 Intent service, ServiceConnection conn, int flags) {
923 if (service == null || conn == null) {
924 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
925 return false;
926 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800927 return mContext.bindServiceAsUser(service, conn, flags,
928 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900929 }
930
satoke7c6998e2011-06-03 17:57:59 +0900931 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900933 // TODO: Make this work even for non-current users?
934 if (!calledFromValidUser()) {
935 return Collections.emptyList();
936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 synchronized (mMethodMap) {
938 return new ArrayList<InputMethodInfo>(mMethodList);
939 }
940 }
941
satoke7c6998e2011-06-03 17:57:59 +0900942 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 public List<InputMethodInfo> getEnabledInputMethodList() {
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) {
satokd87c2592010-09-29 11:52:06 +0900949 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951 }
952
satokbb4aa062011-01-19 21:40:27 +0900953 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
954 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
955 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
956 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900957 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900958 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900959 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900960 }
961 return enabledInputMethodAndSubtypes;
962 }
963
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900964 /**
965 * @param imi if null, returns enabled subtypes for the current imi
966 * @return enabled subtypes of the specified imi
967 */
satoke7c6998e2011-06-03 17:57:59 +0900968 @Override
satok16331c82010-12-20 23:48:46 +0900969 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
970 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 // TODO: Make this work even for non-current users?
972 if (!calledFromValidUser()) {
973 return Collections.emptyList();
974 }
satok67ddf9c2010-11-17 09:45:54 +0900975 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900976 if (imi == null && mCurMethodId != null) {
977 imi = mMethodMap.get(mCurMethodId);
978 }
979 return mSettings.getEnabledInputMethodSubtypeListLocked(
980 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900981 }
982 }
983
satoke7c6998e2011-06-03 17:57:59 +0900984 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 public void addClient(IInputMethodClient client,
986 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 if (!calledFromValidUser()) {
988 return;
989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 synchronized (mMethodMap) {
991 mClients.put(client.asBinder(), new ClientState(client,
992 inputContext, uid, pid));
993 }
994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800995
satoke7c6998e2011-06-03 17:57:59 +0900996 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900998 if (!calledFromValidUser()) {
999 return;
1000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001002 ClientState cs = mClients.remove(client.asBinder());
1003 if (cs != null) {
1004 clearClientSessionLocked(cs);
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 void executeOrSendMessage(IInterface target, Message msg) {
1010 if (target.asBinder() instanceof Binder) {
1011 mCaller.sendMessage(msg);
1012 } else {
1013 handleMessage(msg);
1014 msg.recycle();
1015 }
1016 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001017
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001018 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001020 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 + mCurClient.client.asBinder());
1022 if (mBoundToMethod) {
1023 mBoundToMethod = false;
1024 if (mCurMethod != null) {
1025 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1026 MSG_UNBIND_INPUT, mCurMethod));
1027 }
1028 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001029
1030 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1031 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1033 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1034 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001036
The Android Open Source Project10592532009-03-18 17:39:46 -07001037 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 private int getImeShowFlags() {
1042 int flags = 0;
1043 if (mShowForced) {
1044 flags |= InputMethod.SHOW_FORCED
1045 | InputMethod.SHOW_EXPLICIT;
1046 } else if (mShowExplicitlyRequested) {
1047 flags |= InputMethod.SHOW_EXPLICIT;
1048 }
1049 return flags;
1050 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 private int getAppShowFlags() {
1053 int flags = 0;
1054 if (mShowForced) {
1055 flags |= InputMethodManager.SHOW_FORCED;
1056 } else if (!mShowExplicitlyRequested) {
1057 flags |= InputMethodManager.SHOW_IMPLICIT;
1058 }
1059 return flags;
1060 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001061
Dianne Hackborn7663d802012-02-24 13:08:49 -08001062 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 if (!mBoundToMethod) {
1064 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1065 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1066 mBoundToMethod = true;
1067 }
1068 final SessionState session = mCurClient.curSession;
1069 if (initial) {
1070 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1071 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1072 } else {
1073 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1074 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1075 }
1076 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001078 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001080 return new InputBindResult(session.session,
1081 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001085 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 // If no method is currently selected, do nothing.
1087 if (mCurMethodId == null) {
1088 return mNoBinding;
1089 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 ClientState cs = mClients.get(client.asBinder());
1092 if (cs == null) {
1093 throw new IllegalArgumentException("unknown client "
1094 + client.asBinder());
1095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 try {
1098 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1099 // Check with the window manager to make sure this client actually
1100 // has a window with focus. If not, reject. This is thread safe
1101 // because if the focus changes some time before or after, the
1102 // next client receiving focus that has any interest in input will
1103 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001104 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1106 return null;
1107 }
1108 } catch (RemoteException e) {
1109 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001110
Dianne Hackborn7663d802012-02-24 13:08:49 -08001111 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1112 }
1113
1114 InputBindResult startInputUncheckedLocked(ClientState cs,
1115 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1116 // If no method is currently selected, do nothing.
1117 if (mCurMethodId == null) {
1118 return mNoBinding;
1119 }
1120
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001121 if (mCurClient == null) {
1122 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1123 if (DEBUG) {
1124 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1125 }
1126 }
1127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 if (mCurClient != cs) {
1129 // If the client is changing, we need to switch over to the new
1130 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001131 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001132 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 + cs.client.asBinder());
1134
1135 // If the screen is on, inform the new client it is active
1136 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001137 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1138 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 // Bump up the sequence for this client and attach it.
1143 mCurSeq++;
1144 if (mCurSeq <= 0) mCurSeq = 1;
1145 mCurClient = cs;
1146 mCurInputContext = inputContext;
1147 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 // Check if the input method is changing.
1150 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1151 if (cs.curSession != null) {
1152 // Fast case: if we are already connected to the input method,
1153 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001154 return attachNewInputLocked(
1155 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 if (mHaveConnection) {
1158 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 // Return to client, and we will get back with it when
1160 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001161 requestClientSessionLocked(cs);
1162 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 } else if (SystemClock.uptimeMillis()
1164 < (mLastBindTime+TIME_TO_RECONNECT)) {
1165 // In this case we have connected to the service, but
1166 // don't yet have its interface. If it hasn't been too
1167 // long since we did the connection, we'll return to
1168 // the client and wait to get the service interface so
1169 // we can report back. If it has been too long, we want
1170 // to fall through so we can try a disconnect/reconnect
1171 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001172 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1175 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 }
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001180 return startInputInnerLocked();
1181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001183 InputBindResult startInputInnerLocked() {
1184 if (mCurMethodId == null) {
1185 return mNoBinding;
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001188 if (!mSystemReady) {
1189 // If the system is not yet ready, we shouldn't be running third
1190 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001191 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1195 if (info == null) {
1196 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1197 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001199 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1202 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001203 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1204 com.android.internal.R.string.input_method_binding_label);
1205 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1206 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001207 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001208 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 mLastBindTime = SystemClock.uptimeMillis();
1210 mHaveConnection = true;
1211 mCurId = info.getId();
1212 mCurToken = new Binder();
1213 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001214 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mIWindowManager.addWindowToken(mCurToken,
1216 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1217 } catch (RemoteException e) {
1218 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001219 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 } else {
1221 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001222 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 + mCurIntent);
1224 }
1225 return null;
1226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001227
satoke7c6998e2011-06-03 17:57:59 +09001228 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001230 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001231 if (!calledFromValidUser()) {
1232 return null;
1233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 synchronized (mMethodMap) {
1235 final long ident = Binder.clearCallingIdentity();
1236 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001237 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 } finally {
1239 Binder.restoreCallingIdentity(ident);
1240 }
1241 }
1242 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001243
satoke7c6998e2011-06-03 17:57:59 +09001244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 public void finishInput(IInputMethodClient client) {
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
satoke7c6998e2011-06-03 17:57:59 +09001248 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 public void onServiceConnected(ComponentName name, IBinder service) {
1250 synchronized (mMethodMap) {
1251 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1252 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001253 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001254 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001255 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001256 return;
1257 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001258 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001259 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1260 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001262 clearClientSessionLocked(mCurClient);
1263 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 }
1265 }
1266 }
1267 }
1268
Jeff Brownc28867a2013-03-26 15:42:39 -07001269 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1270 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 synchronized (mMethodMap) {
1272 if (mCurMethod != null && method != null
1273 && mCurMethod.asBinder() == method.asBinder()) {
1274 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001277 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001278 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (res.method != null) {
1280 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1281 MSG_BIND_METHOD, mCurClient.client, res));
1282 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001283 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
1286 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001287
1288 // Session abandoned. Close its associated input channel.
1289 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001292 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001293 if (mVisibleBound) {
1294 mContext.unbindService(mVisibleConnection);
1295 mVisibleBound = false;
1296 }
1297
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001298 if (mHaveConnection) {
1299 mContext.unbindService(this);
1300 mHaveConnection = false;
1301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001302
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001303 if (mCurToken != null) {
1304 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001305 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001306 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001307 // The current IME is shown. Hence an IME switch (transition) is happening.
1308 mWindowManagerService.saveLastInputMethodWindowForTransition();
1309 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001310 mIWindowManager.removeWindowToken(mCurToken);
1311 } catch (RemoteException e) {
1312 }
1313 mCurToken = null;
1314 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315
The Android Open Source Project10592532009-03-18 17:39:46 -07001316 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001317 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001319 if (reportToClient && mCurClient != null) {
1320 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1321 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1322 }
1323 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001324
1325 void requestClientSessionLocked(ClientState cs) {
1326 if (!cs.sessionRequested) {
1327 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1328 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1329 cs.sessionRequested = true;
1330 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1331 MSG_CREATE_SESSION, mCurMethod, channels[1],
1332 new MethodCallback(this, mCurMethod, channels[0])));
1333 }
1334 }
1335
1336 void clearClientSessionLocked(ClientState cs) {
1337 finishSessionLocked(cs.curSession);
1338 cs.curSession = null;
1339 cs.sessionRequested = false;
1340 }
1341
1342 private void finishSessionLocked(SessionState sessionState) {
1343 if (sessionState != null) {
1344 if (sessionState.session != null) {
1345 try {
1346 sessionState.session.finishSession();
1347 } catch (RemoteException e) {
1348 Slog.w(TAG, "Session failed to close due to remote exception", e);
1349 setImeWindowVisibilityStatusHiddenLocked();
1350 }
1351 sessionState.session = null;
1352 }
1353 if (sessionState.channel != null) {
1354 sessionState.channel.dispose();
1355 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001356 }
1357 }
1358 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001359
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001360 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 if (mCurMethod != null) {
1362 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001363 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001365
Jeff Brownc28867a2013-03-26 15:42:39 -07001366 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001367 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 mCurMethod = null;
1369 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001370 if (mStatusBar != null) {
1371 mStatusBar.setIconVisibility("ime", false);
1372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
satoke7c6998e2011-06-03 17:57:59 +09001375 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 public void onServiceDisconnected(ComponentName name) {
1377 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001378 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 + " mCurIntent=" + mCurIntent);
1380 if (mCurMethod != null && mCurIntent != null
1381 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001382 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 // We consider this to be a new bind attempt, since the system
1384 // should now try to restart the service for us.
1385 mLastBindTime = SystemClock.uptimeMillis();
1386 mShowRequested = mInputShown;
1387 mInputShown = false;
1388 if (mCurClient != null) {
1389 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1390 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1391 }
1392 }
1393 }
1394 }
1395
satokf9f01002011-05-19 21:31:50 +09001396 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001398 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 long ident = Binder.clearCallingIdentity();
1400 try {
1401 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001402 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 return;
1404 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 synchronized (mMethodMap) {
1407 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001408 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001409 if (mStatusBar != null) {
1410 mStatusBar.setIconVisibility("ime", false);
1411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001413 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001414 CharSequence contentDescription = null;
1415 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001416 // Use PackageManager to load label
1417 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001418 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001419 mIPackageManager.getApplicationInfo(packageName, 0,
1420 mSettings.getCurrentUserId()));
1421 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001422 /* ignore */
1423 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001424 if (mStatusBar != null) {
1425 mStatusBar.setIcon("ime", packageName, iconId, 0,
1426 contentDescription != null
1427 ? contentDescription.toString() : null);
1428 mStatusBar.setIconVisibility("ime", true);
1429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 }
1432 } finally {
1433 Binder.restoreCallingIdentity(ident);
1434 }
1435 }
1436
satok7cfc0ed2011-06-20 21:29:36 +09001437 private boolean needsToShowImeSwitchOngoingNotification() {
1438 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001439 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001440 synchronized (mMethodMap) {
1441 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1442 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001443 if (N > 2) return true;
1444 if (N < 1) return false;
1445 int nonAuxCount = 0;
1446 int auxCount = 0;
1447 InputMethodSubtype nonAuxSubtype = null;
1448 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001449 for(int i = 0; i < N; ++i) {
1450 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001451 final List<InputMethodSubtype> subtypes =
1452 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001453 final int subtypeCount = subtypes.size();
1454 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001455 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001456 } else {
1457 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001458 final InputMethodSubtype subtype = subtypes.get(j);
1459 if (!subtype.isAuxiliary()) {
1460 ++nonAuxCount;
1461 nonAuxSubtype = subtype;
1462 } else {
1463 ++auxCount;
1464 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001465 }
1466 }
1467 }
satok7cfc0ed2011-06-20 21:29:36 +09001468 }
satokb6359412011-06-28 17:47:41 +09001469 if (nonAuxCount > 1 || auxCount > 1) {
1470 return true;
1471 } else if (nonAuxCount == 1 && auxCount == 1) {
1472 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001473 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1474 || auxSubtype.overridesImplicitlyEnabledSubtype()
1475 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001476 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1477 return false;
1478 }
1479 return true;
1480 }
1481 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001482 }
satok7cfc0ed2011-06-20 21:29:36 +09001483 }
1484
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001485 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001486 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001487 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001488 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001489 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001490 try {
1491 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001492 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001493 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001494 return;
1495 }
1496
1497 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001498 mImeWindowVis = vis;
1499 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001500 if (mStatusBar != null) {
1501 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1502 }
jungheang.lee217fd292013-02-26 16:53:22 +09001503 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1504 && (mWindowManagerService.isHardKeyboardAvailable()
1505 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001506 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1507 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001508 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001509 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001510 final CharSequence title = mRes.getText(
1511 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001512 final CharSequence imiLabel = imi.loadLabel(pm);
1513 final CharSequence summary = mCurrentSubtype != null
1514 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1515 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1516 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001517 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001518 : imiLabel;
1519
satok7cfc0ed2011-06-20 21:29:36 +09001520 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001521 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001522 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001523 if (DEBUG) {
1524 Slog.d(TAG, "--- show notification: label = " + imiLabel
1525 + ", summary = " + summary);
1526 }
1527 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001528 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001529 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001530 mNotificationShown = true;
1531 }
satok7cfc0ed2011-06-20 21:29:36 +09001532 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001533 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001534 if (DEBUG) {
1535 Slog.d(TAG, "--- hide notification");
1536 }
1537 mNotificationManager.cancelAsUser(null,
1538 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001539 mNotificationShown = false;
1540 }
1541 }
satok06487a52010-10-29 11:37:18 +09001542 }
1543 } finally {
1544 Binder.restoreCallingIdentity(ident);
1545 }
1546 }
1547
satoke7c6998e2011-06-03 17:57:59 +09001548 @Override
satokf9f01002011-05-19 21:31:50 +09001549 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001550 if (!calledFromValidUser()) {
1551 return;
1552 }
satokf9f01002011-05-19 21:31:50 +09001553 synchronized (mMethodMap) {
1554 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1555 for (int i = 0; i < spans.length; ++i) {
1556 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001557 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001558 mSecureSuggestionSpans.put(ss, currentImi);
1559 }
1560 }
1561 }
1562 }
1563
satoke7c6998e2011-06-03 17:57:59 +09001564 @Override
satokf9f01002011-05-19 21:31:50 +09001565 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001566 if (!calledFromValidUser()) {
1567 return false;
1568 }
satokf9f01002011-05-19 21:31:50 +09001569 synchronized (mMethodMap) {
1570 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1571 // TODO: Do not send the intent if the process of the targetImi is already dead.
1572 if (targetImi != null) {
1573 final String[] suggestions = span.getSuggestions();
1574 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001575 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001576 final Intent intent = new Intent();
1577 // Ensures that only a class in the original IME package will receive the
1578 // notification.
satok42c5a162011-05-26 16:46:14 +09001579 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001580 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1582 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1583 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001584 final long ident = Binder.clearCallingIdentity();
1585 try {
1586 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1587 } finally {
1588 Binder.restoreCallingIdentity(ident);
1589 }
satokf9f01002011-05-19 21:31:50 +09001590 return true;
1591 }
1592 }
1593 return false;
1594 }
1595
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001596 void updateFromSettingsLocked(boolean enabledMayChange) {
1597 if (enabledMayChange) {
1598 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1599 for (int i=0; i<enabled.size(); i++) {
1600 // We allow the user to select "disabled until used" apps, so if they
1601 // are enabling one of those here we now need to make it enabled.
1602 InputMethodInfo imm = enabled.get(i);
1603 try {
1604 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1605 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1606 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001607 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001608 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001609 if (DEBUG) {
1610 Slog.d(TAG, "Update state(" + imm.getId()
1611 + "): DISABLED_UNTIL_USED -> DEFAULT");
1612 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001613 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1614 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001615 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1616 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001617 }
1618 } catch (RemoteException e) {
1619 }
1620 }
1621 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001622 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1623 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1624 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1625 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001626 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001627 // There is no input method selected, try to choose new applicable input method.
1628 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001629 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001630 }
1631 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001633 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001635 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001636 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001637 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
satokf3db1af2010-11-23 13:34:33 +09001639 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001640 } else {
1641 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001642 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001643 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
satokab751aa2010-09-14 19:17:36 +09001647 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 InputMethodInfo info = mMethodMap.get(id);
1649 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001650 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001652
satokd81e9502012-05-21 12:58:45 +09001653 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001655 final int subtypeCount = info.getSubtypeCount();
1656 if (subtypeCount <= 0) {
1657 return;
satokcd7cd292010-11-20 15:46:23 +09001658 }
satokd81e9502012-05-21 12:58:45 +09001659 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1660 final InputMethodSubtype newSubtype;
1661 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1662 newSubtype = info.getSubtypeAt(subtypeId);
1663 } else {
1664 // If subtype is null, try to find the most applicable one from
1665 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001666 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001667 }
1668 if (newSubtype == null || oldSubtype == null) {
1669 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1670 + ", new subtype = " + newSubtype);
1671 return;
1672 }
1673 if (newSubtype != oldSubtype) {
1674 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1675 if (mCurMethod != null) {
1676 try {
1677 refreshImeWindowVisibilityLocked();
1678 mCurMethod.changeInputMethodSubtype(newSubtype);
1679 } catch (RemoteException e) {
1680 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001681 }
1682 }
1683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 return;
1685 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001686
satokd81e9502012-05-21 12:58:45 +09001687 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 final long ident = Binder.clearCallingIdentity();
1689 try {
satokab751aa2010-09-14 19:17:36 +09001690 // Set a subtype to this input method.
1691 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001692 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1693 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1694 // because mCurMethodId is stored as a history in
1695 // setSelectedInputMethodAndSubtypeLocked().
1696 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697
1698 if (ActivityManagerNative.isSystemReady()) {
1699 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001700 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001702 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001704 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 } finally {
1706 Binder.restoreCallingIdentity(ident);
1707 }
1708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001709
satok42c5a162011-05-26 16:46:14 +09001710 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001711 public boolean showSoftInput(IInputMethodClient client, int flags,
1712 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001713 if (!calledFromValidUser()) {
1714 return false;
1715 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001716 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 long ident = Binder.clearCallingIdentity();
1718 try {
1719 synchronized (mMethodMap) {
1720 if (mCurClient == null || client == null
1721 || mCurClient.client.asBinder() != client.asBinder()) {
1722 try {
1723 // We need to check if this is the current client with
1724 // focus in the window manager, to allow this call to
1725 // be made before input is started in it.
1726 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001727 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001728 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001731 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
1733 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001734
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001735 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001736 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738 } finally {
1739 Binder.restoreCallingIdentity(ident);
1740 }
1741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
The Android Open Source Project4df24232009-03-05 14:34:35 -08001743 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mShowRequested = true;
1745 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1746 mShowExplicitlyRequested = true;
1747 }
1748 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1749 mShowExplicitlyRequested = true;
1750 mShowForced = true;
1751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001752
Dianne Hackborncc278702009-09-02 23:07:23 -07001753 if (!mSystemReady) {
1754 return false;
1755 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001756
The Android Open Source Project4df24232009-03-05 14:34:35 -08001757 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001759 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001760 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1761 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1762 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001764 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001765 bindCurrentInputMethodService(
1766 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001767 mVisibleBound = true;
1768 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001769 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001771 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // The client has asked to have the input method shown, but
1773 // we have been sitting here too long with a connection to the
1774 // service and no interface received, so let's disconnect/connect
1775 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001776 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001778 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001780 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001781 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001782 } else {
1783 if (DEBUG) {
1784 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1785 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001788
The Android Open Source Project4df24232009-03-05 14:34:35 -08001789 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001791
satok42c5a162011-05-26 16:46:14 +09001792 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001793 public boolean hideSoftInput(IInputMethodClient client, int flags,
1794 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001795 if (!calledFromValidUser()) {
1796 return false;
1797 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001798 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 long ident = Binder.clearCallingIdentity();
1800 try {
1801 synchronized (mMethodMap) {
1802 if (mCurClient == null || client == null
1803 || mCurClient.client.asBinder() != client.asBinder()) {
1804 try {
1805 // We need to check if this is the current client with
1806 // focus in the window manager, to allow this call to
1807 // be made before input is started in it.
1808 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001809 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1810 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001811 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001812 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 }
1814 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001815 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001816 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001819
Joe Onorato8a9b2202010-02-26 18:56:32 -08001820 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001821 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
1826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001827
The Android Open Source Project4df24232009-03-05 14:34:35 -08001828 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1830 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001831 if (DEBUG) Slog.v(TAG, "Not hiding: explicit show not cancelled by non-explicit hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001832 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001835 if (DEBUG) Slog.v(TAG, "Not hiding: forced show not cancelled by not-always hide");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001836 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001838 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001840 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1841 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1842 res = true;
1843 } else {
1844 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001846 if (mHaveConnection && mVisibleBound) {
1847 mContext.unbindService(mVisibleConnection);
1848 mVisibleBound = false;
1849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 mInputShown = false;
1851 mShowRequested = false;
1852 mShowExplicitlyRequested = false;
1853 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001854 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001856
satok42c5a162011-05-26 16:46:14 +09001857 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001858 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1859 int controlFlags, int softInputMode, int windowFlags,
1860 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001861 // Needs to check the validity before clearing calling identity
1862 final boolean calledFromValidUser = calledFromValidUser();
1863
Dianne Hackborn7663d802012-02-24 13:08:49 -08001864 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 long ident = Binder.clearCallingIdentity();
1866 try {
1867 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001869 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001871 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001872
Dianne Hackborn7663d802012-02-24 13:08:49 -08001873 ClientState cs = mClients.get(client.asBinder());
1874 if (cs == null) {
1875 throw new IllegalArgumentException("unknown client "
1876 + client.asBinder());
1877 }
1878
1879 try {
1880 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1881 // Check with the window manager to make sure this client actually
1882 // has a window with focus. If not, reject. This is thread safe
1883 // because if the focus changes some time before or after, the
1884 // next client receiving focus that has any interest in input will
1885 // be calling through here after that change happens.
1886 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1887 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1888 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001890 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001892
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001893 if (!calledFromValidUser) {
1894 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1895 Slog.w(TAG, "If you want to interect with IME, you need "
1896 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1897 hideCurrentInputLocked(0, null);
1898 return null;
1899 }
1900
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001901 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001902 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001903 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001904 if (attribute != null) {
1905 return startInputUncheckedLocked(cs, inputContext, attribute,
1906 controlFlags);
1907 }
1908 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001909 }
1910 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001911
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001912 // Should we auto-show the IME even if the caller has not
1913 // specified what should be done with it?
1914 // We only do this automatically if the window can resize
1915 // to accommodate the IME (so what the user sees will give
1916 // them good context without input information being obscured
1917 // by the IME) or if running on a large screen where there
1918 // is more room for the target window + IME.
1919 final boolean doAutoShow =
1920 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1921 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1922 || mRes.getConfiguration().isLayoutSizeAtLeast(
1923 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001924 final boolean isTextEditor =
1925 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1926
1927 // We want to start input before showing the IME, but after closing
1928 // it. We want to do this after closing it to help the IME disappear
1929 // more quickly (not get stuck behind it initializing itself for the
1930 // new focused input, even if its window wants to hide the IME).
1931 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1934 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001935 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1937 // There is no focus view, and this window will
1938 // be behind any soft input window, so hide the
1939 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001940 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001941 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001943 } else if (isTextEditor && doAutoShow && (softInputMode &
1944 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 // There is a focus view, and we are navigating forward
1946 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001947 // We only do this automatically if the window can resize
1948 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001949 // them good context without input information being obscured
1950 // by the IME) or if running on a large screen where there
1951 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001952 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001953 if (attribute != null) {
1954 res = startInputUncheckedLocked(cs, inputContext, attribute,
1955 controlFlags);
1956 didStart = true;
1957 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001958 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
1960 break;
1961 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1962 // Do nothing.
1963 break;
1964 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1965 if ((softInputMode &
1966 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001967 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001968 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
1970 break;
1971 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001973 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 break;
1975 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1976 if ((softInputMode &
1977 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001978 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001979 if (attribute != null) {
1980 res = startInputUncheckedLocked(cs, inputContext, attribute,
1981 controlFlags);
1982 didStart = true;
1983 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001984 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 break;
1987 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001988 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001989 if (attribute != null) {
1990 res = startInputUncheckedLocked(cs, inputContext, attribute,
1991 controlFlags);
1992 didStart = true;
1993 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001994 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 break;
1996 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001997
1998 if (!didStart && attribute != null) {
1999 res = startInputUncheckedLocked(cs, inputContext, attribute,
2000 controlFlags);
2001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 } finally {
2004 Binder.restoreCallingIdentity(ident);
2005 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002006
2007 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
satok42c5a162011-05-26 16:46:14 +09002010 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002012 if (!calledFromValidUser()) {
2013 return;
2014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 synchronized (mMethodMap) {
2016 if (mCurClient == null || client == null
2017 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002018 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002019 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021
satok440aab52010-11-25 09:43:11 +09002022 // Always call subtype picker, because subtype picker is a superset of input method
2023 // picker.
satokab751aa2010-09-14 19:17:36 +09002024 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2025 }
2026 }
2027
satok42c5a162011-05-26 16:46:14 +09002028 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002030 if (!calledFromValidUser()) {
2031 return;
2032 }
satok28203512010-11-24 11:06:49 +09002033 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2034 }
2035
satok42c5a162011-05-26 16:46:14 +09002036 @Override
satok28203512010-11-24 11:06:49 +09002037 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002038 if (!calledFromValidUser()) {
2039 return;
2040 }
satok28203512010-11-24 11:06:49 +09002041 synchronized (mMethodMap) {
2042 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002043 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002044 mMethodMap.get(id), subtype.hashCode()));
2045 } else {
2046 setInputMethod(token, id);
2047 }
2048 }
satokab751aa2010-09-14 19:17:36 +09002049 }
2050
satok42c5a162011-05-26 16:46:14 +09002051 @Override
satokb416a712010-11-25 20:42:14 +09002052 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002053 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002054 if (!calledFromValidUser()) {
2055 return;
2056 }
satokb416a712010-11-25 20:42:14 +09002057 synchronized (mMethodMap) {
2058 if (mCurClient == null || client == null
2059 || mCurClient.client.asBinder() != client.asBinder()) {
2060 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2061 }
satok7fee71f2010-12-17 18:54:26 +09002062 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2063 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002064 }
2065 }
2066
satok4fc87d62011-05-20 16:13:43 +09002067 @Override
satok735cf382010-11-11 20:40:09 +09002068 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002069 if (!calledFromValidUser()) {
2070 return false;
2071 }
satok735cf382010-11-11 20:40:09 +09002072 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002073 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002074 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002075 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002076 lastImi = mMethodMap.get(lastIme.first);
2077 } else {
2078 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002079 }
satok4fc87d62011-05-20 16:13:43 +09002080 String targetLastImiId = null;
2081 int subtypeId = NOT_A_SUBTYPE_ID;
2082 if (lastIme != null && lastImi != null) {
2083 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2084 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2085 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2086 : mCurrentSubtype.hashCode();
2087 // If the last IME is the same as the current IME and the last subtype is not
2088 // defined, there is no need to switch to the last IME.
2089 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2090 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002091 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002092 }
2093 }
2094
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002095 if (TextUtils.isEmpty(targetLastImiId)
2096 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002097 // This is a safety net. If the currentSubtype can't be added to the history
2098 // and the framework couldn't find the last ime, we will make the last ime be
2099 // the most applicable enabled keyboard subtype of the system imes.
2100 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2101 if (enabled != null) {
2102 final int N = enabled.size();
2103 final String locale = mCurrentSubtype == null
2104 ? mRes.getConfiguration().locale.toString()
2105 : mCurrentSubtype.getLocale();
2106 for (int i = 0; i < N; ++i) {
2107 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002108 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002109 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002110 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2111 InputMethodUtils.getSubtypes(imi),
2112 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002113 if (keyboardSubtype != null) {
2114 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002115 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002116 imi, keyboardSubtype.hashCode());
2117 if(keyboardSubtype.getLocale().equals(locale)) {
2118 break;
2119 }
2120 }
2121 }
2122 }
2123 }
2124 }
2125
2126 if (!TextUtils.isEmpty(targetLastImiId)) {
2127 if (DEBUG) {
2128 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2129 + ", from: " + mCurMethodId + ", " + subtypeId);
2130 }
2131 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2132 return true;
2133 } else {
2134 return false;
2135 }
satok735cf382010-11-11 20:40:09 +09002136 }
2137 }
2138
satoke7c6998e2011-06-03 17:57:59 +09002139 @Override
satok688bd472012-02-09 20:09:17 +09002140 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002141 if (!calledFromValidUser()) {
2142 return false;
2143 }
satok688bd472012-02-09 20:09:17 +09002144 synchronized (mMethodMap) {
2145 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2146 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2147 if (nextSubtype == null) {
2148 return false;
2149 }
2150 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2151 return true;
2152 }
2153 }
2154
2155 @Override
satok68f1b782011-04-11 14:26:04 +09002156 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002157 if (!calledFromValidUser()) {
2158 return null;
2159 }
satok68f1b782011-04-11 14:26:04 +09002160 synchronized (mMethodMap) {
2161 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2162 // TODO: Handle the case of the last IME with no subtypes
2163 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2164 || TextUtils.isEmpty(lastIme.second)) return null;
2165 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2166 if (lastImi == null) return null;
2167 try {
2168 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002169 final int lastSubtypeId =
2170 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002171 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2172 return null;
2173 }
2174 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002175 } catch (NumberFormatException e) {
2176 return null;
2177 }
2178 }
2179 }
2180
satoke7c6998e2011-06-03 17:57:59 +09002181 @Override
satokee5e77c2011-09-02 18:50:15 +09002182 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002183 if (!calledFromValidUser()) {
2184 return;
2185 }
satok91e88122011-07-18 11:11:42 +09002186 // By this IPC call, only a process which shares the same uid with the IME can add
2187 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002188 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002189 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002190 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002191 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002192 final String[] packageInfos;
2193 try {
2194 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2195 } catch (RemoteException e) {
2196 Slog.e(TAG, "Failed to get package infos");
2197 return;
2198 }
satok91e88122011-07-18 11:11:42 +09002199 if (packageInfos != null) {
2200 final int packageNum = packageInfos.length;
2201 for (int i = 0; i < packageNum; ++i) {
2202 if (packageInfos[i].equals(imi.getPackageName())) {
2203 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002204 final long ident = Binder.clearCallingIdentity();
2205 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002206 buildInputMethodListLocked(mMethodList, mMethodMap,
2207 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002208 } finally {
2209 Binder.restoreCallingIdentity(ident);
2210 }
satokee5e77c2011-09-02 18:50:15 +09002211 return;
satok91e88122011-07-18 11:11:42 +09002212 }
2213 }
2214 }
satoke7c6998e2011-06-03 17:57:59 +09002215 }
satokee5e77c2011-09-02 18:50:15 +09002216 return;
satoke7c6998e2011-06-03 17:57:59 +09002217 }
2218
satok28203512010-11-24 11:06:49 +09002219 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 synchronized (mMethodMap) {
2221 if (token == null) {
2222 if (mContext.checkCallingOrSelfPermission(
2223 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2224 != PackageManager.PERMISSION_GRANTED) {
2225 throw new SecurityException(
2226 "Using null token requires permission "
2227 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2228 }
2229 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002230 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2231 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 return;
2233 }
2234
satokc5933802011-08-31 21:26:04 +09002235 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 try {
satokab751aa2010-09-14 19:17:36 +09002237 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 } finally {
2239 Binder.restoreCallingIdentity(ident);
2240 }
2241 }
2242 }
2243
satok42c5a162011-05-26 16:46:14 +09002244 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002246 if (!calledFromValidUser()) {
2247 return;
2248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 synchronized (mMethodMap) {
2250 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002251 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2252 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 return;
2254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 long ident = Binder.clearCallingIdentity();
2256 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002257 hideCurrentInputLocked(flags, null);
2258 } finally {
2259 Binder.restoreCallingIdentity(ident);
2260 }
2261 }
2262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002263
satok42c5a162011-05-26 16:46:14 +09002264 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002265 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002266 if (!calledFromValidUser()) {
2267 return;
2268 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002269 synchronized (mMethodMap) {
2270 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002271 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2272 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002273 return;
2274 }
2275 long ident = Binder.clearCallingIdentity();
2276 try {
2277 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002278 } finally {
2279 Binder.restoreCallingIdentity(ident);
2280 }
2281 }
2282 }
2283
2284 void setEnabledSessionInMainThread(SessionState session) {
2285 if (mEnabledSession != session) {
2286 if (mEnabledSession != null) {
2287 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002288 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 mEnabledSession.method.setSessionEnabled(
2290 mEnabledSession.session, false);
2291 } catch (RemoteException e) {
2292 }
2293 }
2294 mEnabledSession = session;
2295 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002296 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 session.method.setSessionEnabled(
2298 session.session, true);
2299 } catch (RemoteException e) {
2300 }
2301 }
2302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002303
satok42c5a162011-05-26 16:46:14 +09002304 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002306 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 switch (msg.what) {
2308 case MSG_SHOW_IM_PICKER:
2309 showInputMethodMenu();
2310 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002311
satokab751aa2010-09-14 19:17:36 +09002312 case MSG_SHOW_IM_SUBTYPE_PICKER:
2313 showInputMethodSubtypeMenu();
2314 return true;
2315
satok47a44912010-10-06 16:03:58 +09002316 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002317 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002318 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002319 args.recycle();
satok217f5482010-12-15 05:19:19 +09002320 return true;
2321
2322 case MSG_SHOW_IM_CONFIG:
2323 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002324 return true;
2325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 case MSG_UNBIND_INPUT:
2329 try {
2330 ((IInputMethod)msg.obj).unbindInput();
2331 } catch (RemoteException e) {
2332 // There is nothing interesting about the method dying.
2333 }
2334 return true;
2335 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002336 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 try {
2338 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2339 } catch (RemoteException e) {
2340 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002341 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 return true;
2343 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002344 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002346 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002347 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002348 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 } catch (RemoteException e) {
2350 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002351 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 return true;
2353 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002354 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002356 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002357 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002358 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 } catch (RemoteException e) {
2360 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002361 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 return true;
2363 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002364 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002366 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2368 } catch (RemoteException e) {
2369 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002370 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002372 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002373 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002374 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002375 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002377 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002379 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002380 // Dispose the channel if the input method is not local to this process
2381 // because the remote proxy will get its own copy when unparceled.
2382 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002383 channel.dispose();
2384 }
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;
Jeff Brownc28867a2013-03-26 15:42:39 -07002388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002392 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 try {
2394 SessionState session = (SessionState)args.arg1;
2395 setEnabledSessionInMainThread(session);
2396 session.method.startInput((IInputContext)args.arg2,
2397 (EditorInfo)args.arg3);
2398 } catch (RemoteException e) {
2399 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002400 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 return true;
2402 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002403 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 try {
2405 SessionState session = (SessionState)args.arg1;
2406 setEnabledSessionInMainThread(session);
2407 session.method.restartInput((IInputContext)args.arg2,
2408 (EditorInfo)args.arg3);
2409 } catch (RemoteException e) {
2410 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002411 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 case MSG_UNBIND_METHOD:
2417 try {
2418 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2419 } catch (RemoteException e) {
2420 // There is nothing interesting about the last client dying.
2421 }
2422 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002423 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002424 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002425 IInputMethodClient client = (IInputMethodClient)args.arg1;
2426 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002428 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002430 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002431 } finally {
2432 // Dispose the channel if the input method is not local to this process
2433 // because the remote proxy will get its own copy when unparceled.
2434 if (res.channel != null && Binder.isProxy(client)) {
2435 res.channel.dispose();
2436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002438 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002440 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002441 case MSG_SET_ACTIVE:
2442 try {
2443 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2444 } catch (RemoteException e) {
2445 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2446 + ((ClientState)msg.obj).pid + " uid "
2447 + ((ClientState)msg.obj).uid);
2448 }
2449 return true;
satok01038492012-04-09 21:08:27 +09002450
2451 // --------------------------------------------------------------
2452 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2453 mHardKeyboardListener.handleHardKeyboardStatusChange(
2454 msg.arg1 == 1, msg.arg2 == 1);
2455 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
2457 return false;
2458 }
2459
satokdc9ddae2011-10-06 12:22:36 +09002460 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002461 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2462 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002463 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002464 if (DEBUG) {
2465 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2466 }
satok723a27e2010-11-11 14:58:11 +09002467 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002468 return true;
2469 }
2470
2471 return false;
2472 }
2473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002475 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002476 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002477 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2478 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 list.clear();
2481 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002482
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002483 // Use for queryIntentServicesAsUser
2484 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002485 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002486 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002488 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002490 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2491 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002492
satoke7c6998e2011-06-03 17:57:59 +09002493 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2494 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 for (int i = 0; i < services.size(); ++i) {
2496 ResolveInfo ri = services.get(i);
2497 ServiceInfo si = ri.serviceInfo;
2498 ComponentName compName = new ComponentName(si.packageName, si.name);
2499 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2500 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002501 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502 + ": it does not require the permission "
2503 + android.Manifest.permission.BIND_INPUT_METHOD);
2504 continue;
2505 }
2506
Joe Onorato8a9b2202010-02-26 18:56:32 -08002507 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508
2509 try {
satoke7c6998e2011-06-03 17:57:59 +09002510 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002512 final String id = p.getId();
2513 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514
2515 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002516 Slog.d(TAG, "Found an 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 Kataokaf1367b72013-01-25 17:20:12 +09002526 if (resetDefaultEnabledIme) {
2527 final ArrayList<InputMethodInfo> defaultEnabledIme =
2528 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2529 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2530 final InputMethodInfo imi = defaultEnabledIme.get(i);
2531 if (DEBUG) {
2532 Slog.d(TAG, "--- enable ime = " + imi);
2533 }
2534 setInputMethodEnabledLocked(imi.getId(), true);
2535 }
2536 }
2537
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002538 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002539 if (!TextUtils.isEmpty(defaultImiId)) {
2540 if (!map.containsKey(defaultImiId)) {
2541 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2542 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002543 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002544 }
2545 } else {
2546 // Double check that the default IME is certainly enabled.
2547 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002548 }
2549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002553
satokab751aa2010-09-14 19:17:36 +09002554 private void showInputMethodMenu() {
2555 showInputMethodMenuInternal(false);
2556 }
2557
2558 private void showInputMethodSubtypeMenu() {
2559 showInputMethodMenuInternal(true);
2560 }
2561
satok217f5482010-12-15 05:19:19 +09002562 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002563 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002564 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002565 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2566 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002567 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002568 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002569 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002570 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002571 }
2572
2573 private void showConfigureInputMethods() {
2574 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2575 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2576 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2577 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002578 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002579 }
2580
satok2c93efc2012-04-02 19:33:47 +09002581 private boolean isScreenLocked() {
2582 return mKeyguardManager != null
2583 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2584 }
satokab751aa2010-09-14 19:17:36 +09002585 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002586 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002589 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002590
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002591 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002592 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002593 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002594
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002595 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002596 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2597 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002598 if (immis == null || immis.size() == 0) {
2599 return;
2600 }
2601
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002602 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603
satok688bd472012-02-09 20:09:17 +09002604 final List<ImeSubtypeListItem> imList =
2605 mImListManager.getSortedInputMethodAndSubtypeList(
2606 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002607
satokc3690562012-01-10 20:14:43 +09002608 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002609 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002610 if (currentSubtype != null) {
2611 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002612 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2613 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002614 }
2615 }
2616
Ken Wakasa761eb372011-03-04 19:06:18 +09002617 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002618 mIms = new InputMethodInfo[N];
2619 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002620 int checkedItem = 0;
2621 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002622 final ImeSubtypeListItem item = imList.get(i);
2623 mIms[i] = item.mImi;
2624 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002625 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002626 int subtypeId = mSubtypeIds[i];
2627 if ((subtypeId == NOT_A_SUBTYPE_ID)
2628 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2629 || (subtypeId == lastInputMethodSubtypeId)) {
2630 checkedItem = i;
2631 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002634 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002635 com.android.internal.R.styleable.DialogPreference,
2636 com.android.internal.R.attr.alertDialogStyle, 0);
2637 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002638 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002639 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002640 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002642 }
2643 })
2644 .setIcon(a.getDrawable(
2645 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2646 a.recycle();
satok01038492012-04-09 21:08:27 +09002647 final LayoutInflater inflater =
2648 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2649 final View tv = inflater.inflate(
2650 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2651 mDialogBuilder.setCustomTitle(tv);
2652
2653 // Setup layout for a toggle switch of the hardware keyboard
2654 mSwitchingDialogTitleView = tv;
2655 mSwitchingDialogTitleView.findViewById(
2656 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2657 mWindowManagerService.isHardKeyboardAvailable() ?
2658 View.VISIBLE : View.GONE);
2659 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2660 com.android.internal.R.id.hard_keyboard_switch));
2661 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2662 hardKeySwitch.setOnCheckedChangeListener(
2663 new OnCheckedChangeListener() {
2664 @Override
2665 public void onCheckedChanged(
2666 CompoundButton buttonView, boolean isChecked) {
2667 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002668 // Ensure that the input method dialog is dismissed when changing
2669 // the hardware keyboard state.
2670 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002671 }
2672 });
satokd87c2592010-09-29 11:52:06 +09002673
Ken Wakasa05dbb652011-08-22 15:22:43 +09002674 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2675 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2676 checkedItem);
2677
2678 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002679 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002680 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002681 public void onClick(DialogInterface dialog, int which) {
2682 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002683 if (mIms == null || mIms.length <= which
2684 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002685 return;
2686 }
2687 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002688 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002689 adapter.mCheckedItem = which;
2690 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002691 hideInputMethodMenu();
2692 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002693 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002694 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002695 subtypeId = NOT_A_SUBTYPE_ID;
2696 }
2697 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002698 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002701 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702
satokbc81b692011-08-26 16:22:22 +09002703 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002704 mDialogBuilder.setPositiveButton(
2705 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002706 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002707 @Override
satok7f35c8c2010-10-07 21:13:11 +09002708 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002709 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002710 }
2711 });
2712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002714 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 mSwitchingDialog.getWindow().setType(
2716 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002717 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2718 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002719 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 mSwitchingDialog.show();
2721 }
2722 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002723
satok93d744d2012-05-09 17:14:08 +09002724 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002725 public final CharSequence mImeName;
2726 public final CharSequence mSubtypeName;
2727 public final InputMethodInfo mImi;
2728 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002729 private final boolean mIsSystemLocale;
2730 private final boolean mIsSystemLanguage;
2731
Ken Wakasa05dbb652011-08-22 15:22:43 +09002732 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002733 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002734 mImeName = imeName;
2735 mSubtypeName = subtypeName;
2736 mImi = imi;
2737 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002738 if (TextUtils.isEmpty(subtypeLocale)) {
2739 mIsSystemLocale = false;
2740 mIsSystemLanguage = false;
2741 } else {
2742 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2743 mIsSystemLanguage = mIsSystemLocale
2744 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2745 }
2746 }
2747
2748 @Override
2749 public int compareTo(ImeSubtypeListItem other) {
2750 if (TextUtils.isEmpty(mImeName)) {
2751 return 1;
2752 }
2753 if (TextUtils.isEmpty(other.mImeName)) {
2754 return -1;
2755 }
2756 if (!TextUtils.equals(mImeName, other.mImeName)) {
2757 return mImeName.toString().compareTo(other.mImeName.toString());
2758 }
2759 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2760 return 0;
2761 }
2762 if (mIsSystemLocale) {
2763 return -1;
2764 }
2765 if (other.mIsSystemLocale) {
2766 return 1;
2767 }
2768 if (mIsSystemLanguage) {
2769 return -1;
2770 }
2771 if (other.mIsSystemLanguage) {
2772 return 1;
2773 }
2774 if (TextUtils.isEmpty(mSubtypeName)) {
2775 return 1;
2776 }
2777 if (TextUtils.isEmpty(other.mSubtypeName)) {
2778 return -1;
2779 }
2780 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002781 }
2782 }
2783
2784 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2785 private final LayoutInflater mInflater;
2786 private final int mTextViewResourceId;
2787 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002788 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002789 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2790 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2791 super(context, textViewResourceId, itemsList);
2792 mTextViewResourceId = textViewResourceId;
2793 mItemsList = itemsList;
2794 mCheckedItem = checkedItem;
2795 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2796 }
2797
2798 @Override
2799 public View getView(int position, View convertView, ViewGroup parent) {
2800 final View view = convertView != null ? convertView
2801 : mInflater.inflate(mTextViewResourceId, null);
2802 if (position < 0 || position >= mItemsList.size()) return view;
2803 final ImeSubtypeListItem item = mItemsList.get(position);
2804 final CharSequence imeName = item.mImeName;
2805 final CharSequence subtypeName = item.mSubtypeName;
2806 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2807 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2808 if (TextUtils.isEmpty(subtypeName)) {
2809 firstTextView.setText(imeName);
2810 secondTextView.setVisibility(View.GONE);
2811 } else {
2812 firstTextView.setText(subtypeName);
2813 secondTextView.setText(imeName);
2814 secondTextView.setVisibility(View.VISIBLE);
2815 }
2816 final RadioButton radioButton =
2817 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2818 radioButton.setChecked(position == mCheckedItem);
2819 return view;
2820 }
2821 }
2822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002824 synchronized (mMethodMap) {
2825 hideInputMethodMenuLocked();
2826 }
2827 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002828
The Android Open Source Project10592532009-03-18 17:39:46 -07002829 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002830 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831
The Android Open Source Project10592532009-03-18 17:39:46 -07002832 if (mSwitchingDialog != null) {
2833 mSwitchingDialog.dismiss();
2834 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002836
The Android Open Source Project10592532009-03-18 17:39:46 -07002837 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002838 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002842
satok42c5a162011-05-26 16:46:14 +09002843 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002845 // TODO: Make this work even for non-current users?
2846 if (!calledFromValidUser()) {
2847 return false;
2848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 synchronized (mMethodMap) {
2850 if (mContext.checkCallingOrSelfPermission(
2851 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2852 != PackageManager.PERMISSION_GRANTED) {
2853 throw new SecurityException(
2854 "Requires permission "
2855 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2856 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 long ident = Binder.clearCallingIdentity();
2859 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002860 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } finally {
2862 Binder.restoreCallingIdentity(ident);
2863 }
2864 }
2865 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002866
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002867 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2868 // Make sure this is a valid input method.
2869 InputMethodInfo imm = mMethodMap.get(id);
2870 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002871 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002872 }
2873
satokd87c2592010-09-29 11:52:06 +09002874 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2875 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002876
satokd87c2592010-09-29 11:52:06 +09002877 if (enabled) {
2878 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2879 if (pair.first.equals(id)) {
2880 // We are enabling this input method, but it is already enabled.
2881 // Nothing to do. The previous state was enabled.
2882 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002883 }
2884 }
satokd87c2592010-09-29 11:52:06 +09002885 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2886 // Previous state was disabled.
2887 return false;
2888 } else {
2889 StringBuilder builder = new StringBuilder();
2890 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2891 builder, enabledInputMethodsList, id)) {
2892 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002893 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002894 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2895 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2896 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002897 }
2898 // Previous state was enabled.
2899 return true;
2900 } else {
2901 // We are disabling the input method but it is already disabled.
2902 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002903 return false;
2904 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002905 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002906 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002907
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
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002911 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002912
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 {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002952 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002953 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
satok4e4569d2010-11-19 18:45:53 +09002962 // If there are no selected shortcuts, tries finding the most applicable ones.
2963 private Pair<InputMethodInfo, InputMethodSubtype>
2964 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2965 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2966 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002967 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002968 boolean foundInSystemIME = false;
2969
2970 // Search applicable subtype for each InputMethodInfo
2971 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002972 final String imiId = imi.getId();
2973 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2974 continue;
2975 }
satokcd7cd292010-11-20 15:46:23 +09002976 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002977 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002978 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002979 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002980 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002981 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002982 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002983 }
satokdf31ae62011-01-15 06:19:44 +09002984 // 2. Search by the system locale from enabledSubtypes.
2985 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002986 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002987 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002988 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002989 }
satoka86f5e42011-09-02 17:12:42 +09002990 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002991 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002992 final ArrayList<InputMethodSubtype> subtypesForSearch =
2993 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002994 ? InputMethodUtils.getSubtypes(imi)
2995 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002996 // 4. Search by the current subtype's locale from all subtypes.
2997 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002998 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002999 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003000 }
3001 // 5. Search by the system locale from all subtypes.
3002 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003003 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003004 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003005 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003006 }
satokcd7cd292010-11-20 15:46:23 +09003007 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003008 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003009 // The current input method is the most applicable IME.
3010 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003011 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003012 break;
satok7599a7f2010-12-22 13:45:23 +09003013 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003014 // The system input method is 2nd applicable IME.
3015 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003016 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003017 if ((imi.getServiceInfo().applicationInfo.flags
3018 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3019 foundInSystemIME = true;
3020 }
satok4e4569d2010-11-19 18:45:53 +09003021 }
3022 }
3023 }
3024 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003025 if (mostApplicableIMI != null) {
3026 Slog.w(TAG, "Most applicable shortcut input method was:"
3027 + mostApplicableIMI.getId());
3028 if (mostApplicableSubtype != null) {
3029 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3030 + "," + mostApplicableSubtype.getMode() + ","
3031 + mostApplicableSubtype.getLocale());
3032 }
3033 }
satok4e4569d2010-11-19 18:45:53 +09003034 }
satokcd7cd292010-11-20 15:46:23 +09003035 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003036 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003037 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003038 } else {
3039 return null;
3040 }
3041 }
3042
satokab751aa2010-09-14 19:17:36 +09003043 /**
3044 * @return Return the current subtype of this input method.
3045 */
satok42c5a162011-05-26 16:46:14 +09003046 @Override
satokab751aa2010-09-14 19:17:36 +09003047 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003048 // TODO: Make this work even for non-current users?
3049 if (!calledFromValidUser()) {
3050 return null;
3051 }
3052 synchronized (mMethodMap) {
3053 return getCurrentInputMethodSubtypeLocked();
3054 }
3055 }
3056
3057 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003058 if (mCurMethodId == null) {
3059 return null;
3060 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003061 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003062 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3063 if (imi == null || imi.getSubtypeCount() == 0) {
3064 return null;
satok4e4569d2010-11-19 18:45:53 +09003065 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003066 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003067 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3068 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003069 if (subtypeId == NOT_A_SUBTYPE_ID) {
3070 // If there are no selected subtypes, the framework will try to find
3071 // the most applicable subtype from explicitly or implicitly enabled
3072 // subtypes.
3073 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003074 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 // If there is only one explicitly or implicitly enabled subtype,
3076 // just returns it.
3077 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3078 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3079 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003080 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003081 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003082 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003083 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003084 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003085 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3086 true);
satok4e4569d2010-11-19 18:45:53 +09003087 }
satok3ef8b292010-11-23 06:06:29 +09003088 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003089 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003090 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003091 }
3092 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003093 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003094 }
3095
satokf3db1af2010-11-23 13:34:33 +09003096 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3097 InputMethodSubtype subtype) {
3098 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3099 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3100 } else {
3101 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3102 subtypes.add(subtype);
3103 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3104 }
3105 }
3106
satok4e4569d2010-11-19 18:45:53 +09003107 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003108 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003109 @Override
satok4e4569d2010-11-19 18:45:53 +09003110 public List getShortcutInputMethodsAndSubtypes() {
3111 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003112 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003113 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003114 // If there are no selected shortcut subtypes, the framework will try to find
3115 // the most applicable subtype from all subtypes whose mode is
3116 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003117 Pair<InputMethodInfo, InputMethodSubtype> info =
3118 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003119 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003120 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003121 ret.add(info.first);
3122 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003123 }
satok3da92232011-01-11 22:46:30 +09003124 return ret;
satokf3db1af2010-11-23 13:34:33 +09003125 }
satokf3db1af2010-11-23 13:34:33 +09003126 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3127 ret.add(imi);
3128 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3129 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003130 }
3131 }
satokf3db1af2010-11-23 13:34:33 +09003132 return ret;
satok4e4569d2010-11-19 18:45:53 +09003133 }
3134 }
3135
satok42c5a162011-05-26 16:46:14 +09003136 @Override
satokb66d2872010-11-10 01:04:04 +09003137 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003138 // TODO: Make this work even for non-current users?
3139 if (!calledFromValidUser()) {
3140 return false;
3141 }
satokb66d2872010-11-10 01:04:04 +09003142 synchronized (mMethodMap) {
3143 if (subtype != null && mCurMethodId != null) {
3144 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003145 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003146 if (subtypeId != NOT_A_SUBTYPE_ID) {
3147 setInputMethodLocked(mCurMethodId, subtypeId);
3148 return true;
3149 }
3150 }
3151 return false;
3152 }
3153 }
3154
satok688bd472012-02-09 20:09:17 +09003155 private static class InputMethodAndSubtypeListManager {
3156 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003157 // Used to load label
satok688bd472012-02-09 20:09:17 +09003158 private final PackageManager mPm;
3159 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003160 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003161 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3162 mContext = context;
3163 mPm = context.getPackageManager();
3164 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003165 final Locale locale = context.getResources().getConfiguration().locale;
3166 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003167 }
3168
3169 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3170 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3171 new Comparator<InputMethodInfo>() {
3172 @Override
3173 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3174 if (imi2 == null) return 0;
3175 if (imi1 == null) return 1;
3176 if (mPm == null) {
3177 return imi1.getId().compareTo(imi2.getId());
3178 }
3179 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3180 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3181 return imiId1.toString().compareTo(imiId2.toString());
3182 }
3183 });
3184
3185 public ImeSubtypeListItem getNextInputMethod(
3186 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3187 if (imi == null) {
3188 return null;
3189 }
3190 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3191 if (imList.size() <= 1) {
3192 return null;
3193 }
3194 final int N = imList.size();
3195 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003196 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003197 : NOT_A_SUBTYPE_ID;
3198 for (int i = 0; i < N; ++i) {
3199 final ImeSubtypeListItem isli = imList.get(i);
3200 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3201 if (!onlyCurrentIme) {
3202 return imList.get((i + 1) % N);
3203 }
3204 for (int j = 0; j < N - 1; ++j) {
3205 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3206 if (candidate.mImi.equals(imi)) {
3207 return candidate;
3208 }
3209 }
3210 return null;
3211 }
3212 }
3213 return null;
3214 }
3215
3216 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3217 return getSortedInputMethodAndSubtypeList(true, false, false);
3218 }
3219
3220 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3221 boolean inputShown, boolean isScreenLocked) {
3222 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3223 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3224 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3225 if (immis == null || immis.size() == 0) {
3226 return Collections.emptyList();
3227 }
3228 mSortedImmis.clear();
3229 mSortedImmis.putAll(immis);
3230 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3231 if (imi == null) continue;
3232 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3233 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3234 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3235 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3236 }
satok688bd472012-02-09 20:09:17 +09003237 final CharSequence imeLabel = imi.loadLabel(mPm);
3238 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3239 final int subtypeCount = imi.getSubtypeCount();
3240 if (DEBUG) {
3241 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3242 }
3243 for (int j = 0; j < subtypeCount; ++j) {
3244 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3245 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3246 // We show all enabled IMEs and subtypes when an IME is shown.
3247 if (enabledSubtypeSet.contains(subtypeHashCode)
3248 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3249 final CharSequence subtypeLabel =
3250 subtype.overridesImplicitlyEnabledSubtype() ? null
3251 : subtype.getDisplayName(mContext, imi.getPackageName(),
3252 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003253 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3254 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003255
3256 // Removing this subtype from enabledSubtypeSet because we no longer
3257 // need to add an entry of this subtype to imList to avoid duplicated
3258 // entries.
3259 enabledSubtypeSet.remove(subtypeHashCode);
3260 }
3261 }
3262 } else {
satok93d744d2012-05-09 17:14:08 +09003263 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3264 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003265 }
3266 }
satok93d744d2012-05-09 17:14:08 +09003267 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003268 return imList;
3269 }
3270 }
3271
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003272 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003273 private static class InputMethodFileManager {
3274 private static final String SYSTEM_PATH = "system";
3275 private static final String INPUT_METHOD_PATH = "inputmethod";
3276 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3277 private static final String NODE_SUBTYPES = "subtypes";
3278 private static final String NODE_SUBTYPE = "subtype";
3279 private static final String NODE_IMI = "imi";
3280 private static final String ATTR_ID = "id";
3281 private static final String ATTR_LABEL = "label";
3282 private static final String ATTR_ICON = "icon";
3283 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3284 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3285 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3286 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3287 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3288 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003289 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003290 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003291 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003292 if (methodMap == null) {
3293 throw new NullPointerException("methodMap is null");
3294 }
3295 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003296 final File systemDir = userId == UserHandle.USER_OWNER
3297 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3298 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003299 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3300 if (!inputMethodDir.mkdirs()) {
3301 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3302 }
3303 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3304 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3305 if (!subtypeFile.exists()) {
3306 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003307 writeAdditionalInputMethodSubtypes(
3308 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003309 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003310 readAdditionalInputMethodSubtypes(
3311 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003312 }
3313 }
3314
3315 private void deleteAllInputMethodSubtypes(String imiId) {
3316 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003317 mAdditionalSubtypesMap.remove(imiId);
3318 writeAdditionalInputMethodSubtypes(
3319 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003320 }
3321 }
3322
3323 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003324 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003325 synchronized (mMethodMap) {
3326 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3327 final int N = additionalSubtypes.length;
3328 for (int i = 0; i < N; ++i) {
3329 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003330 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003331 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003332 } else {
3333 Slog.w(TAG, "Duplicated subtype definition found: "
3334 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003335 }
3336 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003337 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3338 writeAdditionalInputMethodSubtypes(
3339 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003340 }
3341 }
3342
3343 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3344 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003345 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003346 }
3347 }
3348
3349 private static void writeAdditionalInputMethodSubtypes(
3350 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3351 HashMap<String, InputMethodInfo> methodMap) {
3352 // Safety net for the case that this function is called before methodMap is set.
3353 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3354 FileOutputStream fos = null;
3355 try {
3356 fos = subtypesFile.startWrite();
3357 final XmlSerializer out = new FastXmlSerializer();
3358 out.setOutput(fos, "utf-8");
3359 out.startDocument(null, true);
3360 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3361 out.startTag(null, NODE_SUBTYPES);
3362 for (String imiId : allSubtypes.keySet()) {
3363 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3364 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3365 continue;
3366 }
3367 out.startTag(null, NODE_IMI);
3368 out.attribute(null, ATTR_ID, imiId);
3369 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3370 final int N = subtypesList.size();
3371 for (int i = 0; i < N; ++i) {
3372 final InputMethodSubtype subtype = subtypesList.get(i);
3373 out.startTag(null, NODE_SUBTYPE);
3374 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3375 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3376 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3377 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3378 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3379 out.attribute(null, ATTR_IS_AUXILIARY,
3380 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3381 out.endTag(null, NODE_SUBTYPE);
3382 }
3383 out.endTag(null, NODE_IMI);
3384 }
3385 out.endTag(null, NODE_SUBTYPES);
3386 out.endDocument();
3387 subtypesFile.finishWrite(fos);
3388 } catch (java.io.IOException e) {
3389 Slog.w(TAG, "Error writing subtypes", e);
3390 if (fos != null) {
3391 subtypesFile.failWrite(fos);
3392 }
3393 }
3394 }
3395
3396 private static void readAdditionalInputMethodSubtypes(
3397 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3398 if (allSubtypes == null || subtypesFile == null) return;
3399 allSubtypes.clear();
3400 FileInputStream fis = null;
3401 try {
3402 fis = subtypesFile.openRead();
3403 final XmlPullParser parser = Xml.newPullParser();
3404 parser.setInput(fis, null);
3405 int type = parser.getEventType();
3406 // Skip parsing until START_TAG
3407 while ((type = parser.next()) != XmlPullParser.START_TAG
3408 && type != XmlPullParser.END_DOCUMENT) {}
3409 String firstNodeName = parser.getName();
3410 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3411 throw new XmlPullParserException("Xml doesn't start with subtypes");
3412 }
3413 final int depth =parser.getDepth();
3414 String currentImiId = null;
3415 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3416 while (((type = parser.next()) != XmlPullParser.END_TAG
3417 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3418 if (type != XmlPullParser.START_TAG)
3419 continue;
3420 final String nodeName = parser.getName();
3421 if (NODE_IMI.equals(nodeName)) {
3422 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3423 if (TextUtils.isEmpty(currentImiId)) {
3424 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3425 continue;
3426 }
3427 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3428 allSubtypes.put(currentImiId, tempSubtypesArray);
3429 } else if (NODE_SUBTYPE.equals(nodeName)) {
3430 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3431 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3432 continue;
3433 }
3434 final int icon = Integer.valueOf(
3435 parser.getAttributeValue(null, ATTR_ICON));
3436 final int label = Integer.valueOf(
3437 parser.getAttributeValue(null, ATTR_LABEL));
3438 final String imeSubtypeLocale =
3439 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3440 final String imeSubtypeMode =
3441 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3442 final String imeSubtypeExtraValue =
3443 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003444 final boolean isAuxiliary = "1".equals(String.valueOf(
3445 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003446 final InputMethodSubtype subtype =
3447 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3448 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3449 tempSubtypesArray.add(subtype);
3450 }
3451 }
3452 } catch (XmlPullParserException e) {
3453 Slog.w(TAG, "Error reading subtypes: " + e);
3454 return;
3455 } catch (java.io.IOException e) {
3456 Slog.w(TAG, "Error reading subtypes: " + e);
3457 return;
3458 } catch (NumberFormatException e) {
3459 Slog.w(TAG, "Error reading subtypes: " + e);
3460 return;
3461 } finally {
3462 if (fis != null) {
3463 try {
3464 fis.close();
3465 } catch (java.io.IOException e1) {
3466 Slog.w(TAG, "Failed to close.");
3467 }
3468 }
3469 }
3470 }
3471 }
3472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003474 // Utilities for debug
3475 private static String getStackTrace() {
3476 final StringBuilder sb = new StringBuilder();
3477 try {
3478 throw new RuntimeException();
3479 } catch (RuntimeException e) {
3480 final StackTraceElement[] frames = e.getStackTrace();
3481 // Start at 1 because the first frame is here and we don't care about it
3482 for (int j = 1; j < frames.length; ++j) {
3483 sb.append(frames[j].toString() + "\n");
3484 }
3485 }
3486 return sb.toString();
3487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 @Override
3490 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3491 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3492 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3495 + Binder.getCallingPid()
3496 + ", uid=" + Binder.getCallingUid());
3497 return;
3498 }
3499
3500 IInputMethod method;
3501 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 synchronized (mMethodMap) {
3506 p.println("Current Input Method Manager state:");
3507 int N = mMethodList.size();
3508 p.println(" Input Methods:");
3509 for (int i=0; i<N; i++) {
3510 InputMethodInfo info = mMethodList.get(i);
3511 p.println(" InputMethod #" + i + ":");
3512 info.dump(p, " ");
3513 }
3514 p.println(" Clients:");
3515 for (ClientState ci : mClients.values()) {
3516 p.println(" Client " + ci + ":");
3517 p.println(" client=" + ci.client);
3518 p.println(" inputContext=" + ci.inputContext);
3519 p.println(" sessionRequested=" + ci.sessionRequested);
3520 p.println(" curSession=" + ci.curSession);
3521 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003522 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003524 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3525 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3527 + " mBoundToMethod=" + mBoundToMethod);
3528 p.println(" mCurToken=" + mCurToken);
3529 p.println(" mCurIntent=" + mCurIntent);
3530 method = mCurMethod;
3531 p.println(" mCurMethod=" + mCurMethod);
3532 p.println(" mEnabledSession=" + mEnabledSession);
3533 p.println(" mShowRequested=" + mShowRequested
3534 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3535 + " mShowForced=" + mShowForced
3536 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003537 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003539
Jeff Brownb88102f2010-09-08 11:49:43 -07003540 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 pw.flush();
3543 try {
3544 client.client.asBinder().dump(fd, args);
3545 } catch (RemoteException e) {
3546 p.println("Input method client dead: " + e);
3547 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003548 } else {
3549 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003551
Jeff Brownb88102f2010-09-08 11:49:43 -07003552 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 pw.flush();
3555 try {
3556 method.asBinder().dump(fd, args);
3557 } catch (RemoteException e) {
3558 p.println("Input method service dead: " + e);
3559 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003560 } else {
3561 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563 }
3564}