blob: 3baa565ece9b53b4b7540a2e943a583087d037a3 [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);
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900795 // For secondary users, the list of enabled IMEs may not have been updated since the
796 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
797 // not be empty even if the IME has been uninstalled by the primary user.
798 // Even in such cases, IMMS works fine because it will find the most applicable
799 // IME for that user.
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900800 if (DEBUG) {
801 Slog.d(TAG, "Switch user: " + newUserId + " current ime = " + defaultImiId);
802 }
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +0900803 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900804 needsToResetDefaultIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900805 }
806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 @Override
808 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
809 throws RemoteException {
810 try {
811 return super.onTransact(code, data, reply, flags);
812 } catch (RuntimeException e) {
813 // The input method manager only throws security exceptions, so let's
814 // log all others.
815 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 Slog.e(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818 throw e;
819 }
820 }
821
Dianne Hackborn661cd522011-08-22 00:26:20 -0700822 public void systemReady(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700823 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900824 if (DEBUG) {
825 Slog.d(TAG, "--- systemReady");
826 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700827 if (!mSystemReady) {
828 mSystemReady = true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900829 mKeyguardManager =
830 (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700831 mNotificationManager = (NotificationManager)
832 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
833 mStatusBar = statusBar;
834 statusBar.setIconVisibility("ime", false);
835 updateImeWindowStatusLocked();
satokb858c732011-07-22 19:54:34 +0900836 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
837 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +0900838 if (mShowOngoingImeSwitcherForPhones) {
839 mWindowManagerService.setOnHardKeyboardStatusChangeListener(
840 mHardKeyboardListener);
841 }
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900842 buildInputMethodListLocked(mMethodList, mMethodMap,
843 !mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +0900844 if (!mImeSelectedOnBoot) {
845 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
846 checkCurrentLocaleChangedLocked();
847 }
848 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -0700849 try {
850 startInputInnerLocked();
851 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -0700853 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700854 }
855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800857
satok15452a42011-10-28 17:58:28 +0900858 private void setImeWindowVisibilityStatusHiddenLocked() {
859 mImeWindowVis = 0;
860 updateImeWindowStatusLocked();
861 }
862
satok3afd6c02011-11-18 08:38:19 +0900863 private void refreshImeWindowVisibilityLocked() {
864 final Configuration conf = mRes.getConfiguration();
865 final boolean haveHardKeyboard = conf.keyboard
866 != Configuration.KEYBOARD_NOKEYS;
867 final boolean hardKeyShown = haveHardKeyboard
868 && conf.hardKeyboardHidden
869 != Configuration.HARDKEYBOARDHIDDEN_YES;
John Spurlockbc7b6fc2012-11-14 08:51:07 -0500870 final boolean isScreenLocked =
871 mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
872 final boolean isScreenSecurelyLocked =
873 isScreenLocked && mKeyguardManager.isKeyguardSecure();
874 final boolean inputShown = mInputShown && (!isScreenLocked || mInputBoundToKeyguard);
875 mImeWindowVis = (!isScreenSecurelyLocked && (inputShown || hardKeyShown)) ?
satok3afd6c02011-11-18 08:38:19 +0900876 (InputMethodService.IME_ACTIVE | InputMethodService.IME_VISIBLE) : 0;
877 updateImeWindowStatusLocked();
878 }
879
satok15452a42011-10-28 17:58:28 +0900880 private void updateImeWindowStatusLocked() {
satokdbf29502011-08-25 15:28:23 +0900881 setImeWindowStatus(mCurToken, mImeWindowVis, mBackDisposition);
Dianne Hackborn661cd522011-08-22 00:26:20 -0700882 }
883
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900884 // ---------------------------------------------------------------------------------------
885 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
886 // 1) it comes from the system process
887 // 2) the calling process' user id is identical to the current user id IMMS thinks.
888 private boolean calledFromValidUser() {
889 final int uid = Binder.getCallingUid();
890 final int userId = UserHandle.getUserId(uid);
891 if (DEBUG) {
892 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
893 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
894 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataokac86884c2012-10-09 15:20:29 +0900895 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900896 }
897 if (uid == Process.SYSTEM_UID || userId == mSettings.getCurrentUserId()) {
898 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900899 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +0900900
901 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
902 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
903 // must not manage background users' states in any functions.
904 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
905 // by a token.
906 if (mContext.checkCallingOrSelfPermission(
907 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
908 == PackageManager.PERMISSION_GRANTED) {
909 if (DEBUG) {
910 Slog.d(TAG, "--- Access granted because the calling process has "
911 + "the INTERACT_ACROSS_USERS_FULL permission");
912 }
913 return true;
914 }
915 Slog.w(TAG, "--- IPC called from background users. Ignore. \n" + getStackTrace());
916 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900917 }
918
919 private boolean bindCurrentInputMethodService(
920 Intent service, ServiceConnection conn, int flags) {
921 if (service == null || conn == null) {
922 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
923 return false;
924 }
Amith Yamasani27b89e62013-01-16 12:30:11 -0800925 return mContext.bindServiceAsUser(service, conn, flags,
926 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900927 }
928
satoke7c6998e2011-06-03 17:57:59 +0900929 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900931 // TODO: Make this work even for non-current users?
932 if (!calledFromValidUser()) {
933 return Collections.emptyList();
934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 synchronized (mMethodMap) {
936 return new ArrayList<InputMethodInfo>(mMethodList);
937 }
938 }
939
satoke7c6998e2011-06-03 17:57:59 +0900940 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900942 // TODO: Make this work even for non-current users?
943 if (!calledFromValidUser()) {
944 return Collections.emptyList();
945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +0900947 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949 }
950
satokbb4aa062011-01-19 21:40:27 +0900951 private HashMap<InputMethodInfo, List<InputMethodSubtype>>
952 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked() {
953 HashMap<InputMethodInfo, List<InputMethodSubtype>> enabledInputMethodAndSubtypes =
954 new HashMap<InputMethodInfo, List<InputMethodSubtype>>();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900955 for (InputMethodInfo imi: mSettings.getEnabledInputMethodListLocked()) {
satokbb4aa062011-01-19 21:40:27 +0900956 enabledInputMethodAndSubtypes.put(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900957 imi, mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true));
satokbb4aa062011-01-19 21:40:27 +0900958 }
959 return enabledInputMethodAndSubtypes;
960 }
961
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900962 /**
963 * @param imi if null, returns enabled subtypes for the current imi
964 * @return enabled subtypes of the specified imi
965 */
satoke7c6998e2011-06-03 17:57:59 +0900966 @Override
satok16331c82010-12-20 23:48:46 +0900967 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(InputMethodInfo imi,
968 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900969 // TODO: Make this work even for non-current users?
970 if (!calledFromValidUser()) {
971 return Collections.emptyList();
972 }
satok67ddf9c2010-11-17 09:45:54 +0900973 synchronized (mMethodMap) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +0900974 if (imi == null && mCurMethodId != null) {
975 imi = mMethodMap.get(mCurMethodId);
976 }
977 return mSettings.getEnabledInputMethodSubtypeListLocked(
978 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +0900979 }
980 }
981
satoke7c6998e2011-06-03 17:57:59 +0900982 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 public void addClient(IInputMethodClient client,
984 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900985 if (!calledFromValidUser()) {
986 return;
987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 synchronized (mMethodMap) {
989 mClients.put(client.asBinder(), new ClientState(client,
990 inputContext, uid, pid));
991 }
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
satoke7c6998e2011-06-03 17:57:59 +0900994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900996 if (!calledFromValidUser()) {
997 return;
998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001000 ClientState cs = mClients.remove(client.asBinder());
1001 if (cs != null) {
1002 clearClientSessionLocked(cs);
1003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 void executeOrSendMessage(IInterface target, Message msg) {
1008 if (target.asBinder() instanceof Binder) {
1009 mCaller.sendMessage(msg);
1010 } else {
1011 handleMessage(msg);
1012 msg.recycle();
1013 }
1014 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001015
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001016 void unbindCurrentClientLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (mCurClient != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001018 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 + mCurClient.client.asBinder());
1020 if (mBoundToMethod) {
1021 mBoundToMethod = false;
1022 if (mCurMethod != null) {
1023 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1024 MSG_UNBIND_INPUT, mCurMethod));
1025 }
1026 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001027
1028 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1029 MSG_SET_ACTIVE, 0, mCurClient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1031 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1032 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001034
The Android Open Source Project10592532009-03-18 17:39:46 -07001035 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
1037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 private int getImeShowFlags() {
1040 int flags = 0;
1041 if (mShowForced) {
1042 flags |= InputMethod.SHOW_FORCED
1043 | InputMethod.SHOW_EXPLICIT;
1044 } else if (mShowExplicitlyRequested) {
1045 flags |= InputMethod.SHOW_EXPLICIT;
1046 }
1047 return flags;
1048 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 private int getAppShowFlags() {
1051 int flags = 0;
1052 if (mShowForced) {
1053 flags |= InputMethodManager.SHOW_FORCED;
1054 } else if (!mShowExplicitlyRequested) {
1055 flags |= InputMethodManager.SHOW_IMPLICIT;
1056 }
1057 return flags;
1058 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001059
Dianne Hackborn7663d802012-02-24 13:08:49 -08001060 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 if (!mBoundToMethod) {
1062 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1063 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1064 mBoundToMethod = true;
1065 }
1066 final SessionState session = mCurClient.curSession;
1067 if (initial) {
1068 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1069 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1070 } else {
1071 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1072 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1073 }
1074 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001075 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001076 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001078 return new InputBindResult(session.session,
1079 session.channel != null ? session.channel.dup() : null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 InputBindResult startInputLocked(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001083 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 // If no method is currently selected, do nothing.
1085 if (mCurMethodId == null) {
1086 return mNoBinding;
1087 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 ClientState cs = mClients.get(client.asBinder());
1090 if (cs == null) {
1091 throw new IllegalArgumentException("unknown client "
1092 + client.asBinder());
1093 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 try {
1096 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1097 // Check with the window manager to make sure this client actually
1098 // has a window with focus. If not, reject. This is thread safe
1099 // because if the focus changes some time before or after, the
1100 // next client receiving focus that has any interest in input will
1101 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001102 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1104 return null;
1105 }
1106 } catch (RemoteException e) {
1107 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001108
Dianne Hackborn7663d802012-02-24 13:08:49 -08001109 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1110 }
1111
1112 InputBindResult startInputUncheckedLocked(ClientState cs,
1113 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
1114 // If no method is currently selected, do nothing.
1115 if (mCurMethodId == null) {
1116 return mNoBinding;
1117 }
1118
John Spurlockbc7b6fc2012-11-14 08:51:07 -05001119 if (mCurClient == null) {
1120 mInputBoundToKeyguard = mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1121 if (DEBUG) {
1122 Slog.v(TAG, "New bind. keyguard = " + mInputBoundToKeyguard);
1123 }
1124 }
1125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 if (mCurClient != cs) {
1127 // If the client is changing, we need to switch over to the new
1128 // one.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001129 unbindCurrentClientLocked();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001130 if (DEBUG) Slog.v(TAG, "switching to client: client = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 + cs.client.asBinder());
1132
1133 // If the screen is on, inform the new client it is active
1134 if (mScreenOn) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001135 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
1136 MSG_SET_ACTIVE, mScreenOn ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 // Bump up the sequence for this client and attach it.
1141 mCurSeq++;
1142 if (mCurSeq <= 0) mCurSeq = 1;
1143 mCurClient = cs;
1144 mCurInputContext = inputContext;
1145 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 // Check if the input method is changing.
1148 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1149 if (cs.curSession != null) {
1150 // Fast case: if we are already connected to the input method,
1151 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001152 return attachNewInputLocked(
1153 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 }
1155 if (mHaveConnection) {
1156 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 // Return to client, and we will get back with it when
1158 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001159 requestClientSessionLocked(cs);
1160 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 } else if (SystemClock.uptimeMillis()
1162 < (mLastBindTime+TIME_TO_RECONNECT)) {
1163 // In this case we have connected to the service, but
1164 // don't yet have its interface. If it hasn't been too
1165 // long since we did the connection, we'll return to
1166 // the client and wait to get the service interface so
1167 // we can report back. If it has been too long, we want
1168 // to fall through so we can try a disconnect/reconnect
1169 // to see if we can get back in touch with the service.
Jeff Brownc28867a2013-03-26 15:42:39 -07001170 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1173 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175 }
1176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001178 return startInputInnerLocked();
1179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001180
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001181 InputBindResult startInputInnerLocked() {
1182 if (mCurMethodId == null) {
1183 return mNoBinding;
1184 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001185
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001186 if (!mSystemReady) {
1187 // If the system is not yet ready, we shouldn't be running third
1188 // party code.
Jeff Brownc28867a2013-03-26 15:42:39 -07001189 return new InputBindResult(null, null, mCurMethodId, mCurSeq);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001190 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1193 if (info == null) {
1194 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001197 unbindCurrentMethodLocked(false, true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1200 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001201 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1202 com.android.internal.R.string.input_method_binding_label);
1203 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1204 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001205 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001206 | Context.BIND_NOT_VISIBLE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mLastBindTime = SystemClock.uptimeMillis();
1208 mHaveConnection = true;
1209 mCurId = info.getId();
1210 mCurToken = new Binder();
1211 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001212 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 mIWindowManager.addWindowToken(mCurToken,
1214 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1215 } catch (RemoteException e) {
1216 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001217 return new InputBindResult(null, null, mCurId, mCurSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 } else {
1219 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001220 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 + mCurIntent);
1222 }
1223 return null;
1224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
satoke7c6998e2011-06-03 17:57:59 +09001226 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 public InputBindResult startInput(IInputMethodClient client,
Dianne Hackborn7663d802012-02-24 13:08:49 -08001228 IInputContext inputContext, EditorInfo attribute, int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001229 if (!calledFromValidUser()) {
1230 return null;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 synchronized (mMethodMap) {
1233 final long ident = Binder.clearCallingIdentity();
1234 try {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001235 return startInputLocked(client, inputContext, attribute, controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 } finally {
1237 Binder.restoreCallingIdentity(ident);
1238 }
1239 }
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
satoke7c6998e2011-06-03 17:57:59 +09001242 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 public void finishInput(IInputMethodClient client) {
1244 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001245
satoke7c6998e2011-06-03 17:57:59 +09001246 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 public void onServiceConnected(ComponentName name, IBinder service) {
1248 synchronized (mMethodMap) {
1249 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1250 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001251 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001252 Slog.w(TAG, "Service connected without a token!");
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001253 unbindCurrentMethodLocked(false, false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001254 return;
1255 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001256 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001257 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1258 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001260 clearClientSessionLocked(mCurClient);
1261 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 }
1264 }
1265 }
1266
Jeff Brownc28867a2013-03-26 15:42:39 -07001267 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1268 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 synchronized (mMethodMap) {
1270 if (mCurMethod != null && method != null
1271 && mCurMethod.asBinder() == method.asBinder()) {
1272 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001273 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001275 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001276 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 if (res.method != null) {
1278 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
1279 MSG_BIND_METHOD, mCurClient.client, res));
1280 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001281 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283 }
1284 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001285
1286 // Session abandoned. Close its associated input channel.
1287 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001289
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001290 void unbindCurrentMethodLocked(boolean reportToClient, boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001291 if (mVisibleBound) {
1292 mContext.unbindService(mVisibleConnection);
1293 mVisibleBound = false;
1294 }
1295
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001296 if (mHaveConnection) {
1297 mContext.unbindService(this);
1298 mHaveConnection = false;
1299 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001300
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001301 if (mCurToken != null) {
1302 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001303 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001304 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001305 // The current IME is shown. Hence an IME switch (transition) is happening.
1306 mWindowManagerService.saveLastInputMethodWindowForTransition();
1307 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001308 mIWindowManager.removeWindowToken(mCurToken);
1309 } catch (RemoteException e) {
1310 }
1311 mCurToken = null;
1312 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001313
The Android Open Source Project10592532009-03-18 17:39:46 -07001314 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001315 clearCurMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001317 if (reportToClient && mCurClient != null) {
1318 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1319 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1320 }
1321 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001322
1323 void requestClientSessionLocked(ClientState cs) {
1324 if (!cs.sessionRequested) {
1325 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1326 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1327 cs.sessionRequested = true;
1328 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1329 MSG_CREATE_SESSION, mCurMethod, channels[1],
1330 new MethodCallback(this, mCurMethod, channels[0])));
1331 }
1332 }
1333
1334 void clearClientSessionLocked(ClientState cs) {
1335 finishSessionLocked(cs.curSession);
1336 cs.curSession = null;
1337 cs.sessionRequested = false;
1338 }
1339
1340 private void finishSessionLocked(SessionState sessionState) {
1341 if (sessionState != null) {
1342 if (sessionState.session != null) {
1343 try {
1344 sessionState.session.finishSession();
1345 } catch (RemoteException e) {
1346 Slog.w(TAG, "Session failed to close due to remote exception", e);
1347 setImeWindowVisibilityStatusHiddenLocked();
1348 }
1349 sessionState.session = null;
1350 }
1351 if (sessionState.channel != null) {
1352 sessionState.channel.dispose();
1353 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001354 }
1355 }
1356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001358 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 if (mCurMethod != null) {
1360 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001361 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001363
Jeff Brownc28867a2013-03-26 15:42:39 -07001364 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001365 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 mCurMethod = null;
1367 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001368 if (mStatusBar != null) {
1369 mStatusBar.setIconVisibility("ime", false);
1370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001372
satoke7c6998e2011-06-03 17:57:59 +09001373 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 public void onServiceDisconnected(ComponentName name) {
1375 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001376 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 + " mCurIntent=" + mCurIntent);
1378 if (mCurMethod != null && mCurIntent != null
1379 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001380 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 // We consider this to be a new bind attempt, since the system
1382 // should now try to restart the service for us.
1383 mLastBindTime = SystemClock.uptimeMillis();
1384 mShowRequested = mInputShown;
1385 mInputShown = false;
1386 if (mCurClient != null) {
1387 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1388 MSG_UNBIND_METHOD, mCurSeq, mCurClient.client));
1389 }
1390 }
1391 }
1392 }
1393
satokf9f01002011-05-19 21:31:50 +09001394 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001396 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 long ident = Binder.clearCallingIdentity();
1398 try {
1399 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001400 Slog.w(TAG, "Ignoring setInputMethod of uid " + uid + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 return;
1402 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 synchronized (mMethodMap) {
1405 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001406 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001407 if (mStatusBar != null) {
1408 mStatusBar.setIconVisibility("ime", false);
1409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001411 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001412 CharSequence contentDescription = null;
1413 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001414 // Use PackageManager to load label
1415 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001416 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001417 mIPackageManager.getApplicationInfo(packageName, 0,
1418 mSettings.getCurrentUserId()));
1419 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001420 /* ignore */
1421 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001422 if (mStatusBar != null) {
1423 mStatusBar.setIcon("ime", packageName, iconId, 0,
1424 contentDescription != null
1425 ? contentDescription.toString() : null);
1426 mStatusBar.setIconVisibility("ime", true);
1427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429 }
1430 } finally {
1431 Binder.restoreCallingIdentity(ident);
1432 }
1433 }
1434
satok7cfc0ed2011-06-20 21:29:36 +09001435 private boolean needsToShowImeSwitchOngoingNotification() {
1436 if (!mShowOngoingImeSwitcherForPhones) return false;
satok2c93efc2012-04-02 19:33:47 +09001437 if (isScreenLocked()) return false;
satok7cfc0ed2011-06-20 21:29:36 +09001438 synchronized (mMethodMap) {
1439 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1440 final int N = imis.size();
satokb6359412011-06-28 17:47:41 +09001441 if (N > 2) return true;
1442 if (N < 1) return false;
1443 int nonAuxCount = 0;
1444 int auxCount = 0;
1445 InputMethodSubtype nonAuxSubtype = null;
1446 InputMethodSubtype auxSubtype = null;
satok7cfc0ed2011-06-20 21:29:36 +09001447 for(int i = 0; i < N; ++i) {
1448 final InputMethodInfo imi = imis.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001449 final List<InputMethodSubtype> subtypes =
1450 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satok7cfc0ed2011-06-20 21:29:36 +09001451 final int subtypeCount = subtypes.size();
1452 if (subtypeCount == 0) {
satokb6359412011-06-28 17:47:41 +09001453 ++nonAuxCount;
satok7cfc0ed2011-06-20 21:29:36 +09001454 } else {
1455 for (int j = 0; j < subtypeCount; ++j) {
satokb6359412011-06-28 17:47:41 +09001456 final InputMethodSubtype subtype = subtypes.get(j);
1457 if (!subtype.isAuxiliary()) {
1458 ++nonAuxCount;
1459 nonAuxSubtype = subtype;
1460 } else {
1461 ++auxCount;
1462 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001463 }
1464 }
1465 }
satok7cfc0ed2011-06-20 21:29:36 +09001466 }
satokb6359412011-06-28 17:47:41 +09001467 if (nonAuxCount > 1 || auxCount > 1) {
1468 return true;
1469 } else if (nonAuxCount == 1 && auxCount == 1) {
1470 if (nonAuxSubtype != null && auxSubtype != null
satok9747f892011-09-12 15:56:40 +09001471 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1472 || auxSubtype.overridesImplicitlyEnabledSubtype()
1473 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
satokb6359412011-06-28 17:47:41 +09001474 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1475 return false;
1476 }
1477 return true;
1478 }
1479 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001480 }
satok7cfc0ed2011-06-20 21:29:36 +09001481 }
1482
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001483 // Caution! This method is called in this class. Handle multi-user carefully
satokdbf29502011-08-25 15:28:23 +09001484 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001485 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001486 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001487 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001488 try {
1489 if (token == null || mCurToken != token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001490 int uid = Binder.getCallingUid();
Joe Onorato857fd9b2011-01-27 15:08:35 -08001491 Slog.w(TAG, "Ignoring setImeWindowStatus of uid " + uid + " token: " + token);
satok06487a52010-10-29 11:37:18 +09001492 return;
1493 }
1494
1495 synchronized (mMethodMap) {
Joe Onorato857fd9b2011-01-27 15:08:35 -08001496 mImeWindowVis = vis;
1497 mBackDisposition = backDisposition;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001498 if (mStatusBar != null) {
1499 mStatusBar.setImeWindowStatus(token, vis, backDisposition);
1500 }
jungheang.lee217fd292013-02-26 16:53:22 +09001501 final boolean iconVisibility = ((vis & (InputMethodService.IME_ACTIVE)) != 0)
1502 && (mWindowManagerService.isHardKeyboardAvailable()
1503 || (vis & (InputMethodService.IME_VISIBLE)) != 0);
satok5bc8e732011-07-22 21:07:23 +09001504 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1505 if (imi != null && iconVisibility && needsToShowImeSwitchOngoingNotification()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001506 // Used to load label
satok7cfc0ed2011-06-20 21:29:36 +09001507 final PackageManager pm = mContext.getPackageManager();
satok7cfc0ed2011-06-20 21:29:36 +09001508 final CharSequence title = mRes.getText(
1509 com.android.internal.R.string.select_input_method);
satok5bc8e732011-07-22 21:07:23 +09001510 final CharSequence imiLabel = imi.loadLabel(pm);
1511 final CharSequence summary = mCurrentSubtype != null
1512 ? TextUtils.concat(mCurrentSubtype.getDisplayName(mContext,
1513 imi.getPackageName(), imi.getServiceInfo().applicationInfo),
1514 (TextUtils.isEmpty(imiLabel) ?
Ken Wakasa05dbb652011-08-22 15:22:43 +09001515 "" : " - " + imiLabel))
satok5bc8e732011-07-22 21:07:23 +09001516 : imiLabel;
1517
satok7cfc0ed2011-06-20 21:29:36 +09001518 mImeSwitcherNotification.setLatestEventInfo(
satok5bc8e732011-07-22 21:07:23 +09001519 mContext, title, summary, mImeSwitchPendingIntent);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001520 if (mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001521 if (DEBUG) {
1522 Slog.d(TAG, "--- show notification: label = " + imiLabel
1523 + ", summary = " + summary);
1524 }
1525 mNotificationManager.notifyAsUser(null,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001526 com.android.internal.R.string.select_input_method,
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001527 mImeSwitcherNotification, UserHandle.ALL);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001528 mNotificationShown = true;
1529 }
satok7cfc0ed2011-06-20 21:29:36 +09001530 } else {
Dianne Hackborn661cd522011-08-22 00:26:20 -07001531 if (mNotificationShown && mNotificationManager != null) {
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001532 if (DEBUG) {
1533 Slog.d(TAG, "--- hide notification");
1534 }
1535 mNotificationManager.cancelAsUser(null,
1536 com.android.internal.R.string.select_input_method, UserHandle.ALL);
satok7cfc0ed2011-06-20 21:29:36 +09001537 mNotificationShown = false;
1538 }
1539 }
satok06487a52010-10-29 11:37:18 +09001540 }
1541 } finally {
1542 Binder.restoreCallingIdentity(ident);
1543 }
1544 }
1545
satoke7c6998e2011-06-03 17:57:59 +09001546 @Override
satokf9f01002011-05-19 21:31:50 +09001547 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001548 if (!calledFromValidUser()) {
1549 return;
1550 }
satokf9f01002011-05-19 21:31:50 +09001551 synchronized (mMethodMap) {
1552 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1553 for (int i = 0; i < spans.length; ++i) {
1554 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001555 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001556 mSecureSuggestionSpans.put(ss, currentImi);
1557 }
1558 }
1559 }
1560 }
1561
satoke7c6998e2011-06-03 17:57:59 +09001562 @Override
satokf9f01002011-05-19 21:31:50 +09001563 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001564 if (!calledFromValidUser()) {
1565 return false;
1566 }
satokf9f01002011-05-19 21:31:50 +09001567 synchronized (mMethodMap) {
1568 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1569 // TODO: Do not send the intent if the process of the targetImi is already dead.
1570 if (targetImi != null) {
1571 final String[] suggestions = span.getSuggestions();
1572 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001573 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001574 final Intent intent = new Intent();
1575 // Ensures that only a class in the original IME package will receive the
1576 // notification.
satok42c5a162011-05-26 16:46:14 +09001577 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001578 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1579 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1580 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1581 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001582 final long ident = Binder.clearCallingIdentity();
1583 try {
1584 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1585 } finally {
1586 Binder.restoreCallingIdentity(ident);
1587 }
satokf9f01002011-05-19 21:31:50 +09001588 return true;
1589 }
1590 }
1591 return false;
1592 }
1593
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001594 void updateFromSettingsLocked(boolean enabledMayChange) {
1595 if (enabledMayChange) {
1596 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1597 for (int i=0; i<enabled.size(); i++) {
1598 // We allow the user to select "disabled until used" apps, so if they
1599 // are enabling one of those here we now need to make it enabled.
1600 InputMethodInfo imm = enabled.get(i);
1601 try {
1602 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1603 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1604 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001605 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001606 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
1607 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1608 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001609 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1610 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001611 }
1612 } catch (RemoteException e) {
1613 }
1614 }
1615 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001616 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1617 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1618 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1619 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001620 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001621 // There is no input method selected, try to choose new applicable input method.
1622 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001623 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001624 }
1625 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09001627 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001629 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
The Android Open Source Project10592532009-03-18 17:39:46 -07001630 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001631 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
satokf3db1af2010-11-23 13:34:33 +09001633 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001634 } else {
1635 // There is no longer an input method set, so stop any current one.
The Android Open Source Project10592532009-03-18 17:39:46 -07001636 mCurMethodId = null;
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001637 unbindCurrentMethodLocked(true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
1639 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001640
satokab751aa2010-09-14 19:17:36 +09001641 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 InputMethodInfo info = mMethodMap.get(id);
1643 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001644 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
satokd81e9502012-05-21 12:58:45 +09001647 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001649 final int subtypeCount = info.getSubtypeCount();
1650 if (subtypeCount <= 0) {
1651 return;
satokcd7cd292010-11-20 15:46:23 +09001652 }
satokd81e9502012-05-21 12:58:45 +09001653 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1654 final InputMethodSubtype newSubtype;
1655 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1656 newSubtype = info.getSubtypeAt(subtypeId);
1657 } else {
1658 // If subtype is null, try to find the most applicable one from
1659 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001660 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001661 }
1662 if (newSubtype == null || oldSubtype == null) {
1663 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1664 + ", new subtype = " + newSubtype);
1665 return;
1666 }
1667 if (newSubtype != oldSubtype) {
1668 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1669 if (mCurMethod != null) {
1670 try {
1671 refreshImeWindowVisibilityLocked();
1672 mCurMethod.changeInputMethodSubtype(newSubtype);
1673 } catch (RemoteException e) {
1674 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09001675 }
1676 }
1677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 return;
1679 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001680
satokd81e9502012-05-21 12:58:45 +09001681 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 final long ident = Binder.clearCallingIdentity();
1683 try {
satokab751aa2010-09-14 19:17:36 +09001684 // Set a subtype to this input method.
1685 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09001686 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
1687 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
1688 // because mCurMethodId is stored as a history in
1689 // setSelectedInputMethodAndSubtypeLocked().
1690 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691
1692 if (ActivityManagerNative.isSystemReady()) {
1693 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08001694 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07001696 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001698 unbindCurrentClientLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 } finally {
1700 Binder.restoreCallingIdentity(ident);
1701 }
1702 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703
satok42c5a162011-05-26 16:46:14 +09001704 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001705 public boolean showSoftInput(IInputMethodClient client, int flags,
1706 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001707 if (!calledFromValidUser()) {
1708 return false;
1709 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001710 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 long ident = Binder.clearCallingIdentity();
1712 try {
1713 synchronized (mMethodMap) {
1714 if (mCurClient == null || client == null
1715 || mCurClient.client.asBinder() != client.asBinder()) {
1716 try {
1717 // We need to check if this is the current client with
1718 // focus in the window manager, to allow this call to
1719 // be made before input is started in it.
1720 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001721 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001722 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001725 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001728
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001729 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001730 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732 } finally {
1733 Binder.restoreCallingIdentity(ident);
1734 }
1735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001736
The Android Open Source Project4df24232009-03-05 14:34:35 -08001737 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 mShowRequested = true;
1739 if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
1740 mShowExplicitlyRequested = true;
1741 }
1742 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
1743 mShowExplicitlyRequested = true;
1744 mShowForced = true;
1745 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001746
Dianne Hackborncc278702009-09-02 23:07:23 -07001747 if (!mSystemReady) {
1748 return false;
1749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001750
The Android Open Source Project4df24232009-03-05 14:34:35 -08001751 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001753 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08001754 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
1755 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
1756 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001758 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001759 bindCurrentInputMethodService(
1760 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001761 mVisibleBound = true;
1762 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001763 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09001765 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 // The client has asked to have the input method shown, but
1767 // we have been sitting here too long with a connection to the
1768 // service and no interface received, so let's disconnect/connect
1769 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001770 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09001772 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001774 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001775 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001776 } else {
1777 if (DEBUG) {
1778 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
1779 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
1780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001782
The Android Open Source Project4df24232009-03-05 14:34:35 -08001783 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001785
satok42c5a162011-05-26 16:46:14 +09001786 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08001787 public boolean hideSoftInput(IInputMethodClient client, int flags,
1788 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001789 if (!calledFromValidUser()) {
1790 return false;
1791 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001792 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 long ident = Binder.clearCallingIdentity();
1794 try {
1795 synchronized (mMethodMap) {
1796 if (mCurClient == null || client == null
1797 || mCurClient.client.asBinder() != client.asBinder()) {
1798 try {
1799 // We need to check if this is the current client with
1800 // focus in the window manager, to allow this call to
1801 // be made before input is started in it.
1802 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001803 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
1804 + uid + ": " + client);
satok15452a42011-10-28 17:58:28 +09001805 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001806 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808 } catch (RemoteException e) {
satok15452a42011-10-28 17:58:28 +09001809 setImeWindowVisibilityStatusHiddenLocked();
The Android Open Source Project4df24232009-03-05 14:34:35 -08001810 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001813
Joe Onorato8a9b2202010-02-26 18:56:32 -08001814 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001815 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817 } finally {
1818 Binder.restoreCallingIdentity(ident);
1819 }
1820 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001821
The Android Open Source Project4df24232009-03-05 14:34:35 -08001822 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
1824 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001825 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 -08001826 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
1828 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001829 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 -08001830 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001832 boolean res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 if (mInputShown && mCurMethod != null) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08001834 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1835 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
1836 res = true;
1837 } else {
1838 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001840 if (mHaveConnection && mVisibleBound) {
1841 mContext.unbindService(mVisibleConnection);
1842 mVisibleBound = false;
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 mInputShown = false;
1845 mShowRequested = false;
1846 mShowExplicitlyRequested = false;
1847 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08001848 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001850
satok42c5a162011-05-26 16:46:14 +09001851 @Override
Dianne Hackborn7663d802012-02-24 13:08:49 -08001852 public InputBindResult windowGainedFocus(IInputMethodClient client, IBinder windowToken,
1853 int controlFlags, int softInputMode, int windowFlags,
1854 EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001855 // Needs to check the validity before clearing calling identity
1856 final boolean calledFromValidUser = calledFromValidUser();
1857
Dianne Hackborn7663d802012-02-24 13:08:49 -08001858 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 long ident = Binder.clearCallingIdentity();
1860 try {
1861 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001862 if (DEBUG) Slog.v(TAG, "windowGainedFocus: " + client.asBinder()
Dianne Hackborn7663d802012-02-24 13:08:49 -08001863 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08001865 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001866
Dianne Hackborn7663d802012-02-24 13:08:49 -08001867 ClientState cs = mClients.get(client.asBinder());
1868 if (cs == null) {
1869 throw new IllegalArgumentException("unknown client "
1870 + client.asBinder());
1871 }
1872
1873 try {
1874 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1875 // Check with the window manager to make sure this client actually
1876 // has a window with focus. If not, reject. This is thread safe
1877 // because if the focus changes some time before or after, the
1878 // next client receiving focus that has any interest in input will
1879 // be calling through here after that change happens.
1880 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
1881 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1882 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001884 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001886
Satoshi Kataoka8d033052012-11-19 17:30:40 +09001887 if (!calledFromValidUser) {
1888 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
1889 Slog.w(TAG, "If you want to interect with IME, you need "
1890 + "android.permission.INTERACT_ACROSS_USERS_FULL");
1891 hideCurrentInputLocked(0, null);
1892 return null;
1893 }
1894
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001895 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07001896 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09001897 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001898 if (attribute != null) {
1899 return startInputUncheckedLocked(cs, inputContext, attribute,
1900 controlFlags);
1901 }
1902 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001903 }
1904 mCurFocusedWindow = windowToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001905
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001906 // Should we auto-show the IME even if the caller has not
1907 // specified what should be done with it?
1908 // We only do this automatically if the window can resize
1909 // to accommodate the IME (so what the user sees will give
1910 // them good context without input information being obscured
1911 // by the IME) or if running on a large screen where there
1912 // is more room for the target window + IME.
1913 final boolean doAutoShow =
1914 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
1915 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
1916 || mRes.getConfiguration().isLayoutSizeAtLeast(
1917 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001918 final boolean isTextEditor =
1919 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
1920
1921 // We want to start input before showing the IME, but after closing
1922 // it. We want to do this after closing it to help the IME disappear
1923 // more quickly (not get stuck behind it initializing itself for the
1924 // new focused input, even if its window wants to hide the IME).
1925 boolean didStart = false;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
1928 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001929 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
1931 // There is no focus view, and this window will
1932 // be behind any soft input window, so hide the
1933 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001934 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001935 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001937 } else if (isTextEditor && doAutoShow && (softInputMode &
1938 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 // There is a focus view, and we are navigating forward
1940 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001941 // We only do this automatically if the window can resize
1942 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001943 // them good context without input information being obscured
1944 // by the IME) or if running on a large screen where there
1945 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001946 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001947 if (attribute != null) {
1948 res = startInputUncheckedLocked(cs, inputContext, attribute,
1949 controlFlags);
1950 didStart = true;
1951 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001952 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 break;
1955 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
1956 // Do nothing.
1957 break;
1958 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
1959 if ((softInputMode &
1960 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001961 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001962 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
1964 break;
1965 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001966 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001967 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 break;
1969 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
1970 if ((softInputMode &
1971 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001972 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001973 if (attribute != null) {
1974 res = startInputUncheckedLocked(cs, inputContext, attribute,
1975 controlFlags);
1976 didStart = true;
1977 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001978 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980 break;
1981 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08001982 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08001983 if (attribute != null) {
1984 res = startInputUncheckedLocked(cs, inputContext, attribute,
1985 controlFlags);
1986 didStart = true;
1987 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001988 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 break;
1990 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08001991
1992 if (!didStart && attribute != null) {
1993 res = startInputUncheckedLocked(cs, inputContext, attribute,
1994 controlFlags);
1995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997 } finally {
1998 Binder.restoreCallingIdentity(ident);
1999 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002000
2001 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002003
satok42c5a162011-05-26 16:46:14 +09002004 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 public void showInputMethodPickerFromClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002006 if (!calledFromValidUser()) {
2007 return;
2008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 synchronized (mMethodMap) {
2010 if (mCurClient == null || client == null
2011 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002012 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002013 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
2015
satok440aab52010-11-25 09:43:11 +09002016 // Always call subtype picker, because subtype picker is a superset of input method
2017 // picker.
satokab751aa2010-09-14 19:17:36 +09002018 mHandler.sendEmptyMessage(MSG_SHOW_IM_SUBTYPE_PICKER);
2019 }
2020 }
2021
satok42c5a162011-05-26 16:46:14 +09002022 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002024 if (!calledFromValidUser()) {
2025 return;
2026 }
satok28203512010-11-24 11:06:49 +09002027 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2028 }
2029
satok42c5a162011-05-26 16:46:14 +09002030 @Override
satok28203512010-11-24 11:06:49 +09002031 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002032 if (!calledFromValidUser()) {
2033 return;
2034 }
satok28203512010-11-24 11:06:49 +09002035 synchronized (mMethodMap) {
2036 if (subtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002037 setInputMethodWithSubtypeId(token, id, InputMethodUtils.getSubtypeIdFromHashCode(
satok28203512010-11-24 11:06:49 +09002038 mMethodMap.get(id), subtype.hashCode()));
2039 } else {
2040 setInputMethod(token, id);
2041 }
2042 }
satokab751aa2010-09-14 19:17:36 +09002043 }
2044
satok42c5a162011-05-26 16:46:14 +09002045 @Override
satokb416a71e2010-11-25 20:42:14 +09002046 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002047 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002048 if (!calledFromValidUser()) {
2049 return;
2050 }
satokb416a71e2010-11-25 20:42:14 +09002051 synchronized (mMethodMap) {
2052 if (mCurClient == null || client == null
2053 || mCurClient.client.asBinder() != client.asBinder()) {
2054 Slog.w(TAG, "Ignoring showInputMethodAndSubtypeEnablerFromClient of: " + client);
2055 }
satok7fee71f2010-12-17 18:54:26 +09002056 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2057 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a71e2010-11-25 20:42:14 +09002058 }
2059 }
2060
satok4fc87d62011-05-20 16:13:43 +09002061 @Override
satok735cf382010-11-11 20:40:09 +09002062 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002063 if (!calledFromValidUser()) {
2064 return false;
2065 }
satok735cf382010-11-11 20:40:09 +09002066 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002067 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002068 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002069 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002070 lastImi = mMethodMap.get(lastIme.first);
2071 } else {
2072 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002073 }
satok4fc87d62011-05-20 16:13:43 +09002074 String targetLastImiId = null;
2075 int subtypeId = NOT_A_SUBTYPE_ID;
2076 if (lastIme != null && lastImi != null) {
2077 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2078 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2079 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2080 : mCurrentSubtype.hashCode();
2081 // If the last IME is the same as the current IME and the last subtype is not
2082 // defined, there is no need to switch to the last IME.
2083 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2084 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002085 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002086 }
2087 }
2088
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002089 if (TextUtils.isEmpty(targetLastImiId)
2090 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002091 // This is a safety net. If the currentSubtype can't be added to the history
2092 // and the framework couldn't find the last ime, we will make the last ime be
2093 // the most applicable enabled keyboard subtype of the system imes.
2094 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2095 if (enabled != null) {
2096 final int N = enabled.size();
2097 final String locale = mCurrentSubtype == null
2098 ? mRes.getConfiguration().locale.toString()
2099 : mCurrentSubtype.getLocale();
2100 for (int i = 0; i < N; ++i) {
2101 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002102 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002103 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002104 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2105 InputMethodUtils.getSubtypes(imi),
2106 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002107 if (keyboardSubtype != null) {
2108 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002109 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002110 imi, keyboardSubtype.hashCode());
2111 if(keyboardSubtype.getLocale().equals(locale)) {
2112 break;
2113 }
2114 }
2115 }
2116 }
2117 }
2118 }
2119
2120 if (!TextUtils.isEmpty(targetLastImiId)) {
2121 if (DEBUG) {
2122 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2123 + ", from: " + mCurMethodId + ", " + subtypeId);
2124 }
2125 setInputMethodWithSubtypeId(token, targetLastImiId, subtypeId);
2126 return true;
2127 } else {
2128 return false;
2129 }
satok735cf382010-11-11 20:40:09 +09002130 }
2131 }
2132
satoke7c6998e2011-06-03 17:57:59 +09002133 @Override
satok688bd472012-02-09 20:09:17 +09002134 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002135 if (!calledFromValidUser()) {
2136 return false;
2137 }
satok688bd472012-02-09 20:09:17 +09002138 synchronized (mMethodMap) {
2139 final ImeSubtypeListItem nextSubtype = mImListManager.getNextInputMethod(
2140 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2141 if (nextSubtype == null) {
2142 return false;
2143 }
2144 setInputMethodWithSubtypeId(token, nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2145 return true;
2146 }
2147 }
2148
2149 @Override
satok68f1b782011-04-11 14:26:04 +09002150 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002151 if (!calledFromValidUser()) {
2152 return null;
2153 }
satok68f1b782011-04-11 14:26:04 +09002154 synchronized (mMethodMap) {
2155 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2156 // TODO: Handle the case of the last IME with no subtypes
2157 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2158 || TextUtils.isEmpty(lastIme.second)) return null;
2159 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2160 if (lastImi == null) return null;
2161 try {
2162 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002163 final int lastSubtypeId =
2164 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002165 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2166 return null;
2167 }
2168 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002169 } catch (NumberFormatException e) {
2170 return null;
2171 }
2172 }
2173 }
2174
satoke7c6998e2011-06-03 17:57:59 +09002175 @Override
satokee5e77c2011-09-02 18:50:15 +09002176 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002177 if (!calledFromValidUser()) {
2178 return;
2179 }
satok91e88122011-07-18 11:11:42 +09002180 // By this IPC call, only a process which shares the same uid with the IME can add
2181 // additional input method subtypes to the IME.
satokee5e77c2011-09-02 18:50:15 +09002182 if (TextUtils.isEmpty(imiId) || subtypes == null || subtypes.length == 0) return;
satoke7c6998e2011-06-03 17:57:59 +09002183 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002184 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002185 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002186 final String[] packageInfos;
2187 try {
2188 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2189 } catch (RemoteException e) {
2190 Slog.e(TAG, "Failed to get package infos");
2191 return;
2192 }
satok91e88122011-07-18 11:11:42 +09002193 if (packageInfos != null) {
2194 final int packageNum = packageInfos.length;
2195 for (int i = 0; i < packageNum; ++i) {
2196 if (packageInfos[i].equals(imi.getPackageName())) {
2197 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002198 final long ident = Binder.clearCallingIdentity();
2199 try {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002200 buildInputMethodListLocked(mMethodList, mMethodMap,
2201 false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002202 } finally {
2203 Binder.restoreCallingIdentity(ident);
2204 }
satokee5e77c2011-09-02 18:50:15 +09002205 return;
satok91e88122011-07-18 11:11:42 +09002206 }
2207 }
2208 }
satoke7c6998e2011-06-03 17:57:59 +09002209 }
satokee5e77c2011-09-02 18:50:15 +09002210 return;
satoke7c6998e2011-06-03 17:57:59 +09002211 }
2212
satok28203512010-11-24 11:06:49 +09002213 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 synchronized (mMethodMap) {
2215 if (token == null) {
2216 if (mContext.checkCallingOrSelfPermission(
2217 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2218 != PackageManager.PERMISSION_GRANTED) {
2219 throw new SecurityException(
2220 "Using null token requires permission "
2221 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2222 }
2223 } else if (mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002224 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2225 + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 return;
2227 }
2228
satokc5933802011-08-31 21:26:04 +09002229 final long ident = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 try {
satokab751aa2010-09-14 19:17:36 +09002231 setInputMethodLocked(id, subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 } finally {
2233 Binder.restoreCallingIdentity(ident);
2234 }
2235 }
2236 }
2237
satok42c5a162011-05-26 16:46:14 +09002238 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002240 if (!calledFromValidUser()) {
2241 return;
2242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 synchronized (mMethodMap) {
2244 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002245 if (DEBUG) Slog.w(TAG, "Ignoring hideInputMethod of uid "
2246 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 return;
2248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 long ident = Binder.clearCallingIdentity();
2250 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002251 hideCurrentInputLocked(flags, null);
2252 } finally {
2253 Binder.restoreCallingIdentity(ident);
2254 }
2255 }
2256 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002257
satok42c5a162011-05-26 16:46:14 +09002258 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002259 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002260 if (!calledFromValidUser()) {
2261 return;
2262 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002263 synchronized (mMethodMap) {
2264 if (token == null || mCurToken != token) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002265 Slog.w(TAG, "Ignoring showMySoftInput of uid "
2266 + Binder.getCallingUid() + " token: " + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002267 return;
2268 }
2269 long ident = Binder.clearCallingIdentity();
2270 try {
2271 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272 } finally {
2273 Binder.restoreCallingIdentity(ident);
2274 }
2275 }
2276 }
2277
2278 void setEnabledSessionInMainThread(SessionState session) {
2279 if (mEnabledSession != session) {
2280 if (mEnabledSession != null) {
2281 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002282 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 mEnabledSession.method.setSessionEnabled(
2284 mEnabledSession.session, false);
2285 } catch (RemoteException e) {
2286 }
2287 }
2288 mEnabledSession = session;
2289 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002290 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 session.method.setSessionEnabled(
2292 session.session, true);
2293 } catch (RemoteException e) {
2294 }
2295 }
2296 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002297
satok42c5a162011-05-26 16:46:14 +09002298 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002300 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 switch (msg.what) {
2302 case MSG_SHOW_IM_PICKER:
2303 showInputMethodMenu();
2304 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002305
satokab751aa2010-09-14 19:17:36 +09002306 case MSG_SHOW_IM_SUBTYPE_PICKER:
2307 showInputMethodSubtypeMenu();
2308 return true;
2309
satok47a44912010-10-06 16:03:58 +09002310 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002311 args = (SomeArgs)msg.obj;
satok7fee71f2010-12-17 18:54:26 +09002312 showInputMethodAndSubtypeEnabler((String)args.arg1);
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002313 args.recycle();
satok217f5482010-12-15 05:19:19 +09002314 return true;
2315
2316 case MSG_SHOW_IM_CONFIG:
2317 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002318 return true;
2319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 case MSG_UNBIND_INPUT:
2323 try {
2324 ((IInputMethod)msg.obj).unbindInput();
2325 } catch (RemoteException e) {
2326 // There is nothing interesting about the method dying.
2327 }
2328 return true;
2329 case MSG_BIND_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 {
2332 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2333 } catch (RemoteException e) {
2334 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002335 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 return true;
2337 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002338 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002340 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002341 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002342 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 } catch (RemoteException e) {
2344 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002345 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 return true;
2347 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002348 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002350 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002351 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002352 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 } catch (RemoteException e) {
2354 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002355 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 return true;
2357 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002358 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002360 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2362 } catch (RemoteException e) {
2363 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002364 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002366 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002367 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002368 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002369 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002371 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002373 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002374 // Dispose the channel if the input method is not local to this process
2375 // because the remote proxy will get its own copy when unparceled.
2376 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002377 channel.dispose();
2378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002380 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002386 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 try {
2388 SessionState session = (SessionState)args.arg1;
2389 setEnabledSessionInMainThread(session);
2390 session.method.startInput((IInputContext)args.arg2,
2391 (EditorInfo)args.arg3);
2392 } catch (RemoteException e) {
2393 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002394 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 return true;
2396 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002397 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 try {
2399 SessionState session = (SessionState)args.arg1;
2400 setEnabledSessionInMainThread(session);
2401 session.method.restartInput((IInputContext)args.arg2,
2402 (EditorInfo)args.arg3);
2403 } catch (RemoteException e) {
2404 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002405 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 case MSG_UNBIND_METHOD:
2411 try {
2412 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1);
2413 } catch (RemoteException e) {
2414 // There is nothing interesting about the last client dying.
2415 }
2416 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002417 case MSG_BIND_METHOD: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002418 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002419 IInputMethodClient client = (IInputMethodClient)args.arg1;
2420 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002422 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002424 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002425 } finally {
2426 // Dispose the channel if the input method is not local to this process
2427 // because the remote proxy will get its own copy when unparceled.
2428 if (res.channel != null && Binder.isProxy(client)) {
2429 res.channel.dispose();
2430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002432 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002434 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002435 case MSG_SET_ACTIVE:
2436 try {
2437 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2438 } catch (RemoteException e) {
2439 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2440 + ((ClientState)msg.obj).pid + " uid "
2441 + ((ClientState)msg.obj).uid);
2442 }
2443 return true;
satok01038492012-04-09 21:08:27 +09002444
2445 // --------------------------------------------------------------
2446 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
2447 mHardKeyboardListener.handleHardKeyboardStatusChange(
2448 msg.arg1 == 1, msg.arg2 == 1);
2449 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451 return false;
2452 }
2453
satokdc9ddae2011-10-06 12:22:36 +09002454 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002455 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2456 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002457 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002458 if (DEBUG) {
2459 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2460 }
satok723a27e2010-11-11 14:58:11 +09002461 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002462 return true;
2463 }
2464
2465 return false;
2466 }
2467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 void buildInputMethodListLocked(ArrayList<InputMethodInfo> list,
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002469 HashMap<String, InputMethodInfo> map, boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002470 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002471 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
2472 + " \n ------ \n" + getStackTrace());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 list.clear();
2475 map.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002476
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002477 // Use for queryIntentServicesAsUser
2478 final PackageManager pm = mContext.getPackageManager();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002479 String disabledSysImes = mSettings.getDisabledSystemInputMethods();
Amith Yamasanie861ec12010-03-24 21:39:27 -07002480 if (disabledSysImes == null) disabledSysImes = "";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002482 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002484 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2485 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002486
satoke7c6998e2011-06-03 17:57:59 +09002487 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2488 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 for (int i = 0; i < services.size(); ++i) {
2490 ResolveInfo ri = services.get(i);
2491 ServiceInfo si = ri.serviceInfo;
2492 ComponentName compName = new ComponentName(si.packageName, si.name);
2493 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2494 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002495 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 + ": it does not require the permission "
2497 + android.Manifest.permission.BIND_INPUT_METHOD);
2498 continue;
2499 }
2500
Joe Onorato8a9b2202010-02-26 18:56:32 -08002501 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
2503 try {
satoke7c6998e2011-06-03 17:57:59 +09002504 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 list.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002506 final String id = p.getId();
2507 map.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508
2509 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002510 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002514 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002516 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002519
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002520 if (resetDefaultEnabledIme) {
2521 final ArrayList<InputMethodInfo> defaultEnabledIme =
2522 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, list);
2523 for (int i = 0; i < defaultEnabledIme.size(); ++i) {
2524 final InputMethodInfo imi = defaultEnabledIme.get(i);
2525 if (DEBUG) {
2526 Slog.d(TAG, "--- enable ime = " + imi);
2527 }
2528 setInputMethodEnabledLocked(imi.getId(), true);
2529 }
2530 }
2531
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002532 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09002533 if (!TextUtils.isEmpty(defaultImiId)) {
2534 if (!map.containsKey(defaultImiId)) {
2535 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
2536 if (chooseNewDefaultIMELocked()) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002537 updateFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09002538 }
2539 } else {
2540 // Double check that the default IME is certainly enabled.
2541 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002542 }
2543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002547
satokab751aa2010-09-14 19:17:36 +09002548 private void showInputMethodMenu() {
2549 showInputMethodMenuInternal(false);
2550 }
2551
2552 private void showInputMethodSubtypeMenu() {
2553 showInputMethodMenuInternal(true);
2554 }
2555
satok217f5482010-12-15 05:19:19 +09002556 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09002557 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09002558 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09002559 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2560 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09002561 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09002562 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09002563 }
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002564 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok217f5482010-12-15 05:19:19 +09002565 }
2566
2567 private void showConfigureInputMethods() {
2568 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
2569 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
2570 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
2571 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09002572 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09002573 }
2574
satok2c93efc2012-04-02 19:33:47 +09002575 private boolean isScreenLocked() {
2576 return mKeyguardManager != null
2577 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
2578 }
satokab751aa2010-09-14 19:17:36 +09002579 private void showInputMethodMenuInternal(boolean showSubtypes) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002580 if (DEBUG) Slog.v(TAG, "Show switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09002583 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002584
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002585 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002586 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002587 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002588
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002589 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09002590 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
2591 getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
satok7f35c8c2010-10-07 21:13:11 +09002592 if (immis == null || immis.size() == 0) {
2593 return;
2594 }
2595
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002596 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597
satok688bd472012-02-09 20:09:17 +09002598 final List<ImeSubtypeListItem> imList =
2599 mImListManager.getSortedInputMethodAndSubtypeList(
2600 showSubtypes, mInputShown, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09002601
satokc3690562012-01-10 20:14:43 +09002602 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002603 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09002604 if (currentSubtype != null) {
2605 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002606 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
2607 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09002608 }
2609 }
2610
Ken Wakasa761eb372011-03-04 19:06:18 +09002611 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09002612 mIms = new InputMethodInfo[N];
2613 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002614 int checkedItem = 0;
2615 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002616 final ImeSubtypeListItem item = imList.get(i);
2617 mIms[i] = item.mImi;
2618 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002619 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09002620 int subtypeId = mSubtypeIds[i];
2621 if ((subtypeId == NOT_A_SUBTYPE_ID)
2622 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
2623 || (subtypeId == lastInputMethodSubtypeId)) {
2624 checkedItem = i;
2625 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 }
Ken Wakasa05dbb652011-08-22 15:22:43 +09002628 final TypedArray a = context.obtainStyledAttributes(null,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002629 com.android.internal.R.styleable.DialogPreference,
2630 com.android.internal.R.attr.alertDialogStyle, 0);
2631 mDialogBuilder = new AlertDialog.Builder(context)
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002632 .setOnCancelListener(new OnCancelListener() {
satok42c5a162011-05-26 16:46:14 +09002633 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002634 public void onCancel(DialogInterface dialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 hideInputMethodMenu();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002636 }
2637 })
2638 .setIcon(a.getDrawable(
2639 com.android.internal.R.styleable.DialogPreference_dialogTitle));
2640 a.recycle();
satok01038492012-04-09 21:08:27 +09002641 final LayoutInflater inflater =
2642 (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2643 final View tv = inflater.inflate(
2644 com.android.internal.R.layout.input_method_switch_dialog_title, null);
2645 mDialogBuilder.setCustomTitle(tv);
2646
2647 // Setup layout for a toggle switch of the hardware keyboard
2648 mSwitchingDialogTitleView = tv;
2649 mSwitchingDialogTitleView.findViewById(
2650 com.android.internal.R.id.hard_keyboard_section).setVisibility(
2651 mWindowManagerService.isHardKeyboardAvailable() ?
2652 View.VISIBLE : View.GONE);
2653 final Switch hardKeySwitch = ((Switch)mSwitchingDialogTitleView.findViewById(
2654 com.android.internal.R.id.hard_keyboard_switch));
2655 hardKeySwitch.setChecked(mWindowManagerService.isHardKeyboardEnabled());
2656 hardKeySwitch.setOnCheckedChangeListener(
2657 new OnCheckedChangeListener() {
2658 @Override
2659 public void onCheckedChanged(
2660 CompoundButton buttonView, boolean isChecked) {
2661 mWindowManagerService.setHardKeyboardEnabled(isChecked);
Satoshi Kataoka04dd24d2013-01-18 13:44:37 +09002662 // Ensure that the input method dialog is dismissed when changing
2663 // the hardware keyboard state.
2664 hideInputMethodMenu();
satok01038492012-04-09 21:08:27 +09002665 }
2666 });
satokd87c2592010-09-29 11:52:06 +09002667
Ken Wakasa05dbb652011-08-22 15:22:43 +09002668 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(context,
2669 com.android.internal.R.layout.simple_list_item_2_single_choice, imList,
2670 checkedItem);
2671
2672 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem,
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002673 new AlertDialog.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002674 @Override
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002675 public void onClick(DialogInterface dialog, int which) {
2676 synchronized (mMethodMap) {
satokab751aa2010-09-14 19:17:36 +09002677 if (mIms == null || mIms.length <= which
2678 || mSubtypeIds == null || mSubtypeIds.length <= which) {
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002679 return;
2680 }
2681 InputMethodInfo im = mIms[which];
satokab751aa2010-09-14 19:17:36 +09002682 int subtypeId = mSubtypeIds[which];
Satoshi Kataokad2142962012-11-12 18:43:06 +09002683 adapter.mCheckedItem = which;
2684 adapter.notifyDataSetChanged();
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002685 hideInputMethodMenu();
2686 if (im != null) {
satokab751aa2010-09-14 19:17:36 +09002687 if ((subtypeId < 0)
Ken Wakasa586f0512011-01-20 22:31:01 +09002688 || (subtypeId >= im.getSubtypeCount())) {
satokab751aa2010-09-14 19:17:36 +09002689 subtypeId = NOT_A_SUBTYPE_ID;
2690 }
2691 setInputMethodLocked(im.getId(), subtypeId);
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002692 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08002693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07002695 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696
satokbc81b692011-08-26 16:22:22 +09002697 if (showSubtypes && !isScreenLocked) {
satok82beadf2010-12-27 19:03:06 +09002698 mDialogBuilder.setPositiveButton(
2699 com.android.internal.R.string.configure_input_methods,
satok7f35c8c2010-10-07 21:13:11 +09002700 new DialogInterface.OnClickListener() {
satok42c5a162011-05-26 16:46:14 +09002701 @Override
satok7f35c8c2010-10-07 21:13:11 +09002702 public void onClick(DialogInterface dialog, int whichButton) {
satok217f5482010-12-15 05:19:19 +09002703 showConfigureInputMethods();
satok7f35c8c2010-10-07 21:13:11 +09002704 }
2705 });
2706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002708 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 mSwitchingDialog.getWindow().setType(
2710 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09002711 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
2712 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08002713 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 mSwitchingDialog.show();
2715 }
2716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002717
satok93d744d2012-05-09 17:14:08 +09002718 private static class ImeSubtypeListItem implements Comparable<ImeSubtypeListItem> {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002719 public final CharSequence mImeName;
2720 public final CharSequence mSubtypeName;
2721 public final InputMethodInfo mImi;
2722 public final int mSubtypeId;
satok93d744d2012-05-09 17:14:08 +09002723 private final boolean mIsSystemLocale;
2724 private final boolean mIsSystemLanguage;
2725
Ken Wakasa05dbb652011-08-22 15:22:43 +09002726 public ImeSubtypeListItem(CharSequence imeName, CharSequence subtypeName,
satok93d744d2012-05-09 17:14:08 +09002727 InputMethodInfo imi, int subtypeId, String subtypeLocale, String systemLocale) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09002728 mImeName = imeName;
2729 mSubtypeName = subtypeName;
2730 mImi = imi;
2731 mSubtypeId = subtypeId;
satok93d744d2012-05-09 17:14:08 +09002732 if (TextUtils.isEmpty(subtypeLocale)) {
2733 mIsSystemLocale = false;
2734 mIsSystemLanguage = false;
2735 } else {
2736 mIsSystemLocale = subtypeLocale.equals(systemLocale);
2737 mIsSystemLanguage = mIsSystemLocale
2738 || subtypeLocale.startsWith(systemLocale.substring(0, 2));
2739 }
2740 }
2741
2742 @Override
2743 public int compareTo(ImeSubtypeListItem other) {
2744 if (TextUtils.isEmpty(mImeName)) {
2745 return 1;
2746 }
2747 if (TextUtils.isEmpty(other.mImeName)) {
2748 return -1;
2749 }
2750 if (!TextUtils.equals(mImeName, other.mImeName)) {
2751 return mImeName.toString().compareTo(other.mImeName.toString());
2752 }
2753 if (TextUtils.equals(mSubtypeName, other.mSubtypeName)) {
2754 return 0;
2755 }
2756 if (mIsSystemLocale) {
2757 return -1;
2758 }
2759 if (other.mIsSystemLocale) {
2760 return 1;
2761 }
2762 if (mIsSystemLanguage) {
2763 return -1;
2764 }
2765 if (other.mIsSystemLanguage) {
2766 return 1;
2767 }
2768 if (TextUtils.isEmpty(mSubtypeName)) {
2769 return 1;
2770 }
2771 if (TextUtils.isEmpty(other.mSubtypeName)) {
2772 return -1;
2773 }
2774 return mSubtypeName.toString().compareTo(other.mSubtypeName.toString());
Ken Wakasa05dbb652011-08-22 15:22:43 +09002775 }
2776 }
2777
2778 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
2779 private final LayoutInflater mInflater;
2780 private final int mTextViewResourceId;
2781 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09002782 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09002783 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
2784 List<ImeSubtypeListItem> itemsList, int checkedItem) {
2785 super(context, textViewResourceId, itemsList);
2786 mTextViewResourceId = textViewResourceId;
2787 mItemsList = itemsList;
2788 mCheckedItem = checkedItem;
2789 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2790 }
2791
2792 @Override
2793 public View getView(int position, View convertView, ViewGroup parent) {
2794 final View view = convertView != null ? convertView
2795 : mInflater.inflate(mTextViewResourceId, null);
2796 if (position < 0 || position >= mItemsList.size()) return view;
2797 final ImeSubtypeListItem item = mItemsList.get(position);
2798 final CharSequence imeName = item.mImeName;
2799 final CharSequence subtypeName = item.mSubtypeName;
2800 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
2801 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
2802 if (TextUtils.isEmpty(subtypeName)) {
2803 firstTextView.setText(imeName);
2804 secondTextView.setVisibility(View.GONE);
2805 } else {
2806 firstTextView.setText(subtypeName);
2807 secondTextView.setText(imeName);
2808 secondTextView.setVisibility(View.VISIBLE);
2809 }
2810 final RadioButton radioButton =
2811 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
2812 radioButton.setChecked(position == mCheckedItem);
2813 return view;
2814 }
2815 }
2816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07002818 synchronized (mMethodMap) {
2819 hideInputMethodMenuLocked();
2820 }
2821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002822
The Android Open Source Project10592532009-03-18 17:39:46 -07002823 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002824 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825
The Android Open Source Project10592532009-03-18 17:39:46 -07002826 if (mSwitchingDialog != null) {
2827 mSwitchingDialog.dismiss();
2828 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002830
The Android Open Source Project10592532009-03-18 17:39:46 -07002831 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002832 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002836
satok42c5a162011-05-26 16:46:14 +09002837 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002839 // TODO: Make this work even for non-current users?
2840 if (!calledFromValidUser()) {
2841 return false;
2842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 synchronized (mMethodMap) {
2844 if (mContext.checkCallingOrSelfPermission(
2845 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2846 != PackageManager.PERMISSION_GRANTED) {
2847 throw new SecurityException(
2848 "Requires permission "
2849 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
2850 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 long ident = Binder.clearCallingIdentity();
2853 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002854 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 } finally {
2856 Binder.restoreCallingIdentity(ident);
2857 }
2858 }
2859 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002860
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002861 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
2862 // Make sure this is a valid input method.
2863 InputMethodInfo imm = mMethodMap.get(id);
2864 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09002865 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002866 }
2867
satokd87c2592010-09-29 11:52:06 +09002868 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
2869 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002870
satokd87c2592010-09-29 11:52:06 +09002871 if (enabled) {
2872 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
2873 if (pair.first.equals(id)) {
2874 // We are enabling this input method, but it is already enabled.
2875 // Nothing to do. The previous state was enabled.
2876 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002877 }
2878 }
satokd87c2592010-09-29 11:52:06 +09002879 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
2880 // Previous state was disabled.
2881 return false;
2882 } else {
2883 StringBuilder builder = new StringBuilder();
2884 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
2885 builder, enabledInputMethodsList, id)) {
2886 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002887 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002888 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
2889 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
2890 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09002891 }
2892 // Previous state was enabled.
2893 return true;
2894 } else {
2895 // We are disabling the input method but it is already disabled.
2896 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002897 return false;
2898 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002899 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002900 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002901
satok723a27e2010-11-11 14:58:11 +09002902 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
2903 boolean setSubtypeOnly) {
2904 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002905 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09002906
2907 // Set Subtype here
2908 if (imi == null || subtypeId < 0) {
2909 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08002910 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09002911 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09002912 if (subtypeId < imi.getSubtypeCount()) {
2913 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
2914 mSettings.putSelectedSubtype(subtype.hashCode());
2915 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09002916 } else {
2917 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09002918 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002919 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09002920 }
satokab751aa2010-09-14 19:17:36 +09002921 }
satok723a27e2010-11-11 14:58:11 +09002922
satok4c0e7152012-06-20 20:08:44 +09002923 // Workaround.
2924 // ASEC is not ready in the IMMS constructor. Accordingly, forward-locked
2925 // IMEs are not recognized and considered uninstalled.
2926 // Actually, we can't move everything after SystemReady because
2927 // IMMS needs to run in the encryption lock screen. So, we just skip changing
2928 // the default IME here and try cheking the default IME again in systemReady().
2929 // TODO: Do nothing before system ready and implement a separated logic for
2930 // the encryption lock screen.
2931 // TODO: ASEC should be ready before IMMS is instantiated.
2932 if (mSystemReady && !setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09002933 // Set InputMethod here
2934 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
2935 }
2936 }
2937
2938 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
2939 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
2940 int lastSubtypeId = NOT_A_SUBTYPE_ID;
2941 // newDefaultIme is empty when there is no candidate for the selected IME.
2942 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
2943 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
2944 if (subtypeHashCode != null) {
2945 try {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002946 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09002947 imi, Integer.valueOf(subtypeHashCode));
2948 } catch (NumberFormatException e) {
2949 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
2950 }
2951 }
2952 }
2953 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09002954 }
2955
satok4e4569d2010-11-19 18:45:53 +09002956 // If there are no selected shortcuts, tries finding the most applicable ones.
2957 private Pair<InputMethodInfo, InputMethodSubtype>
2958 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
2959 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2960 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09002961 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09002962 boolean foundInSystemIME = false;
2963
2964 // Search applicable subtype for each InputMethodInfo
2965 for (InputMethodInfo imi: imis) {
satok7599a7fb2010-12-22 13:45:23 +09002966 final String imiId = imi.getId();
2967 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
2968 continue;
2969 }
satokcd7cd292010-11-20 15:46:23 +09002970 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09002971 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002972 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09002973 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09002974 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002975 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002976 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09002977 }
satokdf31ae62011-01-15 06:19:44 +09002978 // 2. Search by the system locale from enabledSubtypes.
2979 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09002980 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002981 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09002982 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09002983 }
satoka86f5e42011-09-02 17:12:42 +09002984 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002985 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09002986 final ArrayList<InputMethodSubtype> subtypesForSearch =
2987 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002988 ? InputMethodUtils.getSubtypes(imi)
2989 : overridingImplicitlyEnabledSubtypes;
satok7599a7fb2010-12-22 13:45:23 +09002990 // 4. Search by the current subtype's locale from all subtypes.
2991 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002992 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002993 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7fb2010-12-22 13:45:23 +09002994 }
2995 // 5. Search by the system locale from all subtypes.
2996 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09002997 if (subtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09002998 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09002999 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003000 }
satokcd7cd292010-11-20 15:46:23 +09003001 if (subtype != null) {
satok7599a7fb2010-12-22 13:45:23 +09003002 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003003 // The current input method is the most applicable IME.
3004 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003005 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003006 break;
satok7599a7fb2010-12-22 13:45:23 +09003007 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003008 // The system input method is 2nd applicable IME.
3009 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003010 mostApplicableSubtype = subtype;
satok7599a7fb2010-12-22 13:45:23 +09003011 if ((imi.getServiceInfo().applicationInfo.flags
3012 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3013 foundInSystemIME = true;
3014 }
satok4e4569d2010-11-19 18:45:53 +09003015 }
3016 }
3017 }
3018 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003019 if (mostApplicableIMI != null) {
3020 Slog.w(TAG, "Most applicable shortcut input method was:"
3021 + mostApplicableIMI.getId());
3022 if (mostApplicableSubtype != null) {
3023 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3024 + "," + mostApplicableSubtype.getMode() + ","
3025 + mostApplicableSubtype.getLocale());
3026 }
3027 }
satok4e4569d2010-11-19 18:45:53 +09003028 }
satokcd7cd292010-11-20 15:46:23 +09003029 if (mostApplicableIMI != null) {
satok4e4569d2010-11-19 18:45:53 +09003030 return new Pair<InputMethodInfo, InputMethodSubtype> (mostApplicableIMI,
satokcd7cd292010-11-20 15:46:23 +09003031 mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003032 } else {
3033 return null;
3034 }
3035 }
3036
satokab751aa2010-09-14 19:17:36 +09003037 /**
3038 * @return Return the current subtype of this input method.
3039 */
satok42c5a162011-05-26 16:46:14 +09003040 @Override
satokab751aa2010-09-14 19:17:36 +09003041 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003042 // TODO: Make this work even for non-current users?
3043 if (!calledFromValidUser()) {
3044 return null;
3045 }
3046 synchronized (mMethodMap) {
3047 return getCurrentInputMethodSubtypeLocked();
3048 }
3049 }
3050
3051 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003052 if (mCurMethodId == null) {
3053 return null;
3054 }
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003055 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003056 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3057 if (imi == null || imi.getSubtypeCount() == 0) {
3058 return null;
satok4e4569d2010-11-19 18:45:53 +09003059 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003060 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003061 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3062 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003063 if (subtypeId == NOT_A_SUBTYPE_ID) {
3064 // If there are no selected subtypes, the framework will try to find
3065 // the most applicable subtype from explicitly or implicitly enabled
3066 // subtypes.
3067 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003068 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003069 // If there is only one explicitly or implicitly enabled subtype,
3070 // just returns it.
3071 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3072 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3073 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003074 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003075 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003076 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003077 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003078 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003079 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3080 true);
satok4e4569d2010-11-19 18:45:53 +09003081 }
satok3ef8b292010-11-23 06:06:29 +09003082 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003083 } else {
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003084 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003085 }
3086 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003087 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003088 }
3089
satokf3db1af2010-11-23 13:34:33 +09003090 private void addShortcutInputMethodAndSubtypes(InputMethodInfo imi,
3091 InputMethodSubtype subtype) {
3092 if (mShortcutInputMethodsAndSubtypes.containsKey(imi)) {
3093 mShortcutInputMethodsAndSubtypes.get(imi).add(subtype);
3094 } else {
3095 ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3096 subtypes.add(subtype);
3097 mShortcutInputMethodsAndSubtypes.put(imi, subtypes);
3098 }
3099 }
3100
satok4e4569d2010-11-19 18:45:53 +09003101 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003102 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003103 @Override
satok4e4569d2010-11-19 18:45:53 +09003104 public List getShortcutInputMethodsAndSubtypes() {
3105 synchronized (mMethodMap) {
satok3da92232011-01-11 22:46:30 +09003106 ArrayList<Object> ret = new ArrayList<Object>();
satokf3db1af2010-11-23 13:34:33 +09003107 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003108 // If there are no selected shortcut subtypes, the framework will try to find
3109 // the most applicable subtype from all subtypes whose mode is
3110 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003111 Pair<InputMethodInfo, InputMethodSubtype> info =
3112 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003113 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7fb2010-12-22 13:45:23 +09003114 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003115 ret.add(info.first);
3116 ret.add(info.second);
satok7599a7fb2010-12-22 13:45:23 +09003117 }
satok3da92232011-01-11 22:46:30 +09003118 return ret;
satokf3db1af2010-11-23 13:34:33 +09003119 }
satokf3db1af2010-11-23 13:34:33 +09003120 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3121 ret.add(imi);
3122 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3123 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003124 }
3125 }
satokf3db1af2010-11-23 13:34:33 +09003126 return ret;
satok4e4569d2010-11-19 18:45:53 +09003127 }
3128 }
3129
satok42c5a162011-05-26 16:46:14 +09003130 @Override
satokb66d2872010-11-10 01:04:04 +09003131 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003132 // TODO: Make this work even for non-current users?
3133 if (!calledFromValidUser()) {
3134 return false;
3135 }
satokb66d2872010-11-10 01:04:04 +09003136 synchronized (mMethodMap) {
3137 if (subtype != null && mCurMethodId != null) {
3138 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003139 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003140 if (subtypeId != NOT_A_SUBTYPE_ID) {
3141 setInputMethodLocked(mCurMethodId, subtypeId);
3142 return true;
3143 }
3144 }
3145 return false;
3146 }
3147 }
3148
satok688bd472012-02-09 20:09:17 +09003149 private static class InputMethodAndSubtypeListManager {
3150 private final Context mContext;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003151 // Used to load label
satok688bd472012-02-09 20:09:17 +09003152 private final PackageManager mPm;
3153 private final InputMethodManagerService mImms;
satok93d744d2012-05-09 17:14:08 +09003154 private final String mSystemLocaleStr;
satok688bd472012-02-09 20:09:17 +09003155 public InputMethodAndSubtypeListManager(Context context, InputMethodManagerService imms) {
3156 mContext = context;
3157 mPm = context.getPackageManager();
3158 mImms = imms;
satok0a1bcf42012-05-16 19:26:31 +09003159 final Locale locale = context.getResources().getConfiguration().locale;
3160 mSystemLocaleStr = locale != null ? locale.toString() : "";
satok688bd472012-02-09 20:09:17 +09003161 }
3162
3163 private final TreeMap<InputMethodInfo, List<InputMethodSubtype>> mSortedImmis =
3164 new TreeMap<InputMethodInfo, List<InputMethodSubtype>>(
3165 new Comparator<InputMethodInfo>() {
3166 @Override
3167 public int compare(InputMethodInfo imi1, InputMethodInfo imi2) {
3168 if (imi2 == null) return 0;
3169 if (imi1 == null) return 1;
3170 if (mPm == null) {
3171 return imi1.getId().compareTo(imi2.getId());
3172 }
3173 CharSequence imiId1 = imi1.loadLabel(mPm) + "/" + imi1.getId();
3174 CharSequence imiId2 = imi2.loadLabel(mPm) + "/" + imi2.getId();
3175 return imiId1.toString().compareTo(imiId2.toString());
3176 }
3177 });
3178
3179 public ImeSubtypeListItem getNextInputMethod(
3180 boolean onlyCurrentIme, InputMethodInfo imi, InputMethodSubtype subtype) {
3181 if (imi == null) {
3182 return null;
3183 }
3184 final List<ImeSubtypeListItem> imList = getSortedInputMethodAndSubtypeList();
3185 if (imList.size() <= 1) {
3186 return null;
3187 }
3188 final int N = imList.size();
3189 final int currentSubtypeId = subtype != null
Satoshi Kataoka8e303cc52013-01-11 15:55:28 +09003190 ? InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode())
satok688bd472012-02-09 20:09:17 +09003191 : NOT_A_SUBTYPE_ID;
3192 for (int i = 0; i < N; ++i) {
3193 final ImeSubtypeListItem isli = imList.get(i);
3194 if (isli.mImi.equals(imi) && isli.mSubtypeId == currentSubtypeId) {
3195 if (!onlyCurrentIme) {
3196 return imList.get((i + 1) % N);
3197 }
3198 for (int j = 0; j < N - 1; ++j) {
3199 final ImeSubtypeListItem candidate = imList.get((i + j + 1) % N);
3200 if (candidate.mImi.equals(imi)) {
3201 return candidate;
3202 }
3203 }
3204 return null;
3205 }
3206 }
3207 return null;
3208 }
3209
3210 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList() {
3211 return getSortedInputMethodAndSubtypeList(true, false, false);
3212 }
3213
3214 public List<ImeSubtypeListItem> getSortedInputMethodAndSubtypeList(boolean showSubtypes,
3215 boolean inputShown, boolean isScreenLocked) {
3216 final ArrayList<ImeSubtypeListItem> imList = new ArrayList<ImeSubtypeListItem>();
3217 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
3218 mImms.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked();
3219 if (immis == null || immis.size() == 0) {
3220 return Collections.emptyList();
3221 }
3222 mSortedImmis.clear();
3223 mSortedImmis.putAll(immis);
3224 for (InputMethodInfo imi : mSortedImmis.keySet()) {
3225 if (imi == null) continue;
3226 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypeList = immis.get(imi);
3227 HashSet<String> enabledSubtypeSet = new HashSet<String>();
3228 for (InputMethodSubtype subtype: explicitlyOrImplicitlyEnabledSubtypeList) {
3229 enabledSubtypeSet.add(String.valueOf(subtype.hashCode()));
3230 }
satok688bd472012-02-09 20:09:17 +09003231 final CharSequence imeLabel = imi.loadLabel(mPm);
3232 if (showSubtypes && enabledSubtypeSet.size() > 0) {
3233 final int subtypeCount = imi.getSubtypeCount();
3234 if (DEBUG) {
3235 Slog.v(TAG, "Add subtypes: " + subtypeCount + ", " + imi.getId());
3236 }
3237 for (int j = 0; j < subtypeCount; ++j) {
3238 final InputMethodSubtype subtype = imi.getSubtypeAt(j);
3239 final String subtypeHashCode = String.valueOf(subtype.hashCode());
3240 // We show all enabled IMEs and subtypes when an IME is shown.
3241 if (enabledSubtypeSet.contains(subtypeHashCode)
3242 && ((inputShown && !isScreenLocked) || !subtype.isAuxiliary())) {
3243 final CharSequence subtypeLabel =
3244 subtype.overridesImplicitlyEnabledSubtype() ? null
3245 : subtype.getDisplayName(mContext, imi.getPackageName(),
3246 imi.getServiceInfo().applicationInfo);
satok93d744d2012-05-09 17:14:08 +09003247 imList.add(new ImeSubtypeListItem(imeLabel, subtypeLabel, imi, j,
3248 subtype.getLocale(), mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003249
3250 // Removing this subtype from enabledSubtypeSet because we no longer
3251 // need to add an entry of this subtype to imList to avoid duplicated
3252 // entries.
3253 enabledSubtypeSet.remove(subtypeHashCode);
3254 }
3255 }
3256 } else {
satok93d744d2012-05-09 17:14:08 +09003257 imList.add(new ImeSubtypeListItem(imeLabel, null, imi, NOT_A_SUBTYPE_ID,
3258 null, mSystemLocaleStr));
satok688bd472012-02-09 20:09:17 +09003259 }
3260 }
satok93d744d2012-05-09 17:14:08 +09003261 Collections.sort(imList);
satok688bd472012-02-09 20:09:17 +09003262 return imList;
3263 }
3264 }
3265
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003266 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003267 private static class InputMethodFileManager {
3268 private static final String SYSTEM_PATH = "system";
3269 private static final String INPUT_METHOD_PATH = "inputmethod";
3270 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3271 private static final String NODE_SUBTYPES = "subtypes";
3272 private static final String NODE_SUBTYPE = "subtype";
3273 private static final String NODE_IMI = "imi";
3274 private static final String ATTR_ID = "id";
3275 private static final String ATTR_LABEL = "label";
3276 private static final String ATTR_ICON = "icon";
3277 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
3278 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3279 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3280 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3281 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3282 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003283 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
satoke7c6998e2011-06-03 17:57:59 +09003284 new HashMap<String, List<InputMethodSubtype>>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003285 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003286 if (methodMap == null) {
3287 throw new NullPointerException("methodMap is null");
3288 }
3289 mMethodMap = methodMap;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003290 final File systemDir = userId == UserHandle.USER_OWNER
3291 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3292 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003293 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
3294 if (!inputMethodDir.mkdirs()) {
3295 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3296 }
3297 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3298 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3299 if (!subtypeFile.exists()) {
3300 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003301 writeAdditionalInputMethodSubtypes(
3302 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003303 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003304 readAdditionalInputMethodSubtypes(
3305 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003306 }
3307 }
3308
3309 private void deleteAllInputMethodSubtypes(String imiId) {
3310 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003311 mAdditionalSubtypesMap.remove(imiId);
3312 writeAdditionalInputMethodSubtypes(
3313 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003314 }
3315 }
3316
3317 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003318 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003319 synchronized (mMethodMap) {
3320 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
3321 final int N = additionalSubtypes.length;
3322 for (int i = 0; i < N; ++i) {
3323 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003324 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003325 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003326 } else {
3327 Slog.w(TAG, "Duplicated subtype definition found: "
3328 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003329 }
3330 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003331 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3332 writeAdditionalInputMethodSubtypes(
3333 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003334 }
3335 }
3336
3337 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3338 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003339 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003340 }
3341 }
3342
3343 private static void writeAdditionalInputMethodSubtypes(
3344 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3345 HashMap<String, InputMethodInfo> methodMap) {
3346 // Safety net for the case that this function is called before methodMap is set.
3347 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3348 FileOutputStream fos = null;
3349 try {
3350 fos = subtypesFile.startWrite();
3351 final XmlSerializer out = new FastXmlSerializer();
3352 out.setOutput(fos, "utf-8");
3353 out.startDocument(null, true);
3354 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3355 out.startTag(null, NODE_SUBTYPES);
3356 for (String imiId : allSubtypes.keySet()) {
3357 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3358 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3359 continue;
3360 }
3361 out.startTag(null, NODE_IMI);
3362 out.attribute(null, ATTR_ID, imiId);
3363 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3364 final int N = subtypesList.size();
3365 for (int i = 0; i < N; ++i) {
3366 final InputMethodSubtype subtype = subtypesList.get(i);
3367 out.startTag(null, NODE_SUBTYPE);
3368 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3369 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3370 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
3371 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3372 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3373 out.attribute(null, ATTR_IS_AUXILIARY,
3374 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3375 out.endTag(null, NODE_SUBTYPE);
3376 }
3377 out.endTag(null, NODE_IMI);
3378 }
3379 out.endTag(null, NODE_SUBTYPES);
3380 out.endDocument();
3381 subtypesFile.finishWrite(fos);
3382 } catch (java.io.IOException e) {
3383 Slog.w(TAG, "Error writing subtypes", e);
3384 if (fos != null) {
3385 subtypesFile.failWrite(fos);
3386 }
3387 }
3388 }
3389
3390 private static void readAdditionalInputMethodSubtypes(
3391 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3392 if (allSubtypes == null || subtypesFile == null) return;
3393 allSubtypes.clear();
3394 FileInputStream fis = null;
3395 try {
3396 fis = subtypesFile.openRead();
3397 final XmlPullParser parser = Xml.newPullParser();
3398 parser.setInput(fis, null);
3399 int type = parser.getEventType();
3400 // Skip parsing until START_TAG
3401 while ((type = parser.next()) != XmlPullParser.START_TAG
3402 && type != XmlPullParser.END_DOCUMENT) {}
3403 String firstNodeName = parser.getName();
3404 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3405 throw new XmlPullParserException("Xml doesn't start with subtypes");
3406 }
3407 final int depth =parser.getDepth();
3408 String currentImiId = null;
3409 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3410 while (((type = parser.next()) != XmlPullParser.END_TAG
3411 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3412 if (type != XmlPullParser.START_TAG)
3413 continue;
3414 final String nodeName = parser.getName();
3415 if (NODE_IMI.equals(nodeName)) {
3416 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3417 if (TextUtils.isEmpty(currentImiId)) {
3418 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3419 continue;
3420 }
3421 tempSubtypesArray = new ArrayList<InputMethodSubtype>();
3422 allSubtypes.put(currentImiId, tempSubtypesArray);
3423 } else if (NODE_SUBTYPE.equals(nodeName)) {
3424 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3425 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3426 continue;
3427 }
3428 final int icon = Integer.valueOf(
3429 parser.getAttributeValue(null, ATTR_ICON));
3430 final int label = Integer.valueOf(
3431 parser.getAttributeValue(null, ATTR_LABEL));
3432 final String imeSubtypeLocale =
3433 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
3434 final String imeSubtypeMode =
3435 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3436 final String imeSubtypeExtraValue =
3437 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003438 final boolean isAuxiliary = "1".equals(String.valueOf(
3439 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
satoke7c6998e2011-06-03 17:57:59 +09003440 final InputMethodSubtype subtype =
3441 new InputMethodSubtype(label, icon, imeSubtypeLocale,
3442 imeSubtypeMode, imeSubtypeExtraValue, isAuxiliary);
3443 tempSubtypesArray.add(subtype);
3444 }
3445 }
3446 } catch (XmlPullParserException e) {
3447 Slog.w(TAG, "Error reading subtypes: " + e);
3448 return;
3449 } catch (java.io.IOException e) {
3450 Slog.w(TAG, "Error reading subtypes: " + e);
3451 return;
3452 } catch (NumberFormatException e) {
3453 Slog.w(TAG, "Error reading subtypes: " + e);
3454 return;
3455 } finally {
3456 if (fis != null) {
3457 try {
3458 fis.close();
3459 } catch (java.io.IOException e1) {
3460 Slog.w(TAG, "Failed to close.");
3461 }
3462 }
3463 }
3464 }
3465 }
3466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 // ----------------------------------------------------------------------
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003468 // Utilities for debug
3469 private static String getStackTrace() {
3470 final StringBuilder sb = new StringBuilder();
3471 try {
3472 throw new RuntimeException();
3473 } catch (RuntimeException e) {
3474 final StackTraceElement[] frames = e.getStackTrace();
3475 // Start at 1 because the first frame is here and we don't care about it
3476 for (int j = 1; j < frames.length; ++j) {
3477 sb.append(frames[j].toString() + "\n");
3478 }
3479 }
3480 return sb.toString();
3481 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 @Override
3484 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3485 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3486 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3489 + Binder.getCallingPid()
3490 + ", uid=" + Binder.getCallingUid());
3491 return;
3492 }
3493
3494 IInputMethod method;
3495 ClientState client;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 synchronized (mMethodMap) {
3500 p.println("Current Input Method Manager state:");
3501 int N = mMethodList.size();
3502 p.println(" Input Methods:");
3503 for (int i=0; i<N; i++) {
3504 InputMethodInfo info = mMethodList.get(i);
3505 p.println(" InputMethod #" + i + ":");
3506 info.dump(p, " ");
3507 }
3508 p.println(" Clients:");
3509 for (ClientState ci : mClients.values()) {
3510 p.println(" Client " + ci + ":");
3511 p.println(" client=" + ci.client);
3512 p.println(" inputContext=" + ci.inputContext);
3513 p.println(" sessionRequested=" + ci.sessionRequested);
3514 p.println(" curSession=" + ci.curSession);
3515 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003516 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003518 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3519 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3521 + " mBoundToMethod=" + mBoundToMethod);
3522 p.println(" mCurToken=" + mCurToken);
3523 p.println(" mCurIntent=" + mCurIntent);
3524 method = mCurMethod;
3525 p.println(" mCurMethod=" + mCurMethod);
3526 p.println(" mEnabledSession=" + mEnabledSession);
3527 p.println(" mShowRequested=" + mShowRequested
3528 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3529 + " mShowForced=" + mShowForced
3530 + " mInputShown=" + mInputShown);
Dianne Hackborncc278702009-09-02 23:07:23 -07003531 p.println(" mSystemReady=" + mSystemReady + " mScreenOn=" + mScreenOn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003533
Jeff Brownb88102f2010-09-08 11:49:43 -07003534 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 pw.flush();
3537 try {
3538 client.client.asBinder().dump(fd, args);
3539 } catch (RemoteException e) {
3540 p.println("Input method client dead: " + e);
3541 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003542 } else {
3543 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003545
Jeff Brownb88102f2010-09-08 11:49:43 -07003546 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 pw.flush();
3549 try {
3550 method.asBinder().dump(fd, args);
3551 } catch (RemoteException e) {
3552 p.println("Input method service dead: " + e);
3553 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003554 } else {
3555 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557 }
3558}