blob: 8200a690e0dbacf19d7f31261f9c490848c3f645 [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 Kataoka7c4a2a12013-02-25 15:25:43 +0900794 // For secondary users, the list of enabled IMEs may not have been updated since the
795 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
796 // not be empty even if the IME has been uninstalled by the primary user.
797 // Even in such cases, IMMS works fine because it will find the most applicable
798 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900799 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900800 if (DEBUG) {
801 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
802 }
803 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900804 initialUserSwitch /* needsToResetDefaultIme */);
805 if (initialUserSwitch) {
806 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mContext.getPackageManager(),
807 mSettings.getEnabledInputMethodListLocked());
808 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900809 }
810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 @Override
812 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
813 throws RemoteException {
814 try {
815 return super.onTransact(code, data, reply, flags);
816 } catch (RuntimeException e) {
817 // The input method manager only throws security exceptions, so let's
818 // log all others.
819 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800820 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822 throw e;
823 }
824 }
825
Svetoslav Ganova0027152013-06-25 14:59:53 -0700826 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700827 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900828 if (DEBUG) {
829 Slog.d(TAG, "--- systemReady");
830 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700831 if (!mSystemReady) {
832 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900833 mKeyguardManager =
834 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700835 mNotificationManager = (NotificationManager)
836 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
837 mStatusBar = statusBar;
838 statusBar.setIconVisibility("ime", false);
839 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900840 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
841 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900842 if (mShowOngoingImeSwitcherForPhones) {
843 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
844 mHardKeyboardListener);
845 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900846 buildInputMethodListLocked(mMethodList, mMethodMap,
847 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900848 if (!mImeSelectedOnBoot) {
849 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900850 resetStateIfCurrentLocaleChangedLocked();
851 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(
852 mContext.getPackageManager(),
853 mSettings.getEnabledInputMethodListLocked());
satok0a1bcf42012-05-16 19:26:31 +0900854 }
855 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700856 try {
857 startInputInnerLocked();
858 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800859 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700860 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700861 }
862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800864
satok15452a42011-10-28 17:58:28 +0900865 private void setImeWindowVisibilityStatusHiddenLocked() {
866 mImeWindowVis = 0;
867 updateImeWindowStatusLocked();
868 }
869
satok3afd6c02011-11-18 08:38:19 +0900870 private void refreshImeWindowVisibilityLocked() {
871 final Configuration conf = mRes.getConfiguration();
872 final boolean haveHardKeyboard = conf.keyboard
873 != Configuration.KEYBOARD_NOKEYS;
874 final boolean hardKeyShown = haveHardKeyboard
875 && conf.hardKeyboardHidden
876 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500877 final boolean isScreenLocked =
878 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
879 final boolean isScreenSecurelyLocked =
880 isScreenLocked && mKeyguardManager.isKeyguardSecure();
881 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
882 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900883 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
884 updateImeWindowStatusLocked();
885 }
886
satok15452a42011-10-28 17:58:28 +0900887 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900888 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700889 }
890
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900891 // ---------------------------------------------------------------------------------------
892 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
893 // 1) it comes from the system process
894 // 2) the calling process' user id is identical to the current user id IMMS thinks.
895 private boolean calledFromValidUser() {
896 final int uid = Binder.getCallingUid();
897 final int userId = UserHandle.getUserId(uid);
898 if (DEBUG) {
899 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
900 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
901 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900902 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900903 }
904 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
905 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900906 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900907
908 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
909 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
910 // must not manage background users' states in any functions.
911 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
912 // by a token.
913 if (mContext.checkCallingOrSelfPermission(
914 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
915 == PackageManager.PERMISSION_GRANTED) {
916 if (DEBUG) {
917 Slog.d(TAG, "--- Access granted because the calling process has "
918 + "the INTERACT_ACROSS_USERS_FULL permission");
919 }
920 return true;
921 }
Satoshi Kataoka0766eb02013-07-31 18:30:13 +0900922 Slog.w(TAG, "--- IPC called from background users. Ignore. \n"
923 + InputMethodUtils.getStackTrace());
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900924 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900925 }
926
927 private boolean bindCurrentInputMethodService(
928 Intent service, ServiceConnection conn, int flags) {
929 if (service == null || conn == null) {
930 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
931 return false;
932 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800933 return mContext.bindServiceAsUser(service, conn, flags,
934 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900935 }
936
satoke7c6998e2011-06-03 17:57:59 +0900937 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900939 // TODO: Make this work even for non-current users?
940 if (!calledFromValidUser()) {
941 return Collections.emptyList();
942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 synchronized (mMethodMap) {
944 return new ArrayList<InputMethodInfo>(mMethodList);
945 }
946 }
947
satoke7c6998e2011-06-03 17:57:59 +0900948 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 // TODO: Make this work even for non-current users?
951 if (!calledFromValidUser()) {
952 return Collections.emptyList();
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900955 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957 }
958
satokbb4aa062011-01-19 21:40:27 +0900959 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
960 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
961 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
962 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900963 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900964 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900965 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900966 }
967 return enabledInputMethodAndSubtypes;
968 }
969
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900970 /**
971 * @param imi if null, returns enabled subtypes for the current imi
972 * @return enabled subtypes of the specified imi
973 */
satoke7c6998e2011-06-03 17:57:59 +0900974 @Override
satok16331c82010-12-20 23:48:46 +0900975 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
976 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900977 // TODO: Make this work even for non-current users?
978 if (!calledFromValidUser()) {
979 return Collections.emptyList();
980 }
satok67ddf9c2010-11-17 09:45:54 +0900981 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900982 if (imi == null && mCurMethodId != null) {
983 imi = mMethodMap.get(mCurMethodId);
984 }
985 return mSettings.getEnabledInputMethodSubtypeListLocked(
986 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900987 }
988 }
989
satoke7c6998e2011-06-03 17:57:59 +0900990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 public void addClient(IInputMethodClient client,
992 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900993 if (!calledFromValidUser()) {
994 return;
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 synchronized (mMethodMap) {
997 mClients.put(client.asBinder(), new ClientState(client,
998 inputContext, uid, pid));
999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
satoke7c6998e2011-06-03 17:57:59 +09001002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001004 if (!calledFromValidUser()) {
1005 return;
1006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001008 ClientState cs = mClients.remove(client.asBinder());
1009 if (cs != null) {
1010 clearClientSessionLocked(cs);
1011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void executeOrSendMessage(IInterface target, Message msg) {
1016 if (target.asBinder() instanceof Binder) {
1017 mCaller.sendMessage(msg);
1018 } else {
1019 handleMessage(msg);
1020 msg.recycle();
1021 }
1022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001023
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001024 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001026 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 + mCurClient.client.asBinder());
1028 if (mBoundToMethod) {
1029 mBoundToMethod = false;
1030 if (mCurMethod != null) {
1031 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1032 MSG_UNBIND_INPUT, mCurMethod));
1033 }
1034 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001035
1036 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1037 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1039 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1040 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
The Android Open Source Project10592532009-03-18 17:39:46 -07001043 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 private int getImeShowFlags() {
1048 int flags = 0;
1049 if (mShowForced) {
1050 flags |= InputMethod.SHOW_FORCED
1051 | InputMethod.SHOW_EXPLICIT;
1052 } else if (mShowExplicitlyRequested) {
1053 flags |= InputMethod.SHOW_EXPLICIT;
1054 }
1055 return flags;
1056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 private int getAppShowFlags() {
1059 int flags = 0;
1060 if (mShowForced) {
1061 flags |= InputMethodManager.SHOW_FORCED;
1062 } else if (!mShowExplicitlyRequested) {
1063 flags |= InputMethodManager.SHOW_IMPLICIT;
1064 }
1065 return flags;
1066 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001067
Dianne Hackborn7663d802012-02-24 13:08:49 -08001068 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 if (!mBoundToMethod) {
1070 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1071 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1072 mBoundToMethod = true;
1073 }
1074 final SessionState session = mCurClient.curSession;
1075 if (initial) {
1076 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1077 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1078 } else {
1079 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1080 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1081 }
1082 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001083 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001084 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001086 return new InputBindResult(session.session,
1087 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001091 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 // If no method is currently selected, do nothing.
1093 if (mCurMethodId == null) {
1094 return mNoBinding;
1095 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 ClientState cs = mClients.get(client.asBinder());
1098 if (cs == null) {
1099 throw new IllegalArgumentException("unknown client "
1100 + client.asBinder());
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 try {
1104 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1105 // Check with the window manager to make sure this client actually
1106 // has a window with focus. If not, reject. This is thread safe
1107 // because if the focus changes some time before or after, the
1108 // next client receiving focus that has any interest in input will
1109 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1112 return null;
1113 }
1114 } catch (RemoteException e) {
1115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001116
Dianne Hackborn7663d802012-02-24 13:08:49 -08001117 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1118 }
1119
1120 InputBindResult startInputUncheckedLocked(ClientState cs,
1121 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1122 // If no method is currently selected, do nothing.
1123 if (mCurMethodId == null) {
1124 return mNoBinding;
1125 }
1126
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001127 if (mCurClient == null) {
1128 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1129 if (DEBUG) {
1130 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1131 }
1132 }
1133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 if (mCurClient != cs) {
1135 // If the client is changing, we need to switch over to the new
1136 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001137 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001138 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 + cs.client.asBinder());
1140
1141 // If the screen is on, inform the new client it is active
1142 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001143 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1144 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 // Bump up the sequence for this client and attach it.
1149 mCurSeq++;
1150 if (mCurSeq <= 0) mCurSeq = 1;
1151 mCurClient = cs;
1152 mCurInputContext = inputContext;
1153 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 // Check if the input method is changing.
1156 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1157 if (cs.curSession != null) {
1158 // Fast case: if we are already connected to the input method,
1159 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001160 return attachNewInputLocked(
1161 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
1163 if (mHaveConnection) {
1164 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 // Return to client, and we will get back with it when
1166 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001167 requestClientSessionLocked(cs);
1168 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 } else if (SystemClock.uptimeMillis()
1170 < (mLastBindTime+TIME_TO_RECONNECT)) {
1171 // In this case we have connected to the service, but
1172 // don't yet have its interface. If it hasn't been too
1173 // long since we did the connection, we'll return to
1174 // the client and wait to get the service interface so
1175 // we can report back. If it has been too long, we want
1176 // to fall through so we can try a disconnect/reconnect
1177 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001178 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1181 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001186 return startInputInnerLocked();
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001189 InputBindResult startInputInnerLocked() {
1190 if (mCurMethodId == null) {
1191 return mNoBinding;
1192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001194 if (!mSystemReady) {
1195 // If the system is not yet ready, we shouldn't be running third
1196 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001197 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1201 if (info == null) {
1202 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001205 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1208 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001209 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1210 com.android.internal.R.string.input_method_binding_label);
1211 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1212 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001213 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornc8230512013-07-13 21:32:12 -07001214 | Context.BIND_NOT_VISIBLE | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mLastBindTime = SystemClock.uptimeMillis();
1216 mHaveConnection = true;
1217 mCurId = info.getId();
1218 mCurToken = new Binder();
1219 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001220 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 mIWindowManager.addWindowToken(mCurToken,
1222 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1223 } catch (RemoteException e) {
1224 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001225 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 } else {
1227 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001228 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 + mCurIntent);
1230 }
1231 return null;
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
satoke7c6998e2011-06-03 17:57:59 +09001234 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001236 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001237 if (!calledFromValidUser()) {
1238 return null;
1239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 synchronized (mMethodMap) {
1241 final long ident = Binder.clearCallingIdentity();
1242 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001243 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 } finally {
1245 Binder.restoreCallingIdentity(ident);
1246 }
1247 }
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
satoke7c6998e2011-06-03 17:57:59 +09001250 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public void finishInput(IInputMethodClient client) {
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
satoke7c6998e2011-06-03 17:57:59 +09001254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 public void onServiceConnected(ComponentName name, IBinder service) {
1256 synchronized (mMethodMap) {
1257 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1258 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001259 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001260 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001261 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001262 return;
1263 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001264 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001265 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1266 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001268 clearClientSessionLocked(mCurClient);
1269 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
1272 }
1273 }
1274
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1276 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 synchronized (mMethodMap) {
1278 if (mCurMethod != null && method != null
1279 && mCurMethod.asBinder() == method.asBinder()) {
1280 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001281 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001283 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001284 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 if (res.method != null) {
1286 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1287 MSG_BIND_METHOD, mCurClient.client, res));
1288 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001289 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 }
1291 }
1292 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001293
1294 // Session abandoned. Close its associated input channel.
1295 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001297
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001298 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001299 if (mVisibleBound) {
1300 mContext.unbindService(mVisibleConnection);
1301 mVisibleBound = false;
1302 }
1303
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001304 if (mHaveConnection) {
1305 mContext.unbindService(this);
1306 mHaveConnection = false;
1307 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001308
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001309 if (mCurToken != null) {
1310 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001311 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001312 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001313 // The current IME is shown. Hence an IME switch (transition) is happening.
1314 mWindowManagerService.saveLastInputMethodWindowForTransition();
1315 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001316 mIWindowManager.removeWindowToken(mCurToken);
1317 } catch (RemoteException e) {
1318 }
1319 mCurToken = null;
1320 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001321
The Android Open Source Project10592532009-03-18 17:39:46 -07001322 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001323 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001324
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001325 if (reportToClient && mCurClient != null) {
1326 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1327 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1328 }
1329 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001330
1331 void requestClientSessionLocked(ClientState cs) {
1332 if (!cs.sessionRequested) {
1333 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1334 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1335 cs.sessionRequested = true;
1336 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1337 MSG_CREATE_SESSION, mCurMethod, channels[1],
1338 new MethodCallback(this, mCurMethod, channels[0])));
1339 }
1340 }
1341
1342 void clearClientSessionLocked(ClientState cs) {
1343 finishSessionLocked(cs.curSession);
1344 cs.curSession = null;
1345 cs.sessionRequested = false;
1346 }
1347
1348 private void finishSessionLocked(SessionState sessionState) {
1349 if (sessionState != null) {
1350 if (sessionState.session != null) {
1351 try {
1352 sessionState.session.finishSession();
1353 } catch (RemoteException e) {
1354 Slog.w(TAG, "Session failed to close due to remote exception", e);
1355 setImeWindowVisibilityStatusHiddenLocked();
1356 }
1357 sessionState.session = null;
1358 }
1359 if (sessionState.channel != null) {
1360 sessionState.channel.dispose();
1361 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001362 }
1363 }
1364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001365
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001366 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 if (mCurMethod != null) {
1368 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001369 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001371
Jeff Brownc28867a2013-03-26 15:42:39 -07001372 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001373 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 mCurMethod = null;
1375 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001376 if (mStatusBar != null) {
1377 mStatusBar.setIconVisibility("ime", false);
1378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001380
satoke7c6998e2011-06-03 17:57:59 +09001381 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 public void onServiceDisconnected(ComponentName name) {
1383 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001384 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 + " mCurIntent=" + mCurIntent);
1386 if (mCurMethod != null && mCurIntent != null
1387 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001388 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 // We consider this to be a new bind attempt, since the system
1390 // should now try to restart the service for us.
1391 mLastBindTime = SystemClock.uptimeMillis();
1392 mShowRequested = mInputShown;
1393 mInputShown = false;
1394 if (mCurClient != null) {
1395 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1396 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1397 }
1398 }
1399 }
1400 }
1401
satokf9f01002011-05-19 21:31:50 +09001402 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001404 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 long ident = Binder.clearCallingIdentity();
1406 try {
1407 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001408 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 return;
1410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 synchronized (mMethodMap) {
1413 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001414 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001415 if (mStatusBar != null) {
1416 mStatusBar.setIconVisibility("ime", false);
1417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001420 CharSequence contentDescription = null;
1421 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001422 // Use PackageManager to load label
1423 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001424 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001425 mIPackageManager.getApplicationInfo(packageName, 0,
1426 mSettings.getCurrentUserId()));
1427 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001428 /* ignore */
1429 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001430 if (mStatusBar != null) {
1431 mStatusBar.setIcon("ime", packageName, iconId, 0,
1432 contentDescription != null
1433 ? contentDescription.toString() : null);
1434 mStatusBar.setIconVisibility("ime", true);
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
1437 }
1438 } finally {
1439 Binder.restoreCallingIdentity(ident);
1440 }
1441 }
1442
satok7cfc0ed2011-06-20 21:29:36 +09001443 private boolean needsToShowImeSwitchOngoingNotification() {
1444 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001445 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001446 synchronized (mMethodMap) {
1447 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1448 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001449 if (N > 2) return true;
1450 if (N < 1) return false;
1451 int nonAuxCount = 0;
1452 int auxCount = 0;
1453 InputMethodSubtype nonAuxSubtype = null;
1454 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001455 for(int i = 0; i < N; ++i) {
1456 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001457 final List<InputMethodSubtype> subtypes =
1458 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001459 final int subtypeCount = subtypes.size();
1460 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001461 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001462 } else {
1463 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001464 final InputMethodSubtype subtype = subtypes.get(j);
1465 if (!subtype.isAuxiliary()) {
1466 ++nonAuxCount;
1467 nonAuxSubtype = subtype;
1468 } else {
1469 ++auxCount;
1470 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001471 }
1472 }
1473 }
satok7cfc0ed2011-06-20 21:29:36 +09001474 }
satokb6359412011-06-28 17:47:41 +09001475 if (nonAuxCount > 1 || auxCount > 1) {
1476 return true;
1477 } else if (nonAuxCount == 1 && auxCount == 1) {
1478 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001479 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1480 || auxSubtype.overridesImplicitlyEnabledSubtype()
1481 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001482 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1483 return false;
1484 }
1485 return true;
1486 }
1487 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001488 }
satok7cfc0ed2011-06-20 21:29:36 +09001489 }
1490
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001491 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001492 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001493 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001494 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001495 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001496 try {
1497 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001498 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001499 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001500 return;
1501 }
1502
1503 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001504 mImeWindowVis = vis;
1505 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001506 if (mStatusBar != null) {
1507 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1508 }
jungheang.lee217fd292013-02-26 16:53:22 +09001509 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1510 && (mWindowManagerService.isHardKeyboardAvailable()
1511 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001512 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1513 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001514 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001515 final CharSequence title = mRes.getText(
1516 com.android.internal.R.string.select_input_method);
Satoshi Kataokab2827262013-07-04 19:43:14 +09001517 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1518 mContext, imi, mCurrentSubtype);
satok5bc8e732011-07-22 21:07:23 +09001519
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) {
Satoshi Kataokab2827262013-07-04 19:43:14 +09001524 Slog.d(TAG, "--- show notification: label = " + summary);
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001525 }
1526 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001527 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001528 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001529 mNotificationShown = true;
1530 }
satok7cfc0ed2011-06-20 21:29:36 +09001531 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001532 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001533 if (DEBUG) {
1534 Slog.d(TAG, "--- hide notification");
1535 }
1536 mNotificationManager.cancelAsUser(null,
1537 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001538 mNotificationShown = false;
1539 }
1540 }
satok06487a52010-10-29 11:37:18 +09001541 }
1542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
1545 }
1546
satoke7c6998e2011-06-03 17:57:59 +09001547 @Override
satokf9f01002011-05-19 21:31:50 +09001548 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001549 if (!calledFromValidUser()) {
1550 return;
1551 }
satokf9f01002011-05-19 21:31:50 +09001552 synchronized (mMethodMap) {
1553 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1554 for (int i = 0; i < spans.length; ++i) {
1555 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001556 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001557 mSecureSuggestionSpans.put(ss, currentImi);
1558 }
1559 }
1560 }
1561 }
1562
satoke7c6998e2011-06-03 17:57:59 +09001563 @Override
satokf9f01002011-05-19 21:31:50 +09001564 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001565 if (!calledFromValidUser()) {
1566 return false;
1567 }
satokf9f01002011-05-19 21:31:50 +09001568 synchronized (mMethodMap) {
1569 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1570 // TODO: Do not send the intent if the process of the targetImi is already dead.
1571 if (targetImi != null) {
1572 final String[] suggestions = span.getSuggestions();
1573 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001574 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001575 final Intent intent = new Intent();
1576 // Ensures that only a class in the original IME package will receive the
1577 // notification.
satok42c5a162011-05-26 16:46:14 +09001578 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001579 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1580 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1582 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001583 final long ident = Binder.clearCallingIdentity();
1584 try {
1585 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1586 } finally {
1587 Binder.restoreCallingIdentity(ident);
1588 }
satokf9f01002011-05-19 21:31:50 +09001589 return true;
1590 }
1591 }
1592 return false;
1593 }
1594
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001595 void updateFromSettingsLocked(boolean enabledMayChange) {
1596 if (enabledMayChange) {
1597 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1598 for (int i=0; i<enabled.size(); i++) {
1599 // We allow the user to select "disabled until used" apps, so if they
1600 // are enabling one of those here we now need to make it enabled.
1601 InputMethodInfo imm = enabled.get(i);
1602 try {
1603 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1604 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1605 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001606 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001607 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001608 if (DEBUG) {
1609 Slog.d(TAG, "Update state(" + imm.getId()
1610 + "): DISABLED_UNTIL_USED -> DEFAULT");
1611 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001612 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1613 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001614 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1615 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001616 }
1617 } catch (RemoteException e) {
1618 }
1619 }
1620 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001621 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1622 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1623 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1624 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001625 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001626 // There is no input method selected, try to choose new applicable input method.
1627 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001628 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001629 }
1630 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001632 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001634 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001635 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001636 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
satokf3db1af2010-11-23 13:34:33 +09001638 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001639 } else {
1640 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001641 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001642 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 }
1644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
satokab751aa2010-09-14 19:17:36 +09001646 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 InputMethodInfo info = mMethodMap.get(id);
1648 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001649 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001651
satokd81e9502012-05-21 12:58:45 +09001652 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001654 final int subtypeCount = info.getSubtypeCount();
1655 if (subtypeCount <= 0) {
1656 return;
satokcd7cd292010-11-20 15:46:23 +09001657 }
satokd81e9502012-05-21 12:58:45 +09001658 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1659 final InputMethodSubtype newSubtype;
1660 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1661 newSubtype = info.getSubtypeAt(subtypeId);
1662 } else {
1663 // If subtype is null, try to find the most applicable one from
1664 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001665 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001666 }
1667 if (newSubtype == null || oldSubtype == null) {
1668 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1669 + ", new subtype = " + newSubtype);
1670 return;
1671 }
1672 if (newSubtype != oldSubtype) {
1673 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1674 if (mCurMethod != null) {
1675 try {
1676 refreshImeWindowVisibilityLocked();
1677 mCurMethod.changeInputMethodSubtype(newSubtype);
1678 } catch (RemoteException e) {
1679 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001680 }
1681 }
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 return;
1684 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001685
satokd81e9502012-05-21 12:58:45 +09001686 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 final long ident = Binder.clearCallingIdentity();
1688 try {
satokab751aa2010-09-14 19:17:36 +09001689 // Set a subtype to this input method.
1690 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001691 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1692 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1693 // because mCurMethodId is stored as a history in
1694 // setSelectedInputMethodAndSubtypeLocked().
1695 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696
1697 if (ActivityManagerNative.isSystemReady()) {
1698 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001699 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001701 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001703 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 } finally {
1705 Binder.restoreCallingIdentity(ident);
1706 }
1707 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001708
satok42c5a162011-05-26 16:46:14 +09001709 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001710 public boolean showSoftInput(IInputMethodClient client, int flags,
1711 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001712 if (!calledFromValidUser()) {
1713 return false;
1714 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001715 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long ident = Binder.clearCallingIdentity();
1717 try {
1718 synchronized (mMethodMap) {
1719 if (mCurClient == null || client == null
1720 || mCurClient.client.asBinder() != client.asBinder()) {
1721 try {
1722 // We need to check if this is the current client with
1723 // focus in the window manager, to allow this call to
1724 // be made before input is started in it.
1725 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001726 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001727 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001733
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001734 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001735 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
1737 } finally {
1738 Binder.restoreCallingIdentity(ident);
1739 }
1740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project4df24232009-03-05 14:34:35 -08001742 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 mShowRequested = true;
1744 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1745 mShowExplicitlyRequested = true;
1746 }
1747 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1748 mShowExplicitlyRequested = true;
1749 mShowForced = true;
1750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001751
Dianne Hackborncc278702009-09-02 23:07:23 -07001752 if (!mSystemReady) {
1753 return false;
1754 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001755
The Android Open Source Project4df24232009-03-05 14:34:35 -08001756 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001758 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001759 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1760 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1761 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001763 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001764 bindCurrentInputMethodService(
1765 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001766 mVisibleBound = true;
1767 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001768 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001770 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 // The client has asked to have the input method shown, but
1772 // we have been sitting here too long with a connection to the
1773 // service and no interface received, so let's disconnect/connect
1774 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001777 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001779 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001780 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001781 } else {
1782 if (DEBUG) {
1783 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1784 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001787
The Android Open Source Project4df24232009-03-05 14:34:35 -08001788 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001790
satok42c5a162011-05-26 16:46:14 +09001791 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001792 public boolean hideSoftInput(IInputMethodClient client, int flags,
1793 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001794 if (!calledFromValidUser()) {
1795 return false;
1796 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001797 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 long ident = Binder.clearCallingIdentity();
1799 try {
1800 synchronized (mMethodMap) {
1801 if (mCurClient == null || client == null
1802 || mCurClient.client.asBinder() != client.asBinder()) {
1803 try {
1804 // We need to check if this is the current client with
1805 // focus in the window manager, to allow this call to
1806 // be made before input is started in it.
1807 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001808 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1809 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001810 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001811 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001814 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001815 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001818
Joe Onorato8a9b2202010-02-26 18:56:32 -08001819 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001820 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
1822 } finally {
1823 Binder.restoreCallingIdentity(ident);
1824 }
1825 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001826
The Android Open Source Project4df24232009-03-05 14:34:35 -08001827 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1829 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001830 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 -08001831 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001834 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 -08001835 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001837 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001839 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1840 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1841 res = true;
1842 } else {
1843 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001845 if (mHaveConnection && mVisibleBound) {
1846 mContext.unbindService(mVisibleConnection);
1847 mVisibleBound = false;
1848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 mInputShown = false;
1850 mShowRequested = false;
1851 mShowExplicitlyRequested = false;
1852 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001853 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001855
satok42c5a162011-05-26 16:46:14 +09001856 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001857 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1858 int controlFlags, int softInputMode, int windowFlags,
1859 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001860 // Needs to check the validity before clearing calling identity
1861 final boolean calledFromValidUser = calledFromValidUser();
1862
Dianne Hackborn7663d802012-02-24 13:08:49 -08001863 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 long ident = Binder.clearCallingIdentity();
1865 try {
1866 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001867 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001868 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001870 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001871
Dianne Hackborn7663d802012-02-24 13:08:49 -08001872 ClientState cs = mClients.get(client.asBinder());
1873 if (cs == null) {
1874 throw new IllegalArgumentException("unknown client "
1875 + client.asBinder());
1876 }
1877
1878 try {
1879 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1880 // Check with the window manager to make sure this client actually
1881 // has a window with focus. If not, reject. This is thread safe
1882 // because if the focus changes some time before or after, the
1883 // next client receiving focus that has any interest in input will
1884 // be calling through here after that change happens.
1885 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1886 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1887 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001889 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001891
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001892 if (!calledFromValidUser) {
1893 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1894 Slog.w(TAG, "If you want to interect with IME, you need "
1895 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1896 hideCurrentInputLocked(0, null);
1897 return null;
1898 }
1899
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001900 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001901 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001902 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001903 if (attribute != null) {
1904 return startInputUncheckedLocked(cs, inputContext, attribute,
1905 controlFlags);
1906 }
1907 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001908 }
1909 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001911 // Should we auto-show the IME even if the caller has not
1912 // specified what should be done with it?
1913 // We only do this automatically if the window can resize
1914 // to accommodate the IME (so what the user sees will give
1915 // them good context without input information being obscured
1916 // by the IME) or if running on a large screen where there
1917 // is more room for the target window + IME.
1918 final boolean doAutoShow =
1919 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1920 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1921 || mRes.getConfiguration().isLayoutSizeAtLeast(
1922 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001923 final boolean isTextEditor =
1924 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1925
1926 // We want to start input before showing the IME, but after closing
1927 // it. We want to do this after closing it to help the IME disappear
1928 // more quickly (not get stuck behind it initializing itself for the
1929 // new focused input, even if its window wants to hide the IME).
1930 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1933 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001934 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1936 // There is no focus view, and this window will
1937 // be behind any soft input window, so hide the
1938 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001939 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001940 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001942 } else if (isTextEditor && doAutoShow && (softInputMode &
1943 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 // There is a focus view, and we are navigating forward
1945 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001946 // We only do this automatically if the window can resize
1947 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001948 // them good context without input information being obscured
1949 // by the IME) or if running on a large screen where there
1950 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001951 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001952 if (attribute != null) {
1953 res = startInputUncheckedLocked(cs, inputContext, attribute,
1954 controlFlags);
1955 didStart = true;
1956 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001957 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 break;
1960 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1961 // Do nothing.
1962 break;
1963 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1964 if ((softInputMode &
1965 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001966 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001967 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 }
1969 break;
1970 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001971 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001972 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 break;
1974 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1975 if ((softInputMode &
1976 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001978 if (attribute != null) {
1979 res = startInputUncheckedLocked(cs, inputContext, attribute,
1980 controlFlags);
1981 didStart = true;
1982 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001983 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985 break;
1986 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001987 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001988 if (attribute != null) {
1989 res = startInputUncheckedLocked(cs, inputContext, attribute,
1990 controlFlags);
1991 didStart = true;
1992 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001993 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 break;
1995 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001996
1997 if (!didStart && attribute != null) {
1998 res = startInputUncheckedLocked(cs, inputContext, attribute,
1999 controlFlags);
2000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002 } finally {
2003 Binder.restoreCallingIdentity(ident);
2004 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002005
2006 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
satok42c5a162011-05-26 16:46:14 +09002009 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002011 if (!calledFromValidUser()) {
2012 return;
2013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 synchronized (mMethodMap) {
2015 if (mCurClient == null || client == null
2016 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002017 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002018 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020
satok440aab52010-11-25 09:43:11 +09002021 // Always call subtype picker, because subtype picker is a superset of input method
2022 // picker.
satokab751aa2010-09-14 19:17:36 +09002023 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2024 }
2025 }
2026
satok42c5a162011-05-26 16:46:14 +09002027 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002029 if (!calledFromValidUser()) {
2030 return;
2031 }
satok28203512010-11-24 11:06:49 +09002032 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2033 }
2034
satok42c5a162011-05-26 16:46:14 +09002035 @Override
satok28203512010-11-24 11:06:49 +09002036 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002037 if (!calledFromValidUser()) {
2038 return;
2039 }
satok28203512010-11-24 11:06:49 +09002040 synchronized (mMethodMap) {
2041 if (subtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002042 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002043 mMethodMap.get(id), subtype.hashCode()));
2044 } else {
2045 setInputMethod(token, id);
2046 }
2047 }
satokab751aa2010-09-14 19:17:36 +09002048 }
2049
satok42c5a162011-05-26 16:46:14 +09002050 @Override
satokb416a712010-11-25 20:42:14 +09002051 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002052 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002053 if (!calledFromValidUser()) {
2054 return;
2055 }
satokb416a712010-11-25 20:42:14 +09002056 synchronized (mMethodMap) {
2057 if (mCurClient == null || client == null
2058 || mCurClient.client.asBinder() != client.asBinder()) {
2059 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2060 }
satok7fee71f2010-12-17 18:54:26 +09002061 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2062 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002063 }
2064 }
2065
satok4fc87d62011-05-20 16:13:43 +09002066 @Override
satok735cf382010-11-11 20:40:09 +09002067 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002068 if (!calledFromValidUser()) {
2069 return false;
2070 }
satok735cf382010-11-11 20:40:09 +09002071 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002072 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002073 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002074 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002075 lastImi = mMethodMap.get(lastIme.first);
2076 } else {
2077 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002078 }
satok4fc87d62011-05-20 16:13:43 +09002079 String targetLastImiId = null;
2080 int subtypeId = NOT_A_SUBTYPE_ID;
2081 if (lastIme != null && lastImi != null) {
2082 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2083 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2084 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2085 : mCurrentSubtype.hashCode();
2086 // If the last IME is the same as the current IME and the last subtype is not
2087 // defined, there is no need to switch to the last IME.
2088 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2089 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002090 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002091 }
2092 }
2093
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002094 if (TextUtils.isEmpty(targetLastImiId)
2095 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002096 // This is a safety net. If the currentSubtype can't be added to the history
2097 // and the framework couldn't find the last ime, we will make the last ime be
2098 // the most applicable enabled keyboard subtype of the system imes.
2099 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2100 if (enabled != null) {
2101 final int N = enabled.size();
2102 final String locale = mCurrentSubtype == null
2103 ? mRes.getConfiguration().locale.toString()
2104 : mCurrentSubtype.getLocale();
2105 for (int i = 0; i < N; ++i) {
2106 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002107 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002108 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002109 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2110 InputMethodUtils.getSubtypes(imi),
2111 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002112 if (keyboardSubtype != null) {
2113 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002114 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002115 imi, keyboardSubtype.hashCode());
2116 if(keyboardSubtype.getLocale().equals(locale)) {
2117 break;
2118 }
2119 }
2120 }
2121 }
2122 }
2123 }
2124
2125 if (!TextUtils.isEmpty(targetLastImiId)) {
2126 if (DEBUG) {
2127 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2128 + ", from: " + mCurMethodId + ", " + subtypeId);
2129 }
2130 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2131 return true;
2132 } else {
2133 return false;
2134 }
satok735cf382010-11-11 20:40:09 +09002135 }
2136 }
2137
satoke7c6998e2011-06-03 17:57:59 +09002138 @Override
satok688bd472012-02-09 20:09:17 +09002139 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002140 if (!calledFromValidUser()) {
2141 return false;
2142 }
satok688bd472012-02-09 20:09:17 +09002143 synchronized (mMethodMap) {
2144 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2145 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2146 if (nextSubtype == null) {
2147 return false;
2148 }
2149 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2150 return true;
2151 }
2152 }
2153
2154 @Override
satok68f1b782011-04-11 14:26:04 +09002155 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002156 if (!calledFromValidUser()) {
2157 return null;
2158 }
satok68f1b782011-04-11 14:26:04 +09002159 synchronized (mMethodMap) {
2160 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2161 // TODO: Handle the case of the last IME with no subtypes
2162 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2163 || TextUtils.isEmpty(lastIme.second)) return null;
2164 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2165 if (lastImi == null) return null;
2166 try {
2167 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002168 final int lastSubtypeId =
2169 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002170 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2171 return null;
2172 }
2173 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002174 } catch (NumberFormatException e) {
2175 return null;
2176 }
2177 }
2178 }
2179
satoke7c6998e2011-06-03 17:57:59 +09002180 @Override
satokee5e77c2011-09-02 18:50:15 +09002181 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002182 if (!calledFromValidUser()) {
2183 return;
2184 }
satok91e88122011-07-18 11:11:42 +09002185 // By this IPC call, only a process which shares the same uid with the IME can add
2186 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002187 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002188 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002189 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002190 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002191 final String[] packageInfos;
2192 try {
2193 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2194 } catch (RemoteException e) {
2195 Slog.e(TAG, "Failed to get package infos");
2196 return;
2197 }
satok91e88122011-07-18 11:11:42 +09002198 if (packageInfos != null) {
2199 final int packageNum = packageInfos.length;
2200 for (int i = 0; i < packageNum; ++i) {
2201 if (packageInfos[i].equals(imi.getPackageName())) {
2202 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002203 final long ident = Binder.clearCallingIdentity();
2204 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002205 buildInputMethodListLocked(mMethodList, mMethodMap,
2206 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002207 } finally {
2208 Binder.restoreCallingIdentity(ident);
2209 }
satokee5e77c2011-09-02 18:50:15 +09002210 return;
satok91e88122011-07-18 11:11:42 +09002211 }
2212 }
2213 }
satoke7c6998e2011-06-03 17:57:59 +09002214 }
satokee5e77c2011-09-02 18:50:15 +09002215 return;
satoke7c6998e2011-06-03 17:57:59 +09002216 }
2217
satok28203512010-11-24 11:06:49 +09002218 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 synchronized (mMethodMap) {
2220 if (token == null) {
2221 if (mContext.checkCallingOrSelfPermission(
2222 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2223 != PackageManager.PERMISSION_GRANTED) {
2224 throw new SecurityException(
2225 "Using null token requires permission "
2226 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2227 }
2228 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002229 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2230 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 return;
2232 }
2233
satokc5933802011-08-31 21:26:04 +09002234 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 try {
satokab751aa2010-09-14 19:17:36 +09002236 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 } finally {
2238 Binder.restoreCallingIdentity(ident);
2239 }
2240 }
2241 }
2242
satok42c5a162011-05-26 16:46:14 +09002243 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002245 if (!calledFromValidUser()) {
2246 return;
2247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 synchronized (mMethodMap) {
2249 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002250 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2251 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 return;
2253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 long ident = Binder.clearCallingIdentity();
2255 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002256 hideCurrentInputLocked(flags, null);
2257 } finally {
2258 Binder.restoreCallingIdentity(ident);
2259 }
2260 }
2261 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002262
satok42c5a162011-05-26 16:46:14 +09002263 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002264 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002265 if (!calledFromValidUser()) {
2266 return;
2267 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002268 synchronized (mMethodMap) {
2269 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002270 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2271 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002272 return;
2273 }
2274 long ident = Binder.clearCallingIdentity();
2275 try {
2276 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 } finally {
2278 Binder.restoreCallingIdentity(ident);
2279 }
2280 }
2281 }
2282
2283 void setEnabledSessionInMainThread(SessionState session) {
2284 if (mEnabledSession != session) {
2285 if (mEnabledSession != null) {
2286 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002287 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 mEnabledSession.method.setSessionEnabled(
2289 mEnabledSession.session, false);
2290 } catch (RemoteException e) {
2291 }
2292 }
2293 mEnabledSession = session;
2294 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002295 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 session.method.setSessionEnabled(
2297 session.session, true);
2298 } catch (RemoteException e) {
2299 }
2300 }
2301 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002302
satok42c5a162011-05-26 16:46:14 +09002303 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 switch (msg.what) {
2307 case MSG_SHOW_IM_PICKER:
2308 showInputMethodMenu();
2309 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002310
satokab751aa2010-09-14 19:17:36 +09002311 case MSG_SHOW_IM_SUBTYPE_PICKER:
2312 showInputMethodSubtypeMenu();
2313 return true;
2314
satok47a44912010-10-06 16:03:58 +09002315 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002316 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002317 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002318 args.recycle();
satok217f5482010-12-15 05:19:19 +09002319 return true;
2320
2321 case MSG_SHOW_IM_CONFIG:
2322 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002323 return true;
2324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 case MSG_UNBIND_INPUT:
2328 try {
2329 ((IInputMethod)msg.obj).unbindInput();
2330 } catch (RemoteException e) {
2331 // There is nothing interesting about the method dying.
2332 }
2333 return true;
2334 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 try {
2337 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2338 } catch (RemoteException e) {
2339 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002340 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 return true;
2342 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002343 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002345 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002346 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002347 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 } catch (RemoteException e) {
2349 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002350 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 return true;
2352 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002353 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002354 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002355 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002356 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002357 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 } catch (RemoteException e) {
2359 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002360 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 return true;
2362 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002363 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002365 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2367 } catch (RemoteException e) {
2368 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002369 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002371 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002372 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002373 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002374 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002376 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002378 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002379 // Dispose the channel if the input method is not local to this process
2380 // because the remote proxy will get its own copy when unparceled.
2381 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002382 channel.dispose();
2383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002385 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002391 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 try {
2393 SessionState session = (SessionState)args.arg1;
2394 setEnabledSessionInMainThread(session);
2395 session.method.startInput((IInputContext)args.arg2,
2396 (EditorInfo)args.arg3);
2397 } catch (RemoteException e) {
2398 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002399 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 return true;
2401 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002402 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 try {
2404 SessionState session = (SessionState)args.arg1;
2405 setEnabledSessionInMainThread(session);
2406 session.method.restartInput((IInputContext)args.arg2,
2407 (EditorInfo)args.arg3);
2408 } catch (RemoteException e) {
2409 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002410 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 case MSG_UNBIND_METHOD:
2416 try {
2417 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2418 } catch (RemoteException e) {
2419 // There is nothing interesting about the last client dying.
2420 }
2421 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002422 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002423 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002424 IInputMethodClient client = (IInputMethodClient)args.arg1;
2425 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002427 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002429 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002430 } finally {
2431 // Dispose the channel if the input method is not local to this process
2432 // because the remote proxy will get its own copy when unparceled.
2433 if (res.channel != null && Binder.isProxy(client)) {
2434 res.channel.dispose();
2435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002437 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002439 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002440 case MSG_SET_ACTIVE:
2441 try {
2442 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2443 } catch (RemoteException e) {
2444 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2445 + ((ClientState)msg.obj).pid + " uid "
2446 + ((ClientState)msg.obj).uid);
2447 }
2448 return true;
satok01038492012-04-09 21:08:27 +09002449
2450 // --------------------------------------------------------------
2451 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2452 mHardKeyboardListener.handleHardKeyboardStatusChange(
2453 msg.arg1 == 1, msg.arg2 == 1);
2454 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 }
2456 return false;
2457 }
2458
satokdc9ddae2011-10-06 12:22:36 +09002459 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002460 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2461 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002462 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002463 if (DEBUG) {
2464 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2465 }
satok723a27e2010-11-11 14:58:11 +09002466 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002467 return true;
2468 }
2469
2470 return false;
2471 }
2472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002474 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002475 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002476 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Satoshi Kataoka0766eb02013-07-31 18:30:13 +09002477 + " \n ------ \n" + InputMethodUtils.getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 list.clear();
2480 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002481
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002482 // Use for queryIntentServicesAsUser
2483 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002484 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002485 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002487 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002488 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002489 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2490 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002491
satoke7c6998e2011-06-03 17:57:59 +09002492 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2493 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 for (int i = 0; i < services.size(); ++i) {
2495 ResolveInfo ri = services.get(i);
2496 ServiceInfo si = ri.serviceInfo;
2497 ComponentName compName = new ComponentName(si.packageName, si.name);
2498 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2499 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002500 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 + ": it does not require the permission "
2502 + android.Manifest.permission.BIND_INPUT_METHOD);
2503 continue;
2504 }
2505
Joe Onorato8a9b2202010-02-26 18:56:32 -08002506 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507
2508 try {
satoke7c6998e2011-06-03 17:57:59 +09002509 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002511 final String id = p.getId();
2512 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513
2514 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002515 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002519 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002521 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
2523 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002524
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002525 if (resetDefaultEnabledIme) {
2526 final ArrayList<InputMethodInfo> defaultEnabledIme =
2527 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2528 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2529 final InputMethodInfo imi = defaultEnabledIme.get(i);
2530 if (DEBUG) {
2531 Slog.d(TAG, "--- enable ime = " + imi);
2532 }
2533 setInputMethodEnabledLocked(imi.getId(), true);
2534 }
2535 }
2536
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002537 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002538 if (!TextUtils.isEmpty(defaultImiId)) {
2539 if (!map.containsKey(defaultImiId)) {
2540 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2541 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002542 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002543 }
2544 } else {
2545 // Double check that the default IME is certainly enabled.
2546 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002547 }
2548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002552
satokab751aa2010-09-14 19:17:36 +09002553 private void showInputMethodMenu() {
2554 showInputMethodMenuInternal(false);
2555 }
2556
2557 private void showInputMethodSubtypeMenu() {
2558 showInputMethodMenuInternal(true);
2559 }
2560
satok217f5482010-12-15 05:19:19 +09002561 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002562 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002563 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002564 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2565 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002566 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002567 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002568 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002569 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002570 }
2571
2572 private void showConfigureInputMethods() {
2573 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2574 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2575 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2576 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002577 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002578 }
2579
satok2c93efc2012-04-02 19:33:47 +09002580 private boolean isScreenLocked() {
2581 return mKeyguardManager != null
2582 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2583 }
satokab751aa2010-09-14 19:17:36 +09002584 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002585 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002588 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002589
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002590 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002591 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002592 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002593
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002594 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002595 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2596 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002597 if (immis == null || immis.size() == 0) {
2598 return;
2599 }
2600
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002601 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602
satok688bd472012-02-09 20:09:17 +09002603 final List<ImeSubtypeListItem> imList =
2604 mImListManager.getSortedInputMethodAndSubtypeList(
2605 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002606
satokc3690562012-01-10 20:14:43 +09002607 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002608 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002609 if (currentSubtype != null) {
2610 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002611 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2612 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002613 }
2614 }
2615
Ken Wakasa761eb372011-03-04 19:06:18 +09002616 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002617 mIms = new InputMethodInfo[N];
2618 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 int checkedItem = 0;
2620 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002621 final ImeSubtypeListItem item = imList.get(i);
2622 mIms[i] = item.mImi;
2623 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002624 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002625 int subtypeId = mSubtypeIds[i];
2626 if ((subtypeId == NOT_A_SUBTYPE_ID)
2627 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2628 || (subtypeId == lastInputMethodSubtypeId)) {
2629 checkedItem = i;
2630 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002633 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002634 com.android.internal.R.styleable.DialogPreference,
2635 com.android.internal.R.attr.alertDialogStyle, 0);
2636 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002637 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002638 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002639 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002641 }
2642 })
2643 .setIcon(a.getDrawable(
2644 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2645 a.recycle();
satok01038492012-04-09 21:08:27 +09002646 final LayoutInflater inflater =
2647 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2648 final View tv = inflater.inflate(
2649 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2650 mDialogBuilder.setCustomTitle(tv);
2651
2652 // Setup layout for a toggle switch of the hardware keyboard
2653 mSwitchingDialogTitleView = tv;
2654 mSwitchingDialogTitleView.findViewById(
2655 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2656 mWindowManagerService.isHardKeyboardAvailable() ?
2657 View.VISIBLE : View.GONE);
2658 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2659 com.android.internal.R.id.hard_keyboard_switch));
2660 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2661 hardKeySwitch.setOnCheckedChangeListener(
2662 new OnCheckedChangeListener() {
2663 @Override
2664 public void onCheckedChanged(
2665 CompoundButton buttonView, boolean isChecked) {
2666 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002667 // Ensure that the input method dialog is dismissed when changing
2668 // the hardware keyboard state.
2669 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002670 }
2671 });
satokd87c2592010-09-29 11:52:06 +09002672
Ken Wakasa05dbb652011-08-22 15:22:43 +09002673 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2674 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2675 checkedItem);
2676
2677 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002678 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002679 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002680 public void onClick(DialogInterface dialog, int which) {
2681 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002682 if (mIms == null || mIms.length <= which
2683 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002684 return;
2685 }
2686 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002687 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002688 adapter.mCheckedItem = which;
2689 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002690 hideInputMethodMenu();
2691 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002692 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002693 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002694 subtypeId = NOT_A_SUBTYPE_ID;
2695 }
2696 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002697 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002700 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
satokbc81b692011-08-26 16:22:22 +09002702 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002703 mDialogBuilder.setPositiveButton(
2704 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002705 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002706 @Override
satok7f35c8c2010-10-07 21:13:11 +09002707 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002708 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002709 }
2710 });
2711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002713 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 mSwitchingDialog.getWindow().setType(
2715 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002716 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2717 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002718 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 mSwitchingDialog.show();
2720 }
2721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002722
satok93d744d2012-05-09 17:14:08 +09002723 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002724 public final CharSequence mImeName;
2725 public final CharSequence mSubtypeName;
2726 public final InputMethodInfo mImi;
2727 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002728 private final boolean mIsSystemLocale;
2729 private final boolean mIsSystemLanguage;
2730
Ken Wakasa05dbb652011-08-22 15:22:43 +09002731 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002732 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002733 mImeName = imeName;
2734 mSubtypeName = subtypeName;
2735 mImi = imi;
2736 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002737 if (TextUtils.isEmpty(subtypeLocale)) {
2738 mIsSystemLocale = false;
2739 mIsSystemLanguage = false;
2740 } else {
2741 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2742 mIsSystemLanguage = mIsSystemLocale
2743 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2744 }
2745 }
2746
2747 @Override
2748 public int compareTo(ImeSubtypeListItem other) {
2749 if (TextUtils.isEmpty(mImeName)) {
2750 return 1;
2751 }
2752 if (TextUtils.isEmpty(other.mImeName)) {
2753 return -1;
2754 }
2755 if (!TextUtils.equals(mImeName, other.mImeName)) {
2756 return mImeName.toString().compareTo(other.mImeName.toString());
2757 }
2758 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2759 return 0;
2760 }
2761 if (mIsSystemLocale) {
2762 return -1;
2763 }
2764 if (other.mIsSystemLocale) {
2765 return 1;
2766 }
2767 if (mIsSystemLanguage) {
2768 return -1;
2769 }
2770 if (other.mIsSystemLanguage) {
2771 return 1;
2772 }
2773 if (TextUtils.isEmpty(mSubtypeName)) {
2774 return 1;
2775 }
2776 if (TextUtils.isEmpty(other.mSubtypeName)) {
2777 return -1;
2778 }
2779 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002780 }
2781 }
2782
2783 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2784 private final LayoutInflater mInflater;
2785 private final int mTextViewResourceId;
2786 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002787 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002788 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2789 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2790 super(context, textViewResourceId, itemsList);
2791 mTextViewResourceId = textViewResourceId;
2792 mItemsList = itemsList;
2793 mCheckedItem = checkedItem;
2794 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2795 }
2796
2797 @Override
2798 public View getView(int position, View convertView, ViewGroup parent) {
2799 final View view = convertView != null ? convertView
2800 : mInflater.inflate(mTextViewResourceId, null);
2801 if (position < 0 || position >= mItemsList.size()) return view;
2802 final ImeSubtypeListItem item = mItemsList.get(position);
2803 final CharSequence imeName = item.mImeName;
2804 final CharSequence subtypeName = item.mSubtypeName;
2805 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2806 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2807 if (TextUtils.isEmpty(subtypeName)) {
2808 firstTextView.setText(imeName);
2809 secondTextView.setVisibility(View.GONE);
2810 } else {
2811 firstTextView.setText(subtypeName);
2812 secondTextView.setText(imeName);
2813 secondTextView.setVisibility(View.VISIBLE);
2814 }
2815 final RadioButton radioButton =
2816 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2817 radioButton.setChecked(position == mCheckedItem);
2818 return view;
2819 }
2820 }
2821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002823 synchronized (mMethodMap) {
2824 hideInputMethodMenuLocked();
2825 }
2826 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002827
The Android Open Source Project10592532009-03-18 17:39:46 -07002828 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002829 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830
The Android Open Source Project10592532009-03-18 17:39:46 -07002831 if (mSwitchingDialog != null) {
2832 mSwitchingDialog.dismiss();
2833 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002835
The Android Open Source Project10592532009-03-18 17:39:46 -07002836 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002837 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002841
satok42c5a162011-05-26 16:46:14 +09002842 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002844 // TODO: Make this work even for non-current users?
2845 if (!calledFromValidUser()) {
2846 return false;
2847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 synchronized (mMethodMap) {
2849 if (mContext.checkCallingOrSelfPermission(
2850 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2851 != PackageManager.PERMISSION_GRANTED) {
2852 throw new SecurityException(
2853 "Requires permission "
2854 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2855 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 long ident = Binder.clearCallingIdentity();
2858 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002859 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 } finally {
2861 Binder.restoreCallingIdentity(ident);
2862 }
2863 }
2864 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002865
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002866 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2867 // Make sure this is a valid input method.
2868 InputMethodInfo imm = mMethodMap.get(id);
2869 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002870 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002871 }
2872
satokd87c2592010-09-29 11:52:06 +09002873 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2874 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002875
satokd87c2592010-09-29 11:52:06 +09002876 if (enabled) {
2877 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2878 if (pair.first.equals(id)) {
2879 // We are enabling this input method, but it is already enabled.
2880 // Nothing to do. The previous state was enabled.
2881 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002882 }
2883 }
satokd87c2592010-09-29 11:52:06 +09002884 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2885 // Previous state was disabled.
2886 return false;
2887 } else {
2888 StringBuilder builder = new StringBuilder();
2889 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2890 builder, enabledInputMethodsList, id)) {
2891 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002892 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002893 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2894 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2895 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002896 }
2897 // Previous state was enabled.
2898 return true;
2899 } else {
2900 // We are disabling the input method but it is already disabled.
2901 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002902 return false;
2903 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002904 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002905 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002906
satok723a27e2010-11-11 14:58:11 +09002907 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2908 boolean setSubtypeOnly) {
2909 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002910 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002911
2912 // Set Subtype here
2913 if (imi == null || subtypeId < 0) {
2914 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002915 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002916 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002917 if (subtypeId < imi.getSubtypeCount()) {
2918 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2919 mSettings.putSelectedSubtype(subtype.hashCode());
2920 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002921 } else {
2922 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002923 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002924 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002925 }
satokab751aa2010-09-14 19:17:36 +09002926 }
satok723a27e2010-11-11 14:58:11 +09002927
satok4c0e7152012-06-20 20:08:44 +09002928 // Workaround.
2929 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2930 // IMEs are not recognized and considered uninstalled.
2931 // Actually, we can't move everything after SystemReady because
2932 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2933 // the default IME here and try cheking the default IME again in systemReady().
2934 // TODO: Do nothing before system ready and implement a separated logic for
2935 // the encryption lock screen.
2936 // TODO: ASEC should be ready before IMMS is instantiated.
2937 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002938 // Set InputMethod here
2939 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2940 }
2941 }
2942
2943 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2944 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2945 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2946 // newDefaultIme is empty when there is no candidate for the selected IME.
2947 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2948 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2949 if (subtypeHashCode != null) {
2950 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002951 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002952 imi, Integer.valueOf(subtypeHashCode));
2953 } catch (NumberFormatException e) {
2954 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2955 }
2956 }
2957 }
2958 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002959 }
2960
satok4e4569d2010-11-19 18:45:53 +09002961 // If there are no selected shortcuts, tries finding the most applicable ones.
2962 private Pair<InputMethodInfo, InputMethodSubtype>
2963 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2964 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2965 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002966 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002967 boolean foundInSystemIME = false;
2968
2969 // Search applicable subtype for each InputMethodInfo
2970 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09002971 final String imiId = imi.getId();
2972 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2973 continue;
2974 }
satokcd7cd292010-11-20 15:46:23 +09002975 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002976 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002977 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002978 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002979 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002980 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002981 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002982 }
satokdf31ae62011-01-15 06:19:44 +09002983 // 2. Search by the system locale from enabledSubtypes.
2984 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002985 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002986 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002987 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002988 }
satoka86f5e42011-09-02 17:12:42 +09002989 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002990 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002991 final ArrayList<InputMethodSubtype> subtypesForSearch =
2992 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002993 ? InputMethodUtils.getSubtypes(imi)
2994 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09002995 // 4. Search by the current subtype's locale from all subtypes.
2996 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002997 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002998 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09002999 }
3000 // 5. Search by the system locale from all subtypes.
3001 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003002 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003003 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003004 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003005 }
satokcd7cd292010-11-20 15:46:23 +09003006 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003007 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003008 // The current input method is the most applicable IME.
3009 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003010 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003011 break;
satok7599a7f2010-12-22 13:45:23 +09003012 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003013 // The system input method is 2nd applicable IME.
3014 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003015 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003016 if ((imi.getServiceInfo().applicationInfo.flags
3017 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3018 foundInSystemIME = true;
3019 }
satok4e4569d2010-11-19 18:45:53 +09003020 }
3021 }
3022 }
3023 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003024 if (mostApplicableIMI != null) {
3025 Slog.w(TAG, "Most applicable shortcut input method was:"
3026 + mostApplicableIMI.getId());
3027 if (mostApplicableSubtype != null) {
3028 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3029 + "," + mostApplicableSubtype.getMode() + ","
3030 + mostApplicableSubtype.getLocale());
3031 }
3032 }
satok4e4569d2010-11-19 18:45:53 +09003033 }
satokcd7cd292010-11-20 15:46:23 +09003034 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003035 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003036 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003037 } else {
3038 return null;
3039 }
3040 }
3041
satokab751aa2010-09-14 19:17:36 +09003042 /**
3043 * @return Return the current subtype of this input method.
3044 */
satok42c5a162011-05-26 16:46:14 +09003045 @Override
satokab751aa2010-09-14 19:17:36 +09003046 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003047 // TODO: Make this work even for non-current users?
3048 if (!calledFromValidUser()) {
3049 return null;
3050 }
3051 synchronized (mMethodMap) {
3052 return getCurrentInputMethodSubtypeLocked();
3053 }
3054 }
3055
3056 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003057 if (mCurMethodId == null) {
3058 return null;
3059 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003060 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003061 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3062 if (imi == null || imi.getSubtypeCount() == 0) {
3063 return null;
satok4e4569d2010-11-19 18:45:53 +09003064 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003065 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003066 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3067 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003068 if (subtypeId == NOT_A_SUBTYPE_ID) {
3069 // If there are no selected subtypes, the framework will try to find
3070 // the most applicable subtype from explicitly or implicitly enabled
3071 // subtypes.
3072 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003073 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003074 // If there is only one explicitly or implicitly enabled subtype,
3075 // just returns it.
3076 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3077 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3078 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003079 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003080 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003081 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003082 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003083 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003084 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3085 true);
satok4e4569d2010-11-19 18:45:53 +09003086 }
satok3ef8b292010-11-23 06:06:29 +09003087 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003088 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003089 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003090 }
3091 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003092 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003093 }
3094
satokf3db1af2010-11-23 13:34:33 +09003095 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3096 InputMethodSubtype subtype) {
3097 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3098 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3099 } else {
3100 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3101 subtypes.add(subtype);
3102 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3103 }
3104 }
3105
satok4e4569d2010-11-19 18:45:53 +09003106 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003107 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003108 @Override
satok4e4569d2010-11-19 18:45:53 +09003109 public List getShortcutInputMethodsAndSubtypes() {
3110 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003111 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003112 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003113 // If there are no selected shortcut subtypes, the framework will try to find
3114 // the most applicable subtype from all subtypes whose mode is
3115 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003116 Pair<InputMethodInfo, InputMethodSubtype> info =
3117 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003118 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003119 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003120 ret.add(info.first);
3121 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003122 }
satok3da92232011-01-11 22:46:30 +09003123 return ret;
satokf3db1af2010-11-23 13:34:33 +09003124 }
satokf3db1af2010-11-23 13:34:33 +09003125 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3126 ret.add(imi);
3127 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3128 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003129 }
3130 }
satokf3db1af2010-11-23 13:34:33 +09003131 return ret;
satok4e4569d2010-11-19 18:45:53 +09003132 }
3133 }
3134
satok42c5a162011-05-26 16:46:14 +09003135 @Override
satokb66d2872010-11-10 01:04:04 +09003136 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003137 // TODO: Make this work even for non-current users?
3138 if (!calledFromValidUser()) {
3139 return false;
3140 }
satokb66d2872010-11-10 01:04:04 +09003141 synchronized (mMethodMap) {
3142 if (subtype != null && mCurMethodId != null) {
3143 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003144 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003145 if (subtypeId != NOT_A_SUBTYPE_ID) {
3146 setInputMethodLocked(mCurMethodId, subtypeId);
3147 return true;
3148 }
3149 }
3150 return false;
3151 }
3152 }
3153
satok688bd472012-02-09 20:09:17 +09003154 private static class InputMethodAndSubtypeListManager {
3155 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003156 // Used to load label
satok688bd472012-02-09 20:09:17 +09003157 private final PackageManager mPm;
3158 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003159 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003160 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3161 mContext = context;
3162 mPm = context.getPackageManager();
3163 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003164 final Locale locale = context.getResources().getConfiguration().locale;
3165 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003166 }
3167
3168 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3169 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3170 new Comparator<InputMethodInfo>() {
3171 @Override
3172 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3173 if (imi2 == null) return 0;
3174 if (imi1 == null) return 1;
3175 if (mPm == null) {
3176 return imi1.getId().compareTo(imi2.getId());
3177 }
3178 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3179 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3180 return imiId1.toString().compareTo(imiId2.toString());
3181 }
3182 });
3183
3184 public ImeSubtypeListItem getNextInputMethod(
3185 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3186 if (imi == null) {
3187 return null;
3188 }
3189 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3190 if (imList.size() <= 1) {
3191 return null;
3192 }
3193 final int N = imList.size();
3194 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003195 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003196 : NOT_A_SUBTYPE_ID;
3197 for (int i = 0; i < N; ++i) {
3198 final ImeSubtypeListItem isli = imList.get(i);
3199 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3200 if (!onlyCurrentIme) {
3201 return imList.get((i + 1) % N);
3202 }
3203 for (int j = 0; j < N - 1; ++j) {
3204 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3205 if (candidate.mImi.equals(imi)) {
3206 return candidate;
3207 }
3208 }
3209 return null;
3210 }
3211 }
3212 return null;
3213 }
3214
3215 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3216 return getSortedInputMethodAndSubtypeList(true, false, false);
3217 }
3218
3219 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3220 boolean inputShown, boolean isScreenLocked) {
3221 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3222 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3223 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3224 if (immis == null || immis.size() == 0) {
3225 return Collections.emptyList();
3226 }
3227 mSortedImmis.clear();
3228 mSortedImmis.putAll(immis);
3229 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3230 if (imi == null) continue;
3231 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3232 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3233 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3234 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3235 }
satok688bd472012-02-09 20:09:17 +09003236 final CharSequence imeLabel = imi.loadLabel(mPm);
3237 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3238 final int subtypeCount = imi.getSubtypeCount();
3239 if (DEBUG) {
3240 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3241 }
3242 for (int j = 0; j < subtypeCount; ++j) {
3243 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3244 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3245 // We show all enabled IMEs and subtypes when an IME is shown.
3246 if (enabledSubtypeSet.contains(subtypeHashCode)
3247 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3248 final CharSequence subtypeLabel =
3249 subtype.overridesImplicitlyEnabledSubtype() ? null
3250 : subtype.getDisplayName(mContext, imi.getPackageName(),
3251 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003252 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3253 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003254
3255 // Removing this subtype from enabledSubtypeSet because we no longer
3256 // need to add an entry of this subtype to imList to avoid duplicated
3257 // entries.
3258 enabledSubtypeSet.remove(subtypeHashCode);
3259 }
3260 }
3261 } else {
satok93d744d2012-05-09 17:14:08 +09003262 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3263 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003264 }
3265 }
satok93d744d2012-05-09 17:14:08 +09003266 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003267 return imList;
3268 }
3269 }
3270
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003271 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003272 private static class InputMethodFileManager {
3273 private static final String SYSTEM_PATH = "system";
3274 private static final String INPUT_METHOD_PATH = "inputmethod";
3275 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3276 private static final String NODE_SUBTYPES = "subtypes";
3277 private static final String NODE_SUBTYPE = "subtype";
3278 private static final String NODE_IMI = "imi";
3279 private static final String ATTR_ID = "id";
3280 private static final String ATTR_LABEL = "label";
3281 private static final String ATTR_ICON = "icon";
3282 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3283 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3284 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3285 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3286 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3287 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003288 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003289 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003290 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003291 if (methodMap == null) {
3292 throw new NullPointerException("methodMap is null");
3293 }
3294 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003295 final File systemDir = userId == UserHandle.USER_OWNER
3296 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3297 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003298 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3299 if (!inputMethodDir.mkdirs()) {
3300 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3301 }
3302 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3303 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3304 if (!subtypeFile.exists()) {
3305 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003306 writeAdditionalInputMethodSubtypes(
3307 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003308 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003309 readAdditionalInputMethodSubtypes(
3310 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003311 }
3312 }
3313
3314 private void deleteAllInputMethodSubtypes(String imiId) {
3315 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003316 mAdditionalSubtypesMap.remove(imiId);
3317 writeAdditionalInputMethodSubtypes(
3318 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003319 }
3320 }
3321
3322 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003323 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003324 synchronized (mMethodMap) {
3325 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3326 final int N = additionalSubtypes.length;
3327 for (int i = 0; i < N; ++i) {
3328 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003329 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003330 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003331 } else {
3332 Slog.w(TAG, "Duplicated subtype definition found: "
3333 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003334 }
3335 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003336 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3337 writeAdditionalInputMethodSubtypes(
3338 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003339 }
3340 }
3341
3342 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3343 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003344 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003345 }
3346 }
3347
3348 private static void writeAdditionalInputMethodSubtypes(
3349 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3350 HashMap<String, InputMethodInfo> methodMap) {
3351 // Safety net for the case that this function is called before methodMap is set.
3352 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3353 FileOutputStream fos = null;
3354 try {
3355 fos = subtypesFile.startWrite();
3356 final XmlSerializer out = new FastXmlSerializer();
3357 out.setOutput(fos, "utf-8");
3358 out.startDocument(null, true);
3359 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3360 out.startTag(null, NODE_SUBTYPES);
3361 for (String imiId : allSubtypes.keySet()) {
3362 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3363 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3364 continue;
3365 }
3366 out.startTag(null, NODE_IMI);
3367 out.attribute(null, ATTR_ID, imiId);
3368 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3369 final int N = subtypesList.size();
3370 for (int i = 0; i < N; ++i) {
3371 final InputMethodSubtype subtype = subtypesList.get(i);
3372 out.startTag(null, NODE_SUBTYPE);
3373 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3374 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3375 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3376 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3377 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3378 out.attribute(null, ATTR_IS_AUXILIARY,
3379 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3380 out.endTag(null, NODE_SUBTYPE);
3381 }
3382 out.endTag(null, NODE_IMI);
3383 }
3384 out.endTag(null, NODE_SUBTYPES);
3385 out.endDocument();
3386 subtypesFile.finishWrite(fos);
3387 } catch (java.io.IOException e) {
3388 Slog.w(TAG, "Error writing subtypes", e);
3389 if (fos != null) {
3390 subtypesFile.failWrite(fos);
3391 }
3392 }
3393 }
3394
3395 private static void readAdditionalInputMethodSubtypes(
3396 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3397 if (allSubtypes == null || subtypesFile == null) return;
3398 allSubtypes.clear();
3399 FileInputStream fis = null;
3400 try {
3401 fis = subtypesFile.openRead();
3402 final XmlPullParser parser = Xml.newPullParser();
3403 parser.setInput(fis, null);
3404 int type = parser.getEventType();
3405 // Skip parsing until START_TAG
3406 while ((type = parser.next()) != XmlPullParser.START_TAG
3407 && type != XmlPullParser.END_DOCUMENT) {}
3408 String firstNodeName = parser.getName();
3409 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3410 throw new XmlPullParserException("Xml doesn't start with subtypes");
3411 }
3412 final int depth =parser.getDepth();
3413 String currentImiId = null;
3414 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3415 while (((type = parser.next()) != XmlPullParser.END_TAG
3416 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3417 if (type != XmlPullParser.START_TAG)
3418 continue;
3419 final String nodeName = parser.getName();
3420 if (NODE_IMI.equals(nodeName)) {
3421 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3422 if (TextUtils.isEmpty(currentImiId)) {
3423 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3424 continue;
3425 }
3426 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3427 allSubtypes.put(currentImiId, tempSubtypesArray);
3428 } else if (NODE_SUBTYPE.equals(nodeName)) {
3429 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3430 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3431 continue;
3432 }
3433 final int icon = Integer.valueOf(
3434 parser.getAttributeValue(null, ATTR_ICON));
3435 final int label = Integer.valueOf(
3436 parser.getAttributeValue(null, ATTR_LABEL));
3437 final String imeSubtypeLocale =
3438 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3439 final String imeSubtypeMode =
3440 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3441 final String imeSubtypeExtraValue =
3442 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003443 final boolean isAuxiliary = "1".equals(String.valueOf(
3444 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003445 final InputMethodSubtype subtype =
3446 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3447 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3448 tempSubtypesArray.add(subtype);
3449 }
3450 }
3451 } catch (XmlPullParserException e) {
3452 Slog.w(TAG, "Error reading subtypes: " + e);
3453 return;
3454 } catch (java.io.IOException e) {
3455 Slog.w(TAG, "Error reading subtypes: " + e);
3456 return;
3457 } catch (NumberFormatException e) {
3458 Slog.w(TAG, "Error reading subtypes: " + e);
3459 return;
3460 } finally {
3461 if (fis != null) {
3462 try {
3463 fis.close();
3464 } catch (java.io.IOException e1) {
3465 Slog.w(TAG, "Failed to close.");
3466 }
3467 }
3468 }
3469 }
3470 }
3471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 @Override
3473 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3474 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3475 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3478 + Binder.getCallingPid()
3479 + ", uid=" + Binder.getCallingUid());
3480 return;
3481 }
3482
3483 IInputMethod method;
3484 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 synchronized (mMethodMap) {
3489 p.println("Current Input Method Manager state:");
3490 int N = mMethodList.size();
3491 p.println(" Input Methods:");
3492 for (int i=0; i<N; i++) {
3493 InputMethodInfo info = mMethodList.get(i);
3494 p.println(" InputMethod #" + i + ":");
3495 info.dump(p, " ");
3496 }
3497 p.println(" Clients:");
3498 for (ClientState ci : mClients.values()) {
3499 p.println(" Client " + ci + ":");
3500 p.println(" client=" + ci.client);
3501 p.println(" inputContext=" + ci.inputContext);
3502 p.println(" sessionRequested=" + ci.sessionRequested);
3503 p.println(" curSession=" + ci.curSession);
3504 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003505 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003507 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3508 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3510 + " mBoundToMethod=" + mBoundToMethod);
3511 p.println(" mCurToken=" + mCurToken);
3512 p.println(" mCurIntent=" + mCurIntent);
3513 method = mCurMethod;
3514 p.println(" mCurMethod=" + mCurMethod);
3515 p.println(" mEnabledSession=" + mEnabledSession);
3516 p.println(" mShowRequested=" + mShowRequested
3517 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3518 + " mShowForced=" + mShowForced
3519 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003520 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003522
Jeff Brownb88102f2010-09-08 11:49:43 -07003523 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 pw.flush();
3526 try {
3527 client.client.asBinder().dump(fd, args);
3528 } catch (RemoteException e) {
3529 p.println("Input method client dead: " + e);
3530 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003531 } else {
3532 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003534
Jeff Brownb88102f2010-09-08 11:49:43 -07003535 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 pw.flush();
3538 try {
3539 method.asBinder().dump(fd, args);
3540 } catch (RemoteException e) {
3541 p.println("Input method service dead: " + e);
3542 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003543 } else {
3544 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 }
3546 }
3547}