blob: 3b541ec3963b7f829dff89ddee672d45b42cfc75 [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 Kataoka8e303cc52013-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 Kataoka8e303cc52013-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 Kataoka8e303cc52013-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) {
708 checkCurrentLocaleChangedLocked();
709 }
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 Kataoka8e303cc52013-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 Kataoka8e303cc52013-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 Kataoka8e303cc52013-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
784 private void checkCurrentLocaleChangedLocked() {
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();
794 final boolean needsToResetDefaultIme = TextUtils.isEmpty(defaultImiId);
795 if (DEBUG) {
796 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
797 }
798 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
799 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900800 }
801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 @Override
803 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
804 throws RemoteException {
805 try {
806 return super.onTransact(code, data, reply, flags);
807 } catch (RuntimeException e) {
808 // The input method manager only throws security exceptions, so let's
809 // log all others.
810 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
813 throw e;
814 }
815 }
816
Dianne Hackborn661cd522011-08-22 00:26:20 -0700817 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700818 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900819 if (DEBUG) {
820 Slog.d(TAG, "--- systemReady");
821 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700822 if (!mSystemReady) {
823 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900824 mKeyguardManager =
825 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700826 mNotificationManager = (NotificationManager)
827 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
828 mStatusBar = statusBar;
829 statusBar.setIconVisibility("ime", false);
830 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900831 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
832 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900833 if (mShowOngoingImeSwitcherForPhones) {
834 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
835 mHardKeyboardListener);
836 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900837 buildInputMethodListLocked(mMethodList, mMethodMap,
838 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900839 if (!mImeSelectedOnBoot) {
840 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
841 checkCurrentLocaleChangedLocked();
842 }
843 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700844 try {
845 startInputInnerLocked();
846 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800847 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700848 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700849 }
850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800852
satok15452a42011-10-28 17:58:28 +0900853 private void setImeWindowVisibilityStatusHiddenLocked() {
854 mImeWindowVis = 0;
855 updateImeWindowStatusLocked();
856 }
857
satok3afd6c02011-11-18 08:38:19 +0900858 private void refreshImeWindowVisibilityLocked() {
859 final Configuration conf = mRes.getConfiguration();
860 final boolean haveHardKeyboard = conf.keyboard
861 != Configuration.KEYBOARD_NOKEYS;
862 final boolean hardKeyShown = haveHardKeyboard
863 && conf.hardKeyboardHidden
864 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500865 final boolean isScreenLocked =
866 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
867 final boolean isScreenSecurelyLocked =
868 isScreenLocked && mKeyguardManager.isKeyguardSecure();
869 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
870 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900871 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
872 updateImeWindowStatusLocked();
873 }
874
satok15452a42011-10-28 17:58:28 +0900875 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900876 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700877 }
878
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900879 // ---------------------------------------------------------------------------------------
880 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
881 // 1) it comes from the system process
882 // 2) the calling process' user id is identical to the current user id IMMS thinks.
883 private boolean calledFromValidUser() {
884 final int uid = Binder.getCallingUid();
885 final int userId = UserHandle.getUserId(uid);
886 if (DEBUG) {
887 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
888 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
889 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900890 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900891 }
892 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
893 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900894 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900895
896 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
897 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
898 // must not manage background users' states in any functions.
899 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
900 // by a token.
901 if (mContext.checkCallingOrSelfPermission(
902 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
903 == PackageManager.PERMISSION_GRANTED) {
904 if (DEBUG) {
905 Slog.d(TAG, "--- Access granted because the calling process has "
906 + "the INTERACT_ACROSS_USERS_FULL permission");
907 }
908 return true;
909 }
910 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
911 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900912 }
913
914 private boolean bindCurrentInputMethodService(
915 Intent service, ServiceConnection conn, int flags) {
916 if (service == null || conn == null) {
917 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
918 return false;
919 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800920 return mContext.bindServiceAsUser(service, conn, flags,
921 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900922 }
923
satoke7c6998e2011-06-03 17:57:59 +0900924 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900926 // TODO: Make this work even for non-current users?
927 if (!calledFromValidUser()) {
928 return Collections.emptyList();
929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 synchronized (mMethodMap) {
931 return new ArrayList<InputMethodInfo>(mMethodList);
932 }
933 }
934
satoke7c6998e2011-06-03 17:57:59 +0900935 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900937 // TODO: Make this work even for non-current users?
938 if (!calledFromValidUser()) {
939 return Collections.emptyList();
940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900942 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944 }
945
satokbb4aa062011-01-19 21:40:27 +0900946 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
947 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
948 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
949 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900950 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900951 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900952 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900953 }
954 return enabledInputMethodAndSubtypes;
955 }
956
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900957 /**
958 * @param imi if null, returns enabled subtypes for the current imi
959 * @return enabled subtypes of the specified imi
960 */
satoke7c6998e2011-06-03 17:57:59 +0900961 @Override
satok16331c82010-12-20 23:48:46 +0900962 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
963 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900964 // TODO: Make this work even for non-current users?
965 if (!calledFromValidUser()) {
966 return Collections.emptyList();
967 }
satok67ddf9c2010-11-17 09:45:54 +0900968 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900969 if (imi == null && mCurMethodId != null) {
970 imi = mMethodMap.get(mCurMethodId);
971 }
972 return mSettings.getEnabledInputMethodSubtypeListLocked(
973 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900974 }
975 }
976
satoke7c6998e2011-06-03 17:57:59 +0900977 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 public void addClient(IInputMethodClient client,
979 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900980 if (!calledFromValidUser()) {
981 return;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 synchronized (mMethodMap) {
984 mClients.put(client.asBinder(), new ClientState(client,
985 inputContext, uid, pid));
986 }
987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800988
satoke7c6998e2011-06-03 17:57:59 +0900989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900991 if (!calledFromValidUser()) {
992 return;
993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -0700995 ClientState cs = mClients.remove(client.asBinder());
996 if (cs != null) {
997 clearClientSessionLocked(cs);
998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 void executeOrSendMessage(IInterface target, Message msg) {
1003 if (target.asBinder() instanceof Binder) {
1004 mCaller.sendMessage(msg);
1005 } else {
1006 handleMessage(msg);
1007 msg.recycle();
1008 }
1009 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001010
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001011 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001013 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 + mCurClient.client.asBinder());
1015 if (mBoundToMethod) {
1016 mBoundToMethod = false;
1017 if (mCurMethod != null) {
1018 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1019 MSG_UNBIND_INPUT, mCurMethod));
1020 }
1021 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001022
1023 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1024 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1026 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1027 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001029
The Android Open Source Project10592532009-03-18 17:39:46 -07001030 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 private int getImeShowFlags() {
1035 int flags = 0;
1036 if (mShowForced) {
1037 flags |= InputMethod.SHOW_FORCED
1038 | InputMethod.SHOW_EXPLICIT;
1039 } else if (mShowExplicitlyRequested) {
1040 flags |= InputMethod.SHOW_EXPLICIT;
1041 }
1042 return flags;
1043 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 private int getAppShowFlags() {
1046 int flags = 0;
1047 if (mShowForced) {
1048 flags |= InputMethodManager.SHOW_FORCED;
1049 } else if (!mShowExplicitlyRequested) {
1050 flags |= InputMethodManager.SHOW_IMPLICIT;
1051 }
1052 return flags;
1053 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001054
Dianne Hackborn7663d802012-02-24 13:08:49 -08001055 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 if (!mBoundToMethod) {
1057 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1058 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1059 mBoundToMethod = true;
1060 }
1061 final SessionState session = mCurClient.curSession;
1062 if (initial) {
1063 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1064 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1065 } else {
1066 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1067 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1068 }
1069 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001070 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001071 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001073 return new InputBindResult(session.session, session.channel, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001077 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 // If no method is currently selected, do nothing.
1079 if (mCurMethodId == null) {
1080 return mNoBinding;
1081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 ClientState cs = mClients.get(client.asBinder());
1084 if (cs == null) {
1085 throw new IllegalArgumentException("unknown client "
1086 + client.asBinder());
1087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 try {
1090 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1091 // Check with the window manager to make sure this client actually
1092 // has a window with focus. If not, reject. This is thread safe
1093 // because if the focus changes some time before or after, the
1094 // next client receiving focus that has any interest in input will
1095 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001096 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1098 return null;
1099 }
1100 } catch (RemoteException e) {
1101 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001102
Dianne Hackborn7663d802012-02-24 13:08:49 -08001103 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1104 }
1105
1106 InputBindResult startInputUncheckedLocked(ClientState cs,
1107 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1108 // If no method is currently selected, do nothing.
1109 if (mCurMethodId == null) {
1110 return mNoBinding;
1111 }
1112
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001113 if (mCurClient == null) {
1114 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1115 if (DEBUG) {
1116 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1117 }
1118 }
1119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 if (mCurClient != cs) {
1121 // If the client is changing, we need to switch over to the new
1122 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001123 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001124 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 + cs.client.asBinder());
1126
1127 // If the screen is on, inform the new client it is active
1128 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001129 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1130 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 // Bump up the sequence for this client and attach it.
1135 mCurSeq++;
1136 if (mCurSeq <= 0) mCurSeq = 1;
1137 mCurClient = cs;
1138 mCurInputContext = inputContext;
1139 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // Check if the input method is changing.
1142 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1143 if (cs.curSession != null) {
1144 // Fast case: if we are already connected to the input method,
1145 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001146 return attachNewInputLocked(
1147 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 if (mHaveConnection) {
1150 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 // Return to client, and we will get back with it when
1152 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001153 requestClientSessionLocked(cs);
1154 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 } else if (SystemClock.uptimeMillis()
1156 < (mLastBindTime+TIME_TO_RECONNECT)) {
1157 // In this case we have connected to the service, but
1158 // don't yet have its interface. If it hasn't been too
1159 // long since we did the connection, we'll return to
1160 // the client and wait to get the service interface so
1161 // we can report back. If it has been too long, we want
1162 // to fall through so we can try a disconnect/reconnect
1163 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001164 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1167 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169 }
1170 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001172 return startInputInnerLocked();
1173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001174
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001175 InputBindResult startInputInnerLocked() {
1176 if (mCurMethodId == null) {
1177 return mNoBinding;
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001180 if (!mSystemReady) {
1181 // If the system is not yet ready, we shouldn't be running third
1182 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001183 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1187 if (info == null) {
1188 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001191 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1194 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001195 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1196 com.android.internal.R.string.input_method_binding_label);
1197 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1198 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001199 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001200 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 mLastBindTime = SystemClock.uptimeMillis();
1202 mHaveConnection = true;
1203 mCurId = info.getId();
1204 mCurToken = new Binder();
1205 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001206 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mIWindowManager.addWindowToken(mCurToken,
1208 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1209 } catch (RemoteException e) {
1210 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001211 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } else {
1213 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001214 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 + mCurIntent);
1216 }
1217 return null;
1218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001219
satoke7c6998e2011-06-03 17:57:59 +09001220 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001222 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001223 if (!calledFromValidUser()) {
1224 return null;
1225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 synchronized (mMethodMap) {
1227 final long ident = Binder.clearCallingIdentity();
1228 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001229 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 } finally {
1231 Binder.restoreCallingIdentity(ident);
1232 }
1233 }
1234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001235
satoke7c6998e2011-06-03 17:57:59 +09001236 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 public void finishInput(IInputMethodClient client) {
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
satoke7c6998e2011-06-03 17:57:59 +09001240 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 public void onServiceConnected(ComponentName name, IBinder service) {
1242 synchronized (mMethodMap) {
1243 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1244 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001245 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001246 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001247 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001248 return;
1249 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001250 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001251 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1252 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001254 clearClientSessionLocked(mCurClient);
1255 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 }
1257 }
1258 }
1259 }
1260
Jeff Brownc28867a2013-03-26 15:42:39 -07001261 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1262 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 synchronized (mMethodMap) {
1264 if (mCurMethod != null && method != null
1265 && mCurMethod.asBinder() == method.asBinder()) {
1266 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001267 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001269 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001270 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 if (res.method != null) {
1272 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1273 MSG_BIND_METHOD, mCurClient.client, res));
1274 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277 }
1278 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001279
1280 // Session abandoned. Close its associated input channel.
1281 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001284 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001285 if (mVisibleBound) {
1286 mContext.unbindService(mVisibleConnection);
1287 mVisibleBound = false;
1288 }
1289
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001290 if (mHaveConnection) {
1291 mContext.unbindService(this);
1292 mHaveConnection = false;
1293 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001294
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001295 if (mCurToken != null) {
1296 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001297 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001298 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001299 // The current IME is shown. Hence an IME switch (transition) is happening.
1300 mWindowManagerService.saveLastInputMethodWindowForTransition();
1301 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001302 mIWindowManager.removeWindowToken(mCurToken);
1303 } catch (RemoteException e) {
1304 }
1305 mCurToken = null;
1306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
The Android Open Source Project10592532009-03-18 17:39:46 -07001308 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001309 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001310
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001311 if (reportToClient && mCurClient != null) {
1312 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1313 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1314 }
1315 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001316
1317 void requestClientSessionLocked(ClientState cs) {
1318 if (!cs.sessionRequested) {
1319 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1320 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1321 cs.sessionRequested = true;
1322 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1323 MSG_CREATE_SESSION, mCurMethod, channels[1],
1324 new MethodCallback(this, mCurMethod, channels[0])));
1325 }
1326 }
1327
1328 void clearClientSessionLocked(ClientState cs) {
1329 finishSessionLocked(cs.curSession);
1330 cs.curSession = null;
1331 cs.sessionRequested = false;
1332 }
1333
1334 private void finishSessionLocked(SessionState sessionState) {
1335 if (sessionState != null) {
1336 if (sessionState.session != null) {
1337 try {
1338 sessionState.session.finishSession();
1339 } catch (RemoteException e) {
1340 Slog.w(TAG, "Session failed to close due to remote exception", e);
1341 setImeWindowVisibilityStatusHiddenLocked();
1342 }
1343 sessionState.session = null;
1344 }
1345 if (sessionState.channel != null) {
1346 sessionState.channel.dispose();
1347 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001348 }
1349 }
1350 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001351
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001352 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 if (mCurMethod != null) {
1354 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001355 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001357
Jeff Brownc28867a2013-03-26 15:42:39 -07001358 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001359 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 mCurMethod = null;
1361 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001362 if (mStatusBar != null) {
1363 mStatusBar.setIconVisibility("ime", false);
1364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366
satoke7c6998e2011-06-03 17:57:59 +09001367 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 public void onServiceDisconnected(ComponentName name) {
1369 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001370 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 + " mCurIntent=" + mCurIntent);
1372 if (mCurMethod != null && mCurIntent != null
1373 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001374 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 // We consider this to be a new bind attempt, since the system
1376 // should now try to restart the service for us.
1377 mLastBindTime = SystemClock.uptimeMillis();
1378 mShowRequested = mInputShown;
1379 mInputShown = false;
1380 if (mCurClient != null) {
1381 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1382 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1383 }
1384 }
1385 }
1386 }
1387
satokf9f01002011-05-19 21:31:50 +09001388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001390 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 long ident = Binder.clearCallingIdentity();
1392 try {
1393 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001394 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 return;
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 synchronized (mMethodMap) {
1399 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001400 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001401 if (mStatusBar != null) {
1402 mStatusBar.setIconVisibility("ime", false);
1403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001406 CharSequence contentDescription = null;
1407 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001408 // Use PackageManager to load label
1409 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001410 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001411 mIPackageManager.getApplicationInfo(packageName, 0,
1412 mSettings.getCurrentUserId()));
1413 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001414 /* ignore */
1415 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001416 if (mStatusBar != null) {
1417 mStatusBar.setIcon("ime", packageName, iconId, 0,
1418 contentDescription != null
1419 ? contentDescription.toString() : null);
1420 mStatusBar.setIconVisibility("ime", true);
1421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 }
1424 } finally {
1425 Binder.restoreCallingIdentity(ident);
1426 }
1427 }
1428
satok7cfc0ed2011-06-20 21:29:36 +09001429 private boolean needsToShowImeSwitchOngoingNotification() {
1430 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001431 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001432 synchronized (mMethodMap) {
1433 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1434 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001435 if (N > 2) return true;
1436 if (N < 1) return false;
1437 int nonAuxCount = 0;
1438 int auxCount = 0;
1439 InputMethodSubtype nonAuxSubtype = null;
1440 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001441 for(int i = 0; i < N; ++i) {
1442 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001443 final List<InputMethodSubtype> subtypes =
1444 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001445 final int subtypeCount = subtypes.size();
1446 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001447 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001448 } else {
1449 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001450 final InputMethodSubtype subtype = subtypes.get(j);
1451 if (!subtype.isAuxiliary()) {
1452 ++nonAuxCount;
1453 nonAuxSubtype = subtype;
1454 } else {
1455 ++auxCount;
1456 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001457 }
1458 }
1459 }
satok7cfc0ed2011-06-20 21:29:36 +09001460 }
satokb6359412011-06-28 17:47:41 +09001461 if (nonAuxCount > 1 || auxCount > 1) {
1462 return true;
1463 } else if (nonAuxCount == 1 && auxCount == 1) {
1464 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001465 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1466 || auxSubtype.overridesImplicitlyEnabledSubtype()
1467 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001468 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1469 return false;
1470 }
1471 return true;
1472 }
1473 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001474 }
satok7cfc0ed2011-06-20 21:29:36 +09001475 }
1476
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001477 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001478 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001479 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001480 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001481 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001482 try {
1483 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001484 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001485 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001486 return;
1487 }
1488
1489 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001490 mImeWindowVis = vis;
1491 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001492 if (mStatusBar != null) {
1493 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1494 }
satok7cfc0ed2011-06-20 21:29:36 +09001495 final boolean iconVisibility = (vis & InputMethodService.IME_ACTIVE) != 0;
satok5bc8e732011-07-22 21:07:23 +09001496 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1497 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001498 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001499 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001500 final CharSequence title = mRes.getText(
1501 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001502 final CharSequence imiLabel = imi.loadLabel(pm);
1503 final CharSequence summary = mCurrentSubtype != null
1504 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1505 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1506 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001507 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001508 : imiLabel;
1509
satok7cfc0ed2011-06-20 21:29:36 +09001510 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001511 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001512 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001513 if (DEBUG) {
1514 Slog.d(TAG, "--- show notification: label = " + imiLabel
1515 + ", summary = " + summary);
1516 }
1517 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001518 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001519 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001520 mNotificationShown = true;
1521 }
satok7cfc0ed2011-06-20 21:29:36 +09001522 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001523 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001524 if (DEBUG) {
1525 Slog.d(TAG, "--- hide notification");
1526 }
1527 mNotificationManager.cancelAsUser(null,
1528 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001529 mNotificationShown = false;
1530 }
1531 }
satok06487a52010-10-29 11:37:18 +09001532 }
1533 } finally {
1534 Binder.restoreCallingIdentity(ident);
1535 }
1536 }
1537
satoke7c6998e2011-06-03 17:57:59 +09001538 @Override
satokf9f01002011-05-19 21:31:50 +09001539 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001540 if (!calledFromValidUser()) {
1541 return;
1542 }
satokf9f01002011-05-19 21:31:50 +09001543 synchronized (mMethodMap) {
1544 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1545 for (int i = 0; i < spans.length; ++i) {
1546 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001547 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001548 mSecureSuggestionSpans.put(ss, currentImi);
1549 }
1550 }
1551 }
1552 }
1553
satoke7c6998e2011-06-03 17:57:59 +09001554 @Override
satokf9f01002011-05-19 21:31:50 +09001555 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001556 if (!calledFromValidUser()) {
1557 return false;
1558 }
satokf9f01002011-05-19 21:31:50 +09001559 synchronized (mMethodMap) {
1560 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1561 // TODO: Do not send the intent if the process of the targetImi is already dead.
1562 if (targetImi != null) {
1563 final String[] suggestions = span.getSuggestions();
1564 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001565 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001566 final Intent intent = new Intent();
1567 // Ensures that only a class in the original IME package will receive the
1568 // notification.
satok42c5a162011-05-26 16:46:14 +09001569 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001570 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1571 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1572 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1573 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001574 final long ident = Binder.clearCallingIdentity();
1575 try {
1576 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1577 } finally {
1578 Binder.restoreCallingIdentity(ident);
1579 }
satokf9f01002011-05-19 21:31:50 +09001580 return true;
1581 }
1582 }
1583 return false;
1584 }
1585
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001586 void updateFromSettingsLocked(boolean enabledMayChange) {
1587 if (enabledMayChange) {
1588 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1589 for (int i=0; i<enabled.size(); i++) {
1590 // We allow the user to select "disabled until used" apps, so if they
1591 // are enabling one of those here we now need to make it enabled.
1592 InputMethodInfo imm = enabled.get(i);
1593 try {
1594 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1595 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1596 mSettings.getCurrentUserId());
1597 if (ai.enabledSetting
1598 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1599 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1600 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001601 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1602 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001603 }
1604 } catch (RemoteException e) {
1605 }
1606 }
1607 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001608 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1609 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1610 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1611 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001612 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001613 // There is no input method selected, try to choose new applicable input method.
1614 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001615 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001616 }
1617 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001619 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001621 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001622 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001623 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
satokf3db1af2010-11-23 13:34:33 +09001625 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001626 } else {
1627 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001628 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001629 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001632
satokab751aa2010-09-14 19:17:36 +09001633 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 InputMethodInfo info = mMethodMap.get(id);
1635 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001636 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
satokd81e9502012-05-21 12:58:45 +09001639 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001641 final int subtypeCount = info.getSubtypeCount();
1642 if (subtypeCount <= 0) {
1643 return;
satokcd7cd292010-11-20 15:46:23 +09001644 }
satokd81e9502012-05-21 12:58:45 +09001645 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1646 final InputMethodSubtype newSubtype;
1647 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1648 newSubtype = info.getSubtypeAt(subtypeId);
1649 } else {
1650 // If subtype is null, try to find the most applicable one from
1651 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001652 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001653 }
1654 if (newSubtype == null || oldSubtype == null) {
1655 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1656 + ", new subtype = " + newSubtype);
1657 return;
1658 }
1659 if (newSubtype != oldSubtype) {
1660 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1661 if (mCurMethod != null) {
1662 try {
1663 refreshImeWindowVisibilityLocked();
1664 mCurMethod.changeInputMethodSubtype(newSubtype);
1665 } catch (RemoteException e) {
1666 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001667 }
1668 }
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 return;
1671 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001672
satokd81e9502012-05-21 12:58:45 +09001673 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 final long ident = Binder.clearCallingIdentity();
1675 try {
satokab751aa2010-09-14 19:17:36 +09001676 // Set a subtype to this input method.
1677 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001678 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1679 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1680 // because mCurMethodId is stored as a history in
1681 // setSelectedInputMethodAndSubtypeLocked().
1682 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683
1684 if (ActivityManagerNative.isSystemReady()) {
1685 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001686 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001688 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001690 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 } finally {
1692 Binder.restoreCallingIdentity(ident);
1693 }
1694 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001695
satok42c5a162011-05-26 16:46:14 +09001696 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001697 public boolean showSoftInput(IInputMethodClient client, int flags,
1698 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001699 if (!calledFromValidUser()) {
1700 return false;
1701 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001702 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 long ident = Binder.clearCallingIdentity();
1704 try {
1705 synchronized (mMethodMap) {
1706 if (mCurClient == null || client == null
1707 || mCurClient.client.asBinder() != client.asBinder()) {
1708 try {
1709 // We need to check if this is the current client with
1710 // focus in the window manager, to allow this call to
1711 // be made before input is started in it.
1712 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001713 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001714 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001717 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 }
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001721 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001722 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 } finally {
1725 Binder.restoreCallingIdentity(ident);
1726 }
1727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
The Android Open Source Project4df24232009-03-05 14:34:35 -08001729 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 mShowRequested = true;
1731 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1732 mShowExplicitlyRequested = true;
1733 }
1734 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1735 mShowExplicitlyRequested = true;
1736 mShowForced = true;
1737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001738
Dianne Hackborncc278702009-09-02 23:07:23 -07001739 if (!mSystemReady) {
1740 return false;
1741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001742
The Android Open Source Project4df24232009-03-05 14:34:35 -08001743 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001745 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001746 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1747 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1748 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001750 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001751 bindCurrentInputMethodService(
1752 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001753 mVisibleBound = true;
1754 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001755 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001757 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 // The client has asked to have the input method shown, but
1759 // we have been sitting here too long with a connection to the
1760 // service and no interface received, so let's disconnect/connect
1761 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001762 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001764 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001766 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001767 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001768 } else {
1769 if (DEBUG) {
1770 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1771 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001774
The Android Open Source Project4df24232009-03-05 14:34:35 -08001775 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001777
satok42c5a162011-05-26 16:46:14 +09001778 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001779 public boolean hideSoftInput(IInputMethodClient client, int flags,
1780 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001781 if (!calledFromValidUser()) {
1782 return false;
1783 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001784 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 long ident = Binder.clearCallingIdentity();
1786 try {
1787 synchronized (mMethodMap) {
1788 if (mCurClient == null || client == null
1789 || mCurClient.client.asBinder() != client.asBinder()) {
1790 try {
1791 // We need to check if this is the current client with
1792 // focus in the window manager, to allow this call to
1793 // be made before input is started in it.
1794 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001795 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1796 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001797 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001798 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
1800 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001801 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001802 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001805
Joe Onorato8a9b2202010-02-26 18:56:32 -08001806 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001807 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808 }
1809 } finally {
1810 Binder.restoreCallingIdentity(ident);
1811 }
1812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001813
The Android Open Source Project4df24232009-03-05 14:34:35 -08001814 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1816 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001817 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 -08001818 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
1820 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001821 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 -08001822 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001824 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001826 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1827 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1828 res = true;
1829 } else {
1830 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001832 if (mHaveConnection && mVisibleBound) {
1833 mContext.unbindService(mVisibleConnection);
1834 mVisibleBound = false;
1835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 mInputShown = false;
1837 mShowRequested = false;
1838 mShowExplicitlyRequested = false;
1839 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001840 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001842
satok42c5a162011-05-26 16:46:14 +09001843 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001844 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1845 int controlFlags, int softInputMode, int windowFlags,
1846 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001847 // Needs to check the validity before clearing calling identity
1848 final boolean calledFromValidUser = calledFromValidUser();
1849
Dianne Hackborn7663d802012-02-24 13:08:49 -08001850 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 long ident = Binder.clearCallingIdentity();
1852 try {
1853 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001854 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001855 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001857 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001858
Dianne Hackborn7663d802012-02-24 13:08:49 -08001859 ClientState cs = mClients.get(client.asBinder());
1860 if (cs == null) {
1861 throw new IllegalArgumentException("unknown client "
1862 + client.asBinder());
1863 }
1864
1865 try {
1866 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1867 // Check with the window manager to make sure this client actually
1868 // has a window with focus. If not, reject. This is thread safe
1869 // because if the focus changes some time before or after, the
1870 // next client receiving focus that has any interest in input will
1871 // be calling through here after that change happens.
1872 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1873 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1874 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001876 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001878
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001879 if (!calledFromValidUser) {
1880 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1881 Slog.w(TAG, "If you want to interect with IME, you need "
1882 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1883 hideCurrentInputLocked(0, null);
1884 return null;
1885 }
1886
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001887 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001888 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001889 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001890 if (attribute != null) {
1891 return startInputUncheckedLocked(cs, inputContext, attribute,
1892 controlFlags);
1893 }
1894 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001895 }
1896 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001897
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001898 // Should we auto-show the IME even if the caller has not
1899 // specified what should be done with it?
1900 // We only do this automatically if the window can resize
1901 // to accommodate the IME (so what the user sees will give
1902 // them good context without input information being obscured
1903 // by the IME) or if running on a large screen where there
1904 // is more room for the target window + IME.
1905 final boolean doAutoShow =
1906 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1907 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1908 || mRes.getConfiguration().isLayoutSizeAtLeast(
1909 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001910 final boolean isTextEditor =
1911 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1912
1913 // We want to start input before showing the IME, but after closing
1914 // it. We want to do this after closing it to help the IME disappear
1915 // more quickly (not get stuck behind it initializing itself for the
1916 // new focused input, even if its window wants to hide the IME).
1917 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1920 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001921 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1923 // There is no focus view, and this window will
1924 // be behind any soft input window, so hide the
1925 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001926 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001927 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001929 } else if (isTextEditor && doAutoShow && (softInputMode &
1930 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 // There is a focus view, and we are navigating forward
1932 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001933 // We only do this automatically if the window can resize
1934 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001935 // them good context without input information being obscured
1936 // by the IME) or if running on a large screen where there
1937 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001938 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001939 if (attribute != null) {
1940 res = startInputUncheckedLocked(cs, inputContext, attribute,
1941 controlFlags);
1942 didStart = true;
1943 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001944 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946 break;
1947 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1948 // Do nothing.
1949 break;
1950 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1951 if ((softInputMode &
1952 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001953 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001954 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956 break;
1957 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001958 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001959 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 break;
1961 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1962 if ((softInputMode &
1963 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001965 if (attribute != null) {
1966 res = startInputUncheckedLocked(cs, inputContext, attribute,
1967 controlFlags);
1968 didStart = true;
1969 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001970 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972 break;
1973 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001974 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001975 if (attribute != null) {
1976 res = startInputUncheckedLocked(cs, inputContext, attribute,
1977 controlFlags);
1978 didStart = true;
1979 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001980 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 break;
1982 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001983
1984 if (!didStart && attribute != null) {
1985 res = startInputUncheckedLocked(cs, inputContext, attribute,
1986 controlFlags);
1987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
1989 } finally {
1990 Binder.restoreCallingIdentity(ident);
1991 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001992
1993 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001995
satok42c5a162011-05-26 16:46:14 +09001996 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001998 if (!calledFromValidUser()) {
1999 return;
2000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 synchronized (mMethodMap) {
2002 if (mCurClient == null || client == null
2003 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002004 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002005 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
2007
satok440aab52010-11-25 09:43:11 +09002008 // Always call subtype picker, because subtype picker is a superset of input method
2009 // picker.
satokab751aa2010-09-14 19:17:36 +09002010 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2011 }
2012 }
2013
satok42c5a162011-05-26 16:46:14 +09002014 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002016 if (!calledFromValidUser()) {
2017 return;
2018 }
satok28203512010-11-24 11:06:49 +09002019 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2020 }
2021
satok42c5a162011-05-26 16:46:14 +09002022 @Override
satok28203512010-11-24 11:06:49 +09002023 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002024 if (!calledFromValidUser()) {
2025 return;
2026 }
satok28203512010-11-24 11:06:49 +09002027 synchronized (mMethodMap) {
2028 if (subtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002029 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002030 mMethodMap.get(id), subtype.hashCode()));
2031 } else {
2032 setInputMethod(token, id);
2033 }
2034 }
satokab751aa2010-09-14 19:17:36 +09002035 }
2036
satok42c5a162011-05-26 16:46:14 +09002037 @Override
satokb416a71e2010-11-25 20:42:14 +09002038 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002039 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002040 if (!calledFromValidUser()) {
2041 return;
2042 }
satokb416a71e2010-11-25 20:42:14 +09002043 synchronized (mMethodMap) {
2044 if (mCurClient == null || client == null
2045 || mCurClient.client.asBinder() != client.asBinder()) {
2046 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2047 }
satok7fee71f2010-12-17 18:54:26 +09002048 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2049 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09002050 }
2051 }
2052
satok4fc87d62011-05-20 16:13:43 +09002053 @Override
satok735cf382010-11-11 20:40:09 +09002054 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002055 if (!calledFromValidUser()) {
2056 return false;
2057 }
satok735cf382010-11-11 20:40:09 +09002058 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002059 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002060 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002061 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002062 lastImi = mMethodMap.get(lastIme.first);
2063 } else {
2064 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002065 }
satok4fc87d62011-05-20 16:13:43 +09002066 String targetLastImiId = null;
2067 int subtypeId = NOT_A_SUBTYPE_ID;
2068 if (lastIme != null && lastImi != null) {
2069 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2070 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2071 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2072 : mCurrentSubtype.hashCode();
2073 // If the last IME is the same as the current IME and the last subtype is not
2074 // defined, there is no need to switch to the last IME.
2075 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2076 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002077 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002078 }
2079 }
2080
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002081 if (TextUtils.isEmpty(targetLastImiId)
2082 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002083 // This is a safety net. If the currentSubtype can't be added to the history
2084 // and the framework couldn't find the last ime, we will make the last ime be
2085 // the most applicable enabled keyboard subtype of the system imes.
2086 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2087 if (enabled != null) {
2088 final int N = enabled.size();
2089 final String locale = mCurrentSubtype == null
2090 ? mRes.getConfiguration().locale.toString()
2091 : mCurrentSubtype.getLocale();
2092 for (int i = 0; i < N; ++i) {
2093 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002094 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002095 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002096 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2097 InputMethodUtils.getSubtypes(imi),
2098 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002099 if (keyboardSubtype != null) {
2100 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002101 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002102 imi, keyboardSubtype.hashCode());
2103 if(keyboardSubtype.getLocale().equals(locale)) {
2104 break;
2105 }
2106 }
2107 }
2108 }
2109 }
2110 }
2111
2112 if (!TextUtils.isEmpty(targetLastImiId)) {
2113 if (DEBUG) {
2114 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2115 + ", from: " + mCurMethodId + ", " + subtypeId);
2116 }
2117 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2118 return true;
2119 } else {
2120 return false;
2121 }
satok735cf382010-11-11 20:40:09 +09002122 }
2123 }
2124
satoke7c6998e2011-06-03 17:57:59 +09002125 @Override
satok688bd472012-02-09 20:09:17 +09002126 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002127 if (!calledFromValidUser()) {
2128 return false;
2129 }
satok688bd472012-02-09 20:09:17 +09002130 synchronized (mMethodMap) {
2131 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2132 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2133 if (nextSubtype == null) {
2134 return false;
2135 }
2136 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2137 return true;
2138 }
2139 }
2140
2141 @Override
satok68f1b782011-04-11 14:26:04 +09002142 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002143 if (!calledFromValidUser()) {
2144 return null;
2145 }
satok68f1b782011-04-11 14:26:04 +09002146 synchronized (mMethodMap) {
2147 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2148 // TODO: Handle the case of the last IME with no subtypes
2149 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2150 || TextUtils.isEmpty(lastIme.second)) return null;
2151 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2152 if (lastImi == null) return null;
2153 try {
2154 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002155 final int lastSubtypeId =
2156 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002157 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2158 return null;
2159 }
2160 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002161 } catch (NumberFormatException e) {
2162 return null;
2163 }
2164 }
2165 }
2166
satoke7c6998e2011-06-03 17:57:59 +09002167 @Override
satokee5e77c2011-09-02 18:50:15 +09002168 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002169 if (!calledFromValidUser()) {
2170 return;
2171 }
satok91e88122011-07-18 11:11:42 +09002172 // By this IPC call, only a process which shares the same uid with the IME can add
2173 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002174 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002175 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002176 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002177 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002178 final String[] packageInfos;
2179 try {
2180 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2181 } catch (RemoteException e) {
2182 Slog.e(TAG, "Failed to get package infos");
2183 return;
2184 }
satok91e88122011-07-18 11:11:42 +09002185 if (packageInfos != null) {
2186 final int packageNum = packageInfos.length;
2187 for (int i = 0; i < packageNum; ++i) {
2188 if (packageInfos[i].equals(imi.getPackageName())) {
2189 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002190 final long ident = Binder.clearCallingIdentity();
2191 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002192 buildInputMethodListLocked(mMethodList, mMethodMap,
2193 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002194 } finally {
2195 Binder.restoreCallingIdentity(ident);
2196 }
satokee5e77c2011-09-02 18:50:15 +09002197 return;
satok91e88122011-07-18 11:11:42 +09002198 }
2199 }
2200 }
satoke7c6998e2011-06-03 17:57:59 +09002201 }
satokee5e77c2011-09-02 18:50:15 +09002202 return;
satoke7c6998e2011-06-03 17:57:59 +09002203 }
2204
satok28203512010-11-24 11:06:49 +09002205 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 synchronized (mMethodMap) {
2207 if (token == null) {
2208 if (mContext.checkCallingOrSelfPermission(
2209 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2210 != PackageManager.PERMISSION_GRANTED) {
2211 throw new SecurityException(
2212 "Using null token requires permission "
2213 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2214 }
2215 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002216 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2217 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 return;
2219 }
2220
satokc5933802011-08-31 21:26:04 +09002221 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 try {
satokab751aa2010-09-14 19:17:36 +09002223 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 } finally {
2225 Binder.restoreCallingIdentity(ident);
2226 }
2227 }
2228 }
2229
satok42c5a162011-05-26 16:46:14 +09002230 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002231 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002232 if (!calledFromValidUser()) {
2233 return;
2234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 synchronized (mMethodMap) {
2236 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002237 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2238 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 return;
2240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 long ident = Binder.clearCallingIdentity();
2242 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002243 hideCurrentInputLocked(flags, null);
2244 } finally {
2245 Binder.restoreCallingIdentity(ident);
2246 }
2247 }
2248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002249
satok42c5a162011-05-26 16:46:14 +09002250 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002251 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002252 if (!calledFromValidUser()) {
2253 return;
2254 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002255 synchronized (mMethodMap) {
2256 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002257 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2258 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002259 return;
2260 }
2261 long ident = Binder.clearCallingIdentity();
2262 try {
2263 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 } finally {
2265 Binder.restoreCallingIdentity(ident);
2266 }
2267 }
2268 }
2269
2270 void setEnabledSessionInMainThread(SessionState session) {
2271 if (mEnabledSession != session) {
2272 if (mEnabledSession != null) {
2273 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002274 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 mEnabledSession.method.setSessionEnabled(
2276 mEnabledSession.session, false);
2277 } catch (RemoteException e) {
2278 }
2279 }
2280 mEnabledSession = session;
2281 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 session.method.setSessionEnabled(
2284 session.session, true);
2285 } catch (RemoteException e) {
2286 }
2287 }
2288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002289
satok42c5a162011-05-26 16:46:14 +09002290 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002292 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 switch (msg.what) {
2294 case MSG_SHOW_IM_PICKER:
2295 showInputMethodMenu();
2296 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002297
satokab751aa2010-09-14 19:17:36 +09002298 case MSG_SHOW_IM_SUBTYPE_PICKER:
2299 showInputMethodSubtypeMenu();
2300 return true;
2301
satok47a44912010-10-06 16:03:58 +09002302 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002303 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002304 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002305 args.recycle();
satok217f5482010-12-15 05:19:19 +09002306 return true;
2307
2308 case MSG_SHOW_IM_CONFIG:
2309 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002310 return true;
2311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 case MSG_UNBIND_INPUT:
2315 try {
2316 ((IInputMethod)msg.obj).unbindInput();
2317 } catch (RemoteException e) {
2318 // There is nothing interesting about the method dying.
2319 }
2320 return true;
2321 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002322 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 try {
2324 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2325 } catch (RemoteException e) {
2326 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002327 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 return true;
2329 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002330 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002332 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002333 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002334 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 } catch (RemoteException e) {
2336 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002337 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 return true;
2339 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002340 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002342 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002343 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002344 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 } catch (RemoteException e) {
2346 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002347 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 return true;
2349 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002350 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002352 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2354 } catch (RemoteException e) {
2355 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002356 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002358 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002359 args = (SomeArgs)msg.obj;
Jeff Brownc28867a2013-03-26 15:42:39 -07002360 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 try {
Jeff Brownc28867a2013-03-26 15:42:39 -07002362 ((IInputMethod)args.arg1).createSession(channel,
2363 (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002365 } finally {
2366 if (channel != null) {
2367 channel.dispose();
2368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002370 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002376 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 try {
2378 SessionState session = (SessionState)args.arg1;
2379 setEnabledSessionInMainThread(session);
2380 session.method.startInput((IInputContext)args.arg2,
2381 (EditorInfo)args.arg3);
2382 } catch (RemoteException e) {
2383 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002384 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 return true;
2386 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002387 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 try {
2389 SessionState session = (SessionState)args.arg1;
2390 setEnabledSessionInMainThread(session);
2391 session.method.restartInput((IInputContext)args.arg2,
2392 (EditorInfo)args.arg3);
2393 } catch (RemoteException e) {
2394 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002395 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 case MSG_UNBIND_METHOD:
2401 try {
2402 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2403 } catch (RemoteException e) {
2404 // There is nothing interesting about the last client dying.
2405 }
2406 return true;
2407 case MSG_BIND_METHOD:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002408 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 try {
2410 ((IInputMethodClient)args.arg1).onBindMethod(
2411 (InputBindResult)args.arg2);
2412 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002413 Slog.w(TAG, "Client died receiving input method " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002415 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 return true;
Dianne Hackborna6e41342012-05-22 16:30:34 -07002417 case MSG_SET_ACTIVE:
2418 try {
2419 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2420 } catch (RemoteException e) {
2421 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2422 + ((ClientState)msg.obj).pid + " uid "
2423 + ((ClientState)msg.obj).uid);
2424 }
2425 return true;
satok01038492012-04-09 21:08:27 +09002426
2427 // --------------------------------------------------------------
2428 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2429 mHardKeyboardListener.handleHardKeyboardStatusChange(
2430 msg.arg1 == 1, msg.arg2 == 1);
2431 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 }
2433 return false;
2434 }
2435
satokdc9ddae2011-10-06 12:22:36 +09002436 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002437 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2438 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002439 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002440 if (DEBUG) {
2441 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2442 }
satok723a27e2010-11-11 14:58:11 +09002443 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002444 return true;
2445 }
2446
2447 return false;
2448 }
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002451 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002452 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002453 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2454 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 list.clear();
2457 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002458
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002459 // Use for queryIntentServicesAsUser
2460 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002461 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002462 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002464 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002466 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2467 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002468
satoke7c6998e2011-06-03 17:57:59 +09002469 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2470 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 for (int i = 0; i < services.size(); ++i) {
2472 ResolveInfo ri = services.get(i);
2473 ServiceInfo si = ri.serviceInfo;
2474 ComponentName compName = new ComponentName(si.packageName, si.name);
2475 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2476 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002477 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 + ": it does not require the permission "
2479 + android.Manifest.permission.BIND_INPUT_METHOD);
2480 continue;
2481 }
2482
Joe Onorato8a9b2202010-02-26 18:56:32 -08002483 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484
2485 try {
satoke7c6998e2011-06-03 17:57:59 +09002486 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002488 final String id = p.getId();
2489 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490
2491 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002492 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002496 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002498 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
2500 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002501
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002502 if (resetDefaultEnabledIme) {
2503 final ArrayList<InputMethodInfo> defaultEnabledIme =
2504 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2505 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2506 final InputMethodInfo imi = defaultEnabledIme.get(i);
2507 if (DEBUG) {
2508 Slog.d(TAG, "--- enable ime = " + imi);
2509 }
2510 setInputMethodEnabledLocked(imi.getId(), true);
2511 }
2512 }
2513
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002514 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002515 if (!TextUtils.isEmpty(defaultImiId)) {
2516 if (!map.containsKey(defaultImiId)) {
2517 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2518 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002519 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002520 }
2521 } else {
2522 // Double check that the default IME is certainly enabled.
2523 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002524 }
2525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002529
satokab751aa2010-09-14 19:17:36 +09002530 private void showInputMethodMenu() {
2531 showInputMethodMenuInternal(false);
2532 }
2533
2534 private void showInputMethodSubtypeMenu() {
2535 showInputMethodMenuInternal(true);
2536 }
2537
satok217f5482010-12-15 05:19:19 +09002538 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002539 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002540 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002541 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2542 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002543 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002544 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002545 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002546 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002547 }
2548
2549 private void showConfigureInputMethods() {
2550 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2551 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2552 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2553 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002554 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002555 }
2556
satok2c93efc2012-04-02 19:33:47 +09002557 private boolean isScreenLocked() {
2558 return mKeyguardManager != null
2559 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2560 }
satokab751aa2010-09-14 19:17:36 +09002561 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002562 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002565 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002566
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002567 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002568 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002569 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002570
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002571 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002572 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2573 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002574 if (immis == null || immis.size() == 0) {
2575 return;
2576 }
2577
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002578 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579
satok688bd472012-02-09 20:09:17 +09002580 final List<ImeSubtypeListItem> imList =
2581 mImListManager.getSortedInputMethodAndSubtypeList(
2582 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002583
satokc3690562012-01-10 20:14:43 +09002584 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002585 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002586 if (currentSubtype != null) {
2587 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002588 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2589 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002590 }
2591 }
2592
Ken Wakasa761eb372011-03-04 19:06:18 +09002593 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002594 mIms = new InputMethodInfo[N];
2595 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002596 int checkedItem = 0;
2597 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002598 final ImeSubtypeListItem item = imList.get(i);
2599 mIms[i] = item.mImi;
2600 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002601 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002602 int subtypeId = mSubtypeIds[i];
2603 if ((subtypeId == NOT_A_SUBTYPE_ID)
2604 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2605 || (subtypeId == lastInputMethodSubtypeId)) {
2606 checkedItem = i;
2607 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002610 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002611 com.android.internal.R.styleable.DialogPreference,
2612 com.android.internal.R.attr.alertDialogStyle, 0);
2613 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002614 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002615 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002616 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002618 }
2619 })
2620 .setIcon(a.getDrawable(
2621 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2622 a.recycle();
satok01038492012-04-09 21:08:27 +09002623 final LayoutInflater inflater =
2624 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2625 final View tv = inflater.inflate(
2626 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2627 mDialogBuilder.setCustomTitle(tv);
2628
2629 // Setup layout for a toggle switch of the hardware keyboard
2630 mSwitchingDialogTitleView = tv;
2631 mSwitchingDialogTitleView.findViewById(
2632 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2633 mWindowManagerService.isHardKeyboardAvailable() ?
2634 View.VISIBLE : View.GONE);
2635 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2636 com.android.internal.R.id.hard_keyboard_switch));
2637 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2638 hardKeySwitch.setOnCheckedChangeListener(
2639 new OnCheckedChangeListener() {
2640 @Override
2641 public void onCheckedChanged(
2642 CompoundButton buttonView, boolean isChecked) {
2643 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002644 // Ensure that the input method dialog is dismissed when changing
2645 // the hardware keyboard state.
2646 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002647 }
2648 });
satokd87c2592010-09-29 11:52:06 +09002649
Ken Wakasa05dbb652011-08-22 15:22:43 +09002650 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2651 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2652 checkedItem);
2653
2654 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002655 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002656 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002657 public void onClick(DialogInterface dialog, int which) {
2658 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002659 if (mIms == null || mIms.length <= which
2660 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002661 return;
2662 }
2663 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002664 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002665 adapter.mCheckedItem = which;
2666 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002667 hideInputMethodMenu();
2668 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002669 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002670 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002671 subtypeId = NOT_A_SUBTYPE_ID;
2672 }
2673 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002674 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002677 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678
satokbc81b692011-08-26 16:22:22 +09002679 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002680 mDialogBuilder.setPositiveButton(
2681 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002682 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002683 @Override
satok7f35c8c2010-10-07 21:13:11 +09002684 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002685 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002686 }
2687 });
2688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002690 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 mSwitchingDialog.getWindow().setType(
2692 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002693 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2694 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002695 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 mSwitchingDialog.show();
2697 }
2698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002699
satok93d744d2012-05-09 17:14:08 +09002700 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002701 public final CharSequence mImeName;
2702 public final CharSequence mSubtypeName;
2703 public final InputMethodInfo mImi;
2704 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002705 private final boolean mIsSystemLocale;
2706 private final boolean mIsSystemLanguage;
2707
Ken Wakasa05dbb652011-08-22 15:22:43 +09002708 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002709 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002710 mImeName = imeName;
2711 mSubtypeName = subtypeName;
2712 mImi = imi;
2713 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002714 if (TextUtils.isEmpty(subtypeLocale)) {
2715 mIsSystemLocale = false;
2716 mIsSystemLanguage = false;
2717 } else {
2718 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2719 mIsSystemLanguage = mIsSystemLocale
2720 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2721 }
2722 }
2723
2724 @Override
2725 public int compareTo(ImeSubtypeListItem other) {
2726 if (TextUtils.isEmpty(mImeName)) {
2727 return 1;
2728 }
2729 if (TextUtils.isEmpty(other.mImeName)) {
2730 return -1;
2731 }
2732 if (!TextUtils.equals(mImeName, other.mImeName)) {
2733 return mImeName.toString().compareTo(other.mImeName.toString());
2734 }
2735 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2736 return 0;
2737 }
2738 if (mIsSystemLocale) {
2739 return -1;
2740 }
2741 if (other.mIsSystemLocale) {
2742 return 1;
2743 }
2744 if (mIsSystemLanguage) {
2745 return -1;
2746 }
2747 if (other.mIsSystemLanguage) {
2748 return 1;
2749 }
2750 if (TextUtils.isEmpty(mSubtypeName)) {
2751 return 1;
2752 }
2753 if (TextUtils.isEmpty(other.mSubtypeName)) {
2754 return -1;
2755 }
2756 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002757 }
2758 }
2759
2760 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2761 private final LayoutInflater mInflater;
2762 private final int mTextViewResourceId;
2763 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002764 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002765 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2766 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2767 super(context, textViewResourceId, itemsList);
2768 mTextViewResourceId = textViewResourceId;
2769 mItemsList = itemsList;
2770 mCheckedItem = checkedItem;
2771 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2772 }
2773
2774 @Override
2775 public View getView(int position, View convertView, ViewGroup parent) {
2776 final View view = convertView != null ? convertView
2777 : mInflater.inflate(mTextViewResourceId, null);
2778 if (position < 0 || position >= mItemsList.size()) return view;
2779 final ImeSubtypeListItem item = mItemsList.get(position);
2780 final CharSequence imeName = item.mImeName;
2781 final CharSequence subtypeName = item.mSubtypeName;
2782 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2783 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2784 if (TextUtils.isEmpty(subtypeName)) {
2785 firstTextView.setText(imeName);
2786 secondTextView.setVisibility(View.GONE);
2787 } else {
2788 firstTextView.setText(subtypeName);
2789 secondTextView.setText(imeName);
2790 secondTextView.setVisibility(View.VISIBLE);
2791 }
2792 final RadioButton radioButton =
2793 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2794 radioButton.setChecked(position == mCheckedItem);
2795 return view;
2796 }
2797 }
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002800 synchronized (mMethodMap) {
2801 hideInputMethodMenuLocked();
2802 }
2803 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002804
The Android Open Source Project10592532009-03-18 17:39:46 -07002805 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002806 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807
The Android Open Source Project10592532009-03-18 17:39:46 -07002808 if (mSwitchingDialog != null) {
2809 mSwitchingDialog.dismiss();
2810 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002812
The Android Open Source Project10592532009-03-18 17:39:46 -07002813 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002814 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002818
satok42c5a162011-05-26 16:46:14 +09002819 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002821 // TODO: Make this work even for non-current users?
2822 if (!calledFromValidUser()) {
2823 return false;
2824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 synchronized (mMethodMap) {
2826 if (mContext.checkCallingOrSelfPermission(
2827 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2828 != PackageManager.PERMISSION_GRANTED) {
2829 throw new SecurityException(
2830 "Requires permission "
2831 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2832 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 long ident = Binder.clearCallingIdentity();
2835 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002836 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 } finally {
2838 Binder.restoreCallingIdentity(ident);
2839 }
2840 }
2841 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002842
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002843 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2844 // Make sure this is a valid input method.
2845 InputMethodInfo imm = mMethodMap.get(id);
2846 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002847 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002848 }
2849
satokd87c2592010-09-29 11:52:06 +09002850 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2851 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002852
satokd87c2592010-09-29 11:52:06 +09002853 if (enabled) {
2854 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2855 if (pair.first.equals(id)) {
2856 // We are enabling this input method, but it is already enabled.
2857 // Nothing to do. The previous state was enabled.
2858 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002859 }
2860 }
satokd87c2592010-09-29 11:52:06 +09002861 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2862 // Previous state was disabled.
2863 return false;
2864 } else {
2865 StringBuilder builder = new StringBuilder();
2866 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2867 builder, enabledInputMethodsList, id)) {
2868 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002869 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002870 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2871 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2872 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002873 }
2874 // Previous state was enabled.
2875 return true;
2876 } else {
2877 // We are disabling the input method but it is already disabled.
2878 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002879 return false;
2880 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002881 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002882 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002883
satok723a27e2010-11-11 14:58:11 +09002884 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2885 boolean setSubtypeOnly) {
2886 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002887 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002888
2889 // Set Subtype here
2890 if (imi == null || subtypeId < 0) {
2891 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002892 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002893 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002894 if (subtypeId < imi.getSubtypeCount()) {
2895 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2896 mSettings.putSelectedSubtype(subtype.hashCode());
2897 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002898 } else {
2899 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002900 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002901 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002902 }
satokab751aa2010-09-14 19:17:36 +09002903 }
satok723a27e2010-11-11 14:58:11 +09002904
satok4c0e7152012-06-20 20:08:44 +09002905 // Workaround.
2906 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2907 // IMEs are not recognized and considered uninstalled.
2908 // Actually, we can't move everything after SystemReady because
2909 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2910 // the default IME here and try cheking the default IME again in systemReady().
2911 // TODO: Do nothing before system ready and implement a separated logic for
2912 // the encryption lock screen.
2913 // TODO: ASEC should be ready before IMMS is instantiated.
2914 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002915 // Set InputMethod here
2916 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2917 }
2918 }
2919
2920 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2921 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2922 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2923 // newDefaultIme is empty when there is no candidate for the selected IME.
2924 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2925 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2926 if (subtypeHashCode != null) {
2927 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002928 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002929 imi, Integer.valueOf(subtypeHashCode));
2930 } catch (NumberFormatException e) {
2931 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2932 }
2933 }
2934 }
2935 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002936 }
2937
satok4e4569d2010-11-19 18:45:53 +09002938 // If there are no selected shortcuts, tries finding the most applicable ones.
2939 private Pair<InputMethodInfo, InputMethodSubtype>
2940 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2941 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2942 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002943 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002944 boolean foundInSystemIME = false;
2945
2946 // Search applicable subtype for each InputMethodInfo
2947 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002948 final String imiId = imi.getId();
2949 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2950 continue;
2951 }
satokcd7cd292010-11-20 15:46:23 +09002952 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002953 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002954 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002955 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002956 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002957 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002958 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002959 }
satokdf31ae62011-01-15 06:19:44 +09002960 // 2. Search by the system locale from enabledSubtypes.
2961 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002962 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002963 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002964 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002965 }
satoka86f5e42011-09-02 17:12:42 +09002966 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002967 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002968 final ArrayList<InputMethodSubtype> subtypesForSearch =
2969 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002970 ? InputMethodUtils.getSubtypes(imi)
2971 : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002972 // 4. Search by the current subtype's locale from all subtypes.
2973 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002974 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002975 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002976 }
2977 // 5. Search by the system locale from all subtypes.
2978 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002979 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002980 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002981 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002982 }
satokcd7cd292010-11-20 15:46:23 +09002983 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09002984 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09002985 // The current input method is the most applicable IME.
2986 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002987 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09002988 break;
satok7599a7fb2010-12-22 13:45:23 +09002989 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09002990 // The system input method is 2nd applicable IME.
2991 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09002992 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09002993 if ((imi.getServiceInfo().applicationInfo.flags
2994 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2995 foundInSystemIME = true;
2996 }
satok4e4569d2010-11-19 18:45:53 +09002997 }
2998 }
2999 }
3000 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003001 if (mostApplicableIMI != null) {
3002 Slog.w(TAG, "Most applicable shortcut input method was:"
3003 + mostApplicableIMI.getId());
3004 if (mostApplicableSubtype != null) {
3005 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3006 + "," + mostApplicableSubtype.getMode() + ","
3007 + mostApplicableSubtype.getLocale());
3008 }
3009 }
satok4e4569d2010-11-19 18:45:53 +09003010 }
satokcd7cd292010-11-20 15:46:23 +09003011 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003012 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003013 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003014 } else {
3015 return null;
3016 }
3017 }
3018
satokab751aa2010-09-14 19:17:36 +09003019 /**
3020 * @return Return the current subtype of this input method.
3021 */
satok42c5a162011-05-26 16:46:14 +09003022 @Override
satokab751aa2010-09-14 19:17:36 +09003023 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003024 // TODO: Make this work even for non-current users?
3025 if (!calledFromValidUser()) {
3026 return null;
3027 }
3028 synchronized (mMethodMap) {
3029 return getCurrentInputMethodSubtypeLocked();
3030 }
3031 }
3032
3033 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003034 if (mCurMethodId == null) {
3035 return null;
3036 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003037 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003038 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3039 if (imi == null || imi.getSubtypeCount() == 0) {
3040 return null;
satok4e4569d2010-11-19 18:45:53 +09003041 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003043 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3044 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003045 if (subtypeId == NOT_A_SUBTYPE_ID) {
3046 // If there are no selected subtypes, the framework will try to find
3047 // the most applicable subtype from explicitly or implicitly enabled
3048 // subtypes.
3049 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003050 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003051 // If there is only one explicitly or implicitly enabled subtype,
3052 // just returns it.
3053 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3054 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3055 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003056 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003057 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003058 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003059 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003060 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003061 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3062 true);
satok4e4569d2010-11-19 18:45:53 +09003063 }
satok3ef8b292010-11-23 06:06:29 +09003064 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003065 } else {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003066 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003067 }
3068 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003069 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003070 }
3071
satokf3db1af2010-11-23 13:34:33 +09003072 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3073 InputMethodSubtype subtype) {
3074 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3075 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3076 } else {
3077 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3078 subtypes.add(subtype);
3079 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3080 }
3081 }
3082
satok4e4569d2010-11-19 18:45:53 +09003083 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003084 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003085 @Override
satok4e4569d2010-11-19 18:45:53 +09003086 public List getShortcutInputMethodsAndSubtypes() {
3087 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003088 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003089 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003090 // If there are no selected shortcut subtypes, the framework will try to find
3091 // the most applicable subtype from all subtypes whose mode is
3092 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003093 Pair<InputMethodInfo, InputMethodSubtype> info =
3094 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003095 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003096 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003097 ret.add(info.first);
3098 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003099 }
satok3da92232011-01-11 22:46:30 +09003100 return ret;
satokf3db1af2010-11-23 13:34:33 +09003101 }
satokf3db1af2010-11-23 13:34:33 +09003102 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3103 ret.add(imi);
3104 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3105 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003106 }
3107 }
satokf3db1af2010-11-23 13:34:33 +09003108 return ret;
satok4e4569d2010-11-19 18:45:53 +09003109 }
3110 }
3111
satok42c5a162011-05-26 16:46:14 +09003112 @Override
satokb66d2872010-11-10 01:04:04 +09003113 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003114 // TODO: Make this work even for non-current users?
3115 if (!calledFromValidUser()) {
3116 return false;
3117 }
satokb66d2872010-11-10 01:04:04 +09003118 synchronized (mMethodMap) {
3119 if (subtype != null && mCurMethodId != null) {
3120 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003121 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003122 if (subtypeId != NOT_A_SUBTYPE_ID) {
3123 setInputMethodLocked(mCurMethodId, subtypeId);
3124 return true;
3125 }
3126 }
3127 return false;
3128 }
3129 }
3130
satok688bd472012-02-09 20:09:17 +09003131 private static class InputMethodAndSubtypeListManager {
3132 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003133 // Used to load label
satok688bd472012-02-09 20:09:17 +09003134 private final PackageManager mPm;
3135 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003136 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003137 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3138 mContext = context;
3139 mPm = context.getPackageManager();
3140 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003141 final Locale locale = context.getResources().getConfiguration().locale;
3142 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003143 }
3144
3145 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3146 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3147 new Comparator<InputMethodInfo>() {
3148 @Override
3149 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3150 if (imi2 == null) return 0;
3151 if (imi1 == null) return 1;
3152 if (mPm == null) {
3153 return imi1.getId().compareTo(imi2.getId());
3154 }
3155 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3156 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3157 return imiId1.toString().compareTo(imiId2.toString());
3158 }
3159 });
3160
3161 public ImeSubtypeListItem getNextInputMethod(
3162 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3163 if (imi == null) {
3164 return null;
3165 }
3166 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3167 if (imList.size() <= 1) {
3168 return null;
3169 }
3170 final int N = imList.size();
3171 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003172 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003173 : NOT_A_SUBTYPE_ID;
3174 for (int i = 0; i < N; ++i) {
3175 final ImeSubtypeListItem isli = imList.get(i);
3176 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3177 if (!onlyCurrentIme) {
3178 return imList.get((i + 1) % N);
3179 }
3180 for (int j = 0; j < N - 1; ++j) {
3181 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3182 if (candidate.mImi.equals(imi)) {
3183 return candidate;
3184 }
3185 }
3186 return null;
3187 }
3188 }
3189 return null;
3190 }
3191
3192 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3193 return getSortedInputMethodAndSubtypeList(true, false, false);
3194 }
3195
3196 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3197 boolean inputShown, boolean isScreenLocked) {
3198 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3199 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3200 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3201 if (immis == null || immis.size() == 0) {
3202 return Collections.emptyList();
3203 }
3204 mSortedImmis.clear();
3205 mSortedImmis.putAll(immis);
3206 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3207 if (imi == null) continue;
3208 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3209 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3210 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3211 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3212 }
satok688bd472012-02-09 20:09:17 +09003213 final CharSequence imeLabel = imi.loadLabel(mPm);
3214 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3215 final int subtypeCount = imi.getSubtypeCount();
3216 if (DEBUG) {
3217 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3218 }
3219 for (int j = 0; j < subtypeCount; ++j) {
3220 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3221 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3222 // We show all enabled IMEs and subtypes when an IME is shown.
3223 if (enabledSubtypeSet.contains(subtypeHashCode)
3224 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3225 final CharSequence subtypeLabel =
3226 subtype.overridesImplicitlyEnabledSubtype() ? null
3227 : subtype.getDisplayName(mContext, imi.getPackageName(),
3228 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003229 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3230 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003231
3232 // Removing this subtype from enabledSubtypeSet because we no longer
3233 // need to add an entry of this subtype to imList to avoid duplicated
3234 // entries.
3235 enabledSubtypeSet.remove(subtypeHashCode);
3236 }
3237 }
3238 } else {
satok93d744d2012-05-09 17:14:08 +09003239 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3240 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003241 }
3242 }
satok93d744d2012-05-09 17:14:08 +09003243 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003244 return imList;
3245 }
3246 }
3247
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003248 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003249 private static class InputMethodFileManager {
3250 private static final String SYSTEM_PATH = "system";
3251 private static final String INPUT_METHOD_PATH = "inputmethod";
3252 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3253 private static final String NODE_SUBTYPES = "subtypes";
3254 private static final String NODE_SUBTYPE = "subtype";
3255 private static final String NODE_IMI = "imi";
3256 private static final String ATTR_ID = "id";
3257 private static final String ATTR_LABEL = "label";
3258 private static final String ATTR_ICON = "icon";
3259 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3260 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3261 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3262 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3263 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3264 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003265 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003266 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003267 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003268 if (methodMap == null) {
3269 throw new NullPointerException("methodMap is null");
3270 }
3271 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003272 final File systemDir = userId == UserHandle.USER_OWNER
3273 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3274 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003275 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3276 if (!inputMethodDir.mkdirs()) {
3277 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3278 }
3279 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3280 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3281 if (!subtypeFile.exists()) {
3282 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003283 writeAdditionalInputMethodSubtypes(
3284 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003285 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003286 readAdditionalInputMethodSubtypes(
3287 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003288 }
3289 }
3290
3291 private void deleteAllInputMethodSubtypes(String imiId) {
3292 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003293 mAdditionalSubtypesMap.remove(imiId);
3294 writeAdditionalInputMethodSubtypes(
3295 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003296 }
3297 }
3298
3299 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003300 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003301 synchronized (mMethodMap) {
3302 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3303 final int N = additionalSubtypes.length;
3304 for (int i = 0; i < N; ++i) {
3305 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003306 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003307 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003308 } else {
3309 Slog.w(TAG, "Duplicated subtype definition found: "
3310 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003311 }
3312 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003313 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3314 writeAdditionalInputMethodSubtypes(
3315 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003316 }
3317 }
3318
3319 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3320 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003321 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003322 }
3323 }
3324
3325 private static void writeAdditionalInputMethodSubtypes(
3326 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3327 HashMap<String, InputMethodInfo> methodMap) {
3328 // Safety net for the case that this function is called before methodMap is set.
3329 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3330 FileOutputStream fos = null;
3331 try {
3332 fos = subtypesFile.startWrite();
3333 final XmlSerializer out = new FastXmlSerializer();
3334 out.setOutput(fos, "utf-8");
3335 out.startDocument(null, true);
3336 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3337 out.startTag(null, NODE_SUBTYPES);
3338 for (String imiId : allSubtypes.keySet()) {
3339 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3340 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3341 continue;
3342 }
3343 out.startTag(null, NODE_IMI);
3344 out.attribute(null, ATTR_ID, imiId);
3345 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3346 final int N = subtypesList.size();
3347 for (int i = 0; i < N; ++i) {
3348 final InputMethodSubtype subtype = subtypesList.get(i);
3349 out.startTag(null, NODE_SUBTYPE);
3350 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3351 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3352 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3353 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3354 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3355 out.attribute(null, ATTR_IS_AUXILIARY,
3356 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3357 out.endTag(null, NODE_SUBTYPE);
3358 }
3359 out.endTag(null, NODE_IMI);
3360 }
3361 out.endTag(null, NODE_SUBTYPES);
3362 out.endDocument();
3363 subtypesFile.finishWrite(fos);
3364 } catch (java.io.IOException e) {
3365 Slog.w(TAG, "Error writing subtypes", e);
3366 if (fos != null) {
3367 subtypesFile.failWrite(fos);
3368 }
3369 }
3370 }
3371
3372 private static void readAdditionalInputMethodSubtypes(
3373 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3374 if (allSubtypes == null || subtypesFile == null) return;
3375 allSubtypes.clear();
3376 FileInputStream fis = null;
3377 try {
3378 fis = subtypesFile.openRead();
3379 final XmlPullParser parser = Xml.newPullParser();
3380 parser.setInput(fis, null);
3381 int type = parser.getEventType();
3382 // Skip parsing until START_TAG
3383 while ((type = parser.next()) != XmlPullParser.START_TAG
3384 && type != XmlPullParser.END_DOCUMENT) {}
3385 String firstNodeName = parser.getName();
3386 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3387 throw new XmlPullParserException("Xml doesn't start with subtypes");
3388 }
3389 final int depth =parser.getDepth();
3390 String currentImiId = null;
3391 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3392 while (((type = parser.next()) != XmlPullParser.END_TAG
3393 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3394 if (type != XmlPullParser.START_TAG)
3395 continue;
3396 final String nodeName = parser.getName();
3397 if (NODE_IMI.equals(nodeName)) {
3398 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3399 if (TextUtils.isEmpty(currentImiId)) {
3400 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3401 continue;
3402 }
3403 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3404 allSubtypes.put(currentImiId, tempSubtypesArray);
3405 } else if (NODE_SUBTYPE.equals(nodeName)) {
3406 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3407 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3408 continue;
3409 }
3410 final int icon = Integer.valueOf(
3411 parser.getAttributeValue(null, ATTR_ICON));
3412 final int label = Integer.valueOf(
3413 parser.getAttributeValue(null, ATTR_LABEL));
3414 final String imeSubtypeLocale =
3415 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3416 final String imeSubtypeMode =
3417 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3418 final String imeSubtypeExtraValue =
3419 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003420 final boolean isAuxiliary = "1".equals(String.valueOf(
3421 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003422 final InputMethodSubtype subtype =
3423 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3424 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3425 tempSubtypesArray.add(subtype);
3426 }
3427 }
3428 } catch (XmlPullParserException e) {
3429 Slog.w(TAG, "Error reading subtypes: " + e);
3430 return;
3431 } catch (java.io.IOException e) {
3432 Slog.w(TAG, "Error reading subtypes: " + e);
3433 return;
3434 } catch (NumberFormatException e) {
3435 Slog.w(TAG, "Error reading subtypes: " + e);
3436 return;
3437 } finally {
3438 if (fis != null) {
3439 try {
3440 fis.close();
3441 } catch (java.io.IOException e1) {
3442 Slog.w(TAG, "Failed to close.");
3443 }
3444 }
3445 }
3446 }
3447 }
3448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003450 // Utilities for debug
3451 private static String getStackTrace() {
3452 final StringBuilder sb = new StringBuilder();
3453 try {
3454 throw new RuntimeException();
3455 } catch (RuntimeException e) {
3456 final StackTraceElement[] frames = e.getStackTrace();
3457 // Start at 1 because the first frame is here and we don't care about it
3458 for (int j = 1; j < frames.length; ++j) {
3459 sb.append(frames[j].toString() + "\n");
3460 }
3461 }
3462 return sb.toString();
3463 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 @Override
3466 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3467 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3468 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3471 + Binder.getCallingPid()
3472 + ", uid=" + Binder.getCallingUid());
3473 return;
3474 }
3475
3476 IInputMethod method;
3477 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 synchronized (mMethodMap) {
3482 p.println("Current Input Method Manager state:");
3483 int N = mMethodList.size();
3484 p.println(" Input Methods:");
3485 for (int i=0; i<N; i++) {
3486 InputMethodInfo info = mMethodList.get(i);
3487 p.println(" InputMethod #" + i + ":");
3488 info.dump(p, " ");
3489 }
3490 p.println(" Clients:");
3491 for (ClientState ci : mClients.values()) {
3492 p.println(" Client " + ci + ":");
3493 p.println(" client=" + ci.client);
3494 p.println(" inputContext=" + ci.inputContext);
3495 p.println(" sessionRequested=" + ci.sessionRequested);
3496 p.println(" curSession=" + ci.curSession);
3497 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003498 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003500 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3501 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3503 + " mBoundToMethod=" + mBoundToMethod);
3504 p.println(" mCurToken=" + mCurToken);
3505 p.println(" mCurIntent=" + mCurIntent);
3506 method = mCurMethod;
3507 p.println(" mCurMethod=" + mCurMethod);
3508 p.println(" mEnabledSession=" + mEnabledSession);
3509 p.println(" mShowRequested=" + mShowRequested
3510 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3511 + " mShowForced=" + mShowForced
3512 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003513 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003515
Jeff Brownb88102f2010-09-08 11:49:43 -07003516 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 pw.flush();
3519 try {
3520 client.client.asBinder().dump(fd, args);
3521 } catch (RemoteException e) {
3522 p.println("Input method client dead: " + e);
3523 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003524 } else {
3525 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003527
Jeff Brownb88102f2010-09-08 11:49:43 -07003528 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 pw.flush();
3531 try {
3532 method.asBinder().dump(fd, args);
3533 } catch (RemoteException e) {
3534 p.println("Input method service dead: " + e);
3535 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003536 } else {
3537 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 }
3539 }
3540}