blob: 5ba8bd555237ffc34795b1bd20362c3eb2810293 [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 Kataokad7443c82013-10-15 17:45:43 +090019import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
Satoshi Kataokad787f692013-10-26 04:44:21 +090020import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +090021import com.android.internal.inputmethod.InputMethodUtils;
22import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.internal.os.HandlerCaller;
Svetoslav Ganov758143e2012-08-06 16:40:27 -070024import com.android.internal.os.SomeArgs;
satoke7c6998e2011-06-03 17:57:59 +090025import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import com.android.internal.view.IInputContext;
27import com.android.internal.view.IInputMethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import com.android.internal.view.IInputMethodClient;
29import com.android.internal.view.IInputMethodManager;
30import com.android.internal.view.IInputMethodSession;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import com.android.internal.view.IInputSessionCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import com.android.internal.view.InputBindResult;
Yohei Yukawa33e81792015-11-17 21:14:42 -080033import com.android.internal.view.InputMethodClient;
Adam Lesinskief2ea1f2013-12-05 16:48:06 -080034import com.android.server.statusbar.StatusBarManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
satoke7c6998e2011-06-03 17:57:59 +090036import org.xmlpull.v1.XmlPullParser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import org.xmlpull.v1.XmlPullParserException;
satoke7c6998e2011-06-03 17:57:59 +090038import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070040import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070041import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.ActivityManagerNative;
43import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070044import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090045import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090046import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090047import android.app.Notification;
48import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070049import android.app.PendingIntent;
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -080050import android.app.SynchronousUserSwitchObserver;
satok5b927c432012-05-01 20:09:34 +090051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090057import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090059import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070061import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090062import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.PackageManager;
64import android.content.pm.ResolveInfo;
65import android.content.pm.ServiceInfo;
Amith Yamasani734983f2014-03-04 16:48:05 -080066import android.content.pm.UserInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070067import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.content.res.Resources;
69import android.content.res.TypedArray;
70import android.database.ContentObserver;
Alan Viverette505e3ab2014-11-24 15:22:11 -080071import android.graphics.drawable.Drawable;
Yohei Yukawab097b822015-12-01 10:43:08 -080072import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080073import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070074import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040076import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070077import android.os.Debug;
satoke7c6998e2011-06-03 17:57:59 +090078import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Handler;
80import android.os.IBinder;
81import android.os.IInterface;
82import android.os.Message;
83import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070084import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080086import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.ServiceManager;
88import android.os.SystemClock;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090089import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080090import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.provider.Settings;
92import android.text.TextUtils;
satokf9f01002011-05-19 21:31:50 +090093import android.text.style.SuggestionSpan;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070094import android.util.ArrayMap;
95import android.util.ArraySet;
Dianne Hackborn39606a02012-07-31 17:54:35 -070096import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090098import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090099import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.PrintWriterPrinter;
101import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900102import android.util.Slog;
103import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900104import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700106import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.view.LayoutInflater;
108import android.view.View;
109import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700111import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900112import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import android.view.inputmethod.InputBinding;
114import android.view.inputmethod.InputMethod;
115import android.view.inputmethod.InputMethodInfo;
116import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700117import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900118import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900119import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900120import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900121import android.widget.CompoundButton;
122import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900123import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900124import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900125import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
satoke7c6998e2011-06-03 17:57:59 +0900127import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900129import java.io.FileInputStream;
130import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.IOException;
132import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100133import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900135import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.HashMap;
137import java.util.List;
satok5b927c432012-05-01 20:09:34 +0900138import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140/**
141 * This class provides a system service that manages input methods.
142 */
143public class InputMethodManagerService extends IInputMethodManager.Stub
144 implements ServiceConnection, Handler.Callback {
145 static final boolean DEBUG = false;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700146 static final boolean DEBUG_RESTORE = DEBUG || false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700147 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700149 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
150 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
151 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 static final int MSG_UNBIND_INPUT = 1000;
154 static final int MSG_BIND_INPUT = 1010;
155 static final int MSG_SHOW_SOFT_INPUT = 1020;
156 static final int MSG_HIDE_SOFT_INPUT = 1030;
157 static final int MSG_ATTACH_TOKEN = 1040;
158 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 static final int MSG_START_INPUT = 2000;
161 static final int MSG_RESTART_INPUT = 2010;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800162
Yohei Yukawa33e81792015-11-17 21:14:42 -0800163 static final int MSG_UNBIND_CLIENT = 3000;
164 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700165 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700166 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900167 static final int MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER = 3040;
Yohei Yukawaae61f712015-12-09 13:00:10 -0800168 static final int MSG_SWITCH_IME = 3050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169
satok01038492012-04-09 21:08:27 +0900170 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
171
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700172 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800173
satokf9f01002011-05-19 21:31:50 +0900174 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
175
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900176 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900177 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900178
satok4e4569d2010-11-19 18:45:53 +0900179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800181 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900183 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700186 final WindowManagerInternal mWindowManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700188 final boolean mHasFeature;
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900189 private InputMethodFileManager mFileManager;
satok01038492012-04-09 21:08:27 +0900190 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900191 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800192 private final UserManager mUserManager;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900194 final InputBindResult mNoBinding = new InputBindResult(null, null, null, -1, -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 // All known input methods. mMethodMap also serves as the global
197 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700198 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
199 final HashMap<String, InputMethodInfo> mMethodMap = new HashMap<>();
satokf9f01002011-05-19 21:31:50 +0900200 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700201 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900202 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800203
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 // Used to bring IME service up to visible adjustment while it is being shown.
205 final ServiceConnection mVisibleConnection = new ServiceConnection() {
206 @Override public void onServiceConnected(ComponentName name, IBinder service) {
207 }
208
209 @Override public void onServiceDisconnected(ComponentName name) {
210 }
211 };
212 boolean mVisibleBound = false;
213
satok7cfc0ed2011-06-20 21:29:36 +0900214 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700215 private NotificationManager mNotificationManager;
216 private KeyguardManager mKeyguardManager;
217 private StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700219 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900220 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900221 private boolean mNotificationShown;
satok0a1bcf42012-05-16 19:26:31 +0900222 private final boolean mImeSelectedOnBoot;
satok7cfc0ed2011-06-20 21:29:36 +0900223
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900224 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 final ClientState client;
226 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700227
228 IInputMethodSession session;
229 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public String toString() {
233 return "SessionState{uid " + client.uid + " pid " + client.pid
234 + " method " + Integer.toHexString(
235 System.identityHashCode(method))
236 + " session " + Integer.toHexString(
237 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 + "}";
240 }
241
242 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700243 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 client = _client;
245 method = _method;
246 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700247 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800250
Jeff Brownc28867a2013-03-26 15:42:39 -0700251 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 final IInputMethodClient client;
253 final IInputContext inputContext;
254 final int uid;
255 final int pid;
256 final InputBinding binding;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean sessionRequested;
259 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 @Override
262 public String toString() {
263 return "ClientState{" + Integer.toHexString(
264 System.identityHashCode(this)) + " uid " + uid
265 + " pid " + pid + "}";
266 }
267
268 ClientState(IInputMethodClient _client, IInputContext _inputContext,
269 int _uid, int _pid) {
270 client = _client;
271 inputContext = _inputContext;
272 uid = _uid;
273 pid = _pid;
274 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
275 }
276 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800277
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700278 final HashMap<IBinder, ClientState> mClients = new HashMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700281 * Set once the system is ready to run third party code.
282 */
283 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800284
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700285 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700286 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
287 * method. This is to be synchronized with the secure settings keyed with
288 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
289 *
290 * <p>This can be transiently {@code null} when the system is re-initializing input method
291 * settings, e.g., the system locale is just changed.</p>
292 *
293 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
294 * {@link InputMethodManagerService}.</p>
295 *
296 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700298 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * The current binding sequence number, incremented every time there is
303 * a new bind performed.
304 */
305 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 /**
308 * The client that is currently bound to an input method.
309 */
310 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800313 * The last window token that we confirmed to be focused. This is always updated upon reports
314 * from the input method client. If the window state is already changed before the report is
315 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700316 */
317 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800318
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700319 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800320 * The client by which {@link #mCurFocusedWindow} was reported. Used only for debugging.
321 */
322 ClientState mCurFocusedWindowClient;
323
324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * The input context last provided by the current client.
326 */
327 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * The attributes last provided by the current client.
331 */
332 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700335 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700337 *
338 * <p>This can be {@code null} when no input method is connected.</p>
339 *
340 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700342 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 /**
satokab751aa2010-09-14 19:17:36 +0900346 * The current subtype of the current input method.
347 */
348 private InputMethodSubtype mCurrentSubtype;
349
satok4e4569d2010-11-19 18:45:53 +0900350 // This list contains the pairs of InputMethodInfo and InputMethodSubtype.
satokf3db1af2010-11-23 13:34:33 +0900351 private final HashMap<InputMethodInfo, ArrayList<InputMethodSubtype>>
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700352 mShortcutInputMethodsAndSubtypes = new HashMap<>();
satokab751aa2010-09-14 19:17:36 +0900353
John Spurlocke0980502013-10-25 11:59:29 -0400354 // Was the keyguard locked when this client became current?
355 private boolean mCurClientInKeyguard;
356
satokab751aa2010-09-14 19:17:36 +0900357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * Set to true if our ServiceConnection is currently actively bound to
359 * a service (whether or not we have gotten its IBinder back yet).
360 */
361 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
364 * Set if the client has asked for the input method to be shown.
365 */
366 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
369 * Set if we were explicitly told to show the input method.
370 */
371 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 /**
374 * Set if we were forced to be shown.
375 */
376 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 /**
379 * Set if we last told the input method to show itself.
380 */
381 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
384 * The Intent used to connect to the current input method.
385 */
386 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 /**
389 * The token we have made for the currently active input method, to
390 * identify it in the future.
391 */
392 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 /**
395 * If non-null, this is the input method service we are currently connected
396 * to.
397 */
398 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Time that we last initiated a bind to the input method, to determine
402 * if we should try to disconnect and reconnect to it.
403 */
404 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 /**
407 * Have we called mCurMethod.bindInput()?
408 */
409 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 /**
412 * Currently enabled session. Only touched by service thread, not
413 * protected by a lock.
414 */
415 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700418 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700420 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800421
Yohei Yukawa3d1e8122014-06-06 19:12:47 +0900422 int mCurUserActionNotificationSequenceNumber = 0;
423
Joe Onorato857fd9b2011-01-27 15:08:35 -0800424 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900425
426 /**
427 * A set of status bits regarding the active IME.
428 *
429 * <p>This value is a combination of following two bits:</p>
430 * <dl>
431 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
432 * <dd>
433 * If this bit is ON, connected IME is ready to accept touch/key events.
434 * </dd>
435 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
436 * <dd>
437 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
438 * </dd>
439 * </dl>
440 * <em>Do not update this value outside of setImeWindowStatus.</em>
441 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800442 int mImeWindowVis;
443
Ken Wakasa05dbb652011-08-22 15:22:43 +0900444 private AlertDialog.Builder mDialogBuilder;
445 private AlertDialog mSwitchingDialog;
satok01038492012-04-09 21:08:27 +0900446 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900447 private InputMethodInfo[] mIms;
448 private int[] mSubtypeIds;
satok5b927c432012-05-01 20:09:34 +0900449 private Locale mLastSystemLocale;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700450 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800451 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900452 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
453 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500454 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -0700457 int mUserId;
458 boolean mRegistered = false;
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800459 String mLastEnabled = "";
460
Yohei Yukawa81482972015-06-04 00:58:59 -0700461 /**
462 * <em>This constructor must be called within the lock.</em>
463 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 SettingsObserver(Handler handler) {
465 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -0700466 }
467
468 public void registerContentObserverLocked(int userId) {
469 if (mRegistered && mUserId == userId) {
470 return;
471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -0700473 if (mRegistered) {
474 mContext.getContentResolver().unregisterContentObserver(this);
475 mRegistered = false;
476 }
477 if (mUserId != userId) {
478 mLastEnabled = "";
479 mUserId = userId;
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700482 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +0900483 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700484 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +0900485 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700486 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -0700487 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -0700488 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -0800489 resolver.registerContentObserver(Settings.Secure.getUriFor(
490 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -0700491 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800493
Michael Wright7b5a96b2014-08-09 19:28:42 -0700494 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -0800495 final Uri showImeUri = Settings.Secure.getUriFor(
496 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
497 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
498 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700500 if (showImeUri.equals(uri)) {
501 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -0800502 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
503 mAccessibilityRequestingNoSoftKeyboard = Settings.Secure.getIntForUser(
504 mContext.getContentResolver(),
505 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE,
506 0, mUserId) == 1;
507 if (mAccessibilityRequestingNoSoftKeyboard) {
508 final boolean showRequested = mShowRequested;
509 hideCurrentInputLocked(0, null);
510 mShowRequested = showRequested;
511 } else if (mShowRequested) {
512 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
513 }
Michael Wright7b5a96b2014-08-09 19:28:42 -0700514 } else {
515 boolean enabledChanged = false;
516 String newEnabled = mSettings.getEnabledInputMethodsStr();
517 if (!mLastEnabled.equals(newEnabled)) {
518 mLastEnabled = newEnabled;
519 enabledChanged = true;
520 }
521 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 }
Yohei Yukawa81482972015-06-04 00:58:59 -0700525
526 @Override
527 public String toString() {
528 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
529 + " mLastEnabled=" + mLastEnabled + "}";
530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800532
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900533 class ImmsBroadcastReceiver extends android.content.BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900534 @Override
535 public void onReceive(Context context, Intent intent) {
536 final String action = intent.getAction();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700537 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900538 hideInputMethodMenu();
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700539 // No need to update mIsInteractive
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900540 return;
Amith Yamasani734983f2014-03-04 16:48:05 -0800541 } else if (Intent.ACTION_USER_ADDED.equals(action)
542 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +0100543 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -0800544 return;
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700545 } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
546 final String name = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
547 if (Settings.Secure.ENABLED_INPUT_METHODS.equals(name)) {
548 final String prevValue = intent.getStringExtra(
549 Intent.EXTRA_SETTING_PREVIOUS_VALUE);
550 final String newValue = intent.getStringExtra(
551 Intent.EXTRA_SETTING_NEW_VALUE);
552 restoreEnabledInputMethods(mContext, prevValue, newValue);
553 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900554 } else {
555 Slog.w(TAG, "Unexpected intent " + intent);
556 }
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800559
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700560 // Apply the results of a restore operation to the set of enabled IMEs. Note that this
561 // does not attempt to validate on the fly with any installed device policy, so must only
562 // be run in the context of initial device setup.
563 //
564 // TODO: Move this method to InputMethodUtils with adding unit tests.
565 static void restoreEnabledInputMethods(Context context, String prevValue, String newValue) {
566 if (DEBUG_RESTORE) {
567 Slog.i(TAG, "Restoring enabled input methods:");
568 Slog.i(TAG, "prev=" + prevValue);
569 Slog.i(TAG, " new=" + newValue);
570 }
571 // 'new' is the just-restored state, 'prev' is what was in settings prior to the restore
Seigo Nonaka2028dda2015-07-06 17:41:24 +0900572 ArrayMap<String, ArraySet<String>> prevMap =
573 InputMethodUtils.parseInputMethodsAndSubtypesString(prevValue);
574 ArrayMap<String, ArraySet<String>> newMap =
575 InputMethodUtils.parseInputMethodsAndSubtypesString(newValue);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700576
577 // Merge the restored ime+subtype enabled states into the live state
578 for (ArrayMap.Entry<String, ArraySet<String>> entry : newMap.entrySet()) {
579 final String imeId = entry.getKey();
580 ArraySet<String> prevSubtypes = prevMap.get(imeId);
581 if (prevSubtypes == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700582 prevSubtypes = new ArraySet<>(2);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700583 prevMap.put(imeId, prevSubtypes);
584 }
585 prevSubtypes.addAll(entry.getValue());
586 }
587
Seigo Nonaka2a099bc2015-08-14 19:29:45 -0700588 final String mergedImesAndSubtypesString =
589 InputMethodUtils.buildInputMethodsAndSubtypesString(prevMap);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700590 if (DEBUG_RESTORE) {
591 Slog.i(TAG, "Merged IME string:");
592 Slog.i(TAG, " " + mergedImesAndSubtypesString);
593 }
594 Settings.Secure.putString(context.getContentResolver(),
595 Settings.Secure.ENABLED_INPUT_METHODS, mergedImesAndSubtypesString);
596 }
597
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800598 class MyPackageMonitor extends PackageMonitor {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900599 private boolean isChangingPackagesOfCurrentUser() {
600 final int userId = getChangingUserId();
601 final boolean retval = userId == mSettings.getCurrentUserId();
602 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +0900603 if (!retval) {
604 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
605 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900606 }
607 return retval;
608 }
609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800611 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900612 if (!isChangingPackagesOfCurrentUser()) {
613 return false;
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900616 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 final int N = mMethodList.size();
618 if (curInputMethodId != null) {
619 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800620 InputMethodInfo imi = mMethodList.get(i);
621 if (imi.getId().equals(curInputMethodId)) {
622 for (String pkg : packages) {
623 if (imi.getPackageName().equals(pkg)) {
624 if (!doit) {
625 return true;
626 }
satok723a27e2010-11-11 14:58:11 +0900627 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800628 chooseNewDefaultIMELocked();
629 return true;
630 }
631 }
632 }
633 }
634 }
635 }
636 return false;
637 }
638
639 @Override
640 public void onSomePackagesChanged() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900641 if (!isChangingPackagesOfCurrentUser()) {
642 return;
643 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800644 synchronized (mMethodMap) {
645 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900646 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800647 final int N = mMethodList.size();
648 if (curInputMethodId != null) {
649 for (int i=0; i<N; i++) {
650 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +0900651 final String imiId = imi.getId();
652 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800653 curIm = imi;
654 }
satoke7c6998e2011-06-03 17:57:59 +0900655
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800656 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +0900657 if (isPackageModified(imi.getPackageName())) {
658 mFileManager.deleteAllInputMethodSubtypes(imiId);
659 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800660 if (change == PACKAGE_TEMPORARY_CHANGE
661 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800662 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800663 + imi.getComponent());
664 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666 }
667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800668
Yohei Yukawa94e33302016-02-12 19:37:03 -0800669 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800672
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800673 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -0800674 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 if (change == PACKAGE_TEMPORARY_CHANGE
676 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800677 ServiceInfo si = null;
678 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900679 si = mIPackageManager.getServiceInfo(
680 curIm.getComponent(), 0, mSettings.getCurrentUserId());
681 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 }
683 if (si == null) {
684 // Uh oh, current input method is no longer around!
685 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -0800686 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900687 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800689 changed = true;
690 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +0900692 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800693 }
694 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800695 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800696 }
satokab751aa2010-09-14 19:17:36 +0900697
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800698 if (curIm == null) {
699 // We currently don't have a default input method... is
700 // one now available?
701 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -0700702 } else if (!changed && isPackageModified(curIm.getPackageName())) {
703 // Even if the current input method is still available, mCurrentSubtype could
704 // be obsolete when the package is modified in practice.
705 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800708 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800709 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 }
713 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800714
Jeff Brownc28867a2013-03-26 15:42:39 -0700715 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900716 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -0700717 private final IInputMethod mMethod;
718 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800719
Jeff Brownc28867a2013-03-26 15:42:39 -0700720 MethodCallback(InputMethodManagerService imms, IInputMethod method,
721 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +0900722 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -0700723 mMethod = method;
724 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
satoke7c6998e2011-06-03 17:57:59 +0900727 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -0700728 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -0700729 long ident = Binder.clearCallingIdentity();
730 try {
731 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
732 } finally {
733 Binder.restoreCallingIdentity(ident);
734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800737
satok01038492012-04-09 21:08:27 +0900738 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -0700739 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +0900740 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -0700741 public void onHardKeyboardStatusChange(boolean available) {
742 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
743 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +0900744 }
745
Michael Wright7b5a96b2014-08-09 19:28:42 -0700746 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +0900747 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -0700748 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +0900749 }
750 synchronized(mMethodMap) {
751 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
752 && mSwitchingDialog.isShowing()) {
753 mSwitchingDialogTitleView.findViewById(
754 com.android.internal.R.id.hard_keyboard_section).setVisibility(
755 available ? View.VISIBLE : View.GONE);
756 }
757 }
758 }
759 }
760
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800761 public static final class Lifecycle extends SystemService {
762 private InputMethodManagerService mService;
763
764 public Lifecycle(Context context) {
765 super(context);
766 mService = new InputMethodManagerService(context);
767 }
768
769 @Override
770 public void onStart() {
771 LocalServices.addService(InputMethodManagerInternal.class,
772 new LocalServiceImpl(mService.mHandler));
773 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
774 }
775
776 @Override
777 public void onBootPhase(int phase) {
778 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
779 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
780 .getService(Context.STATUS_BAR_SERVICE);
781 mService.systemRunning(statusBarService);
782 }
783 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800784
785 @Override
786 public void onUnlockUser(int userHandle) {
787 mService.onUnlockUser(userHandle);
788 }
789 }
790
791 public void onUnlockUser(int userId) {
792 synchronized(mMethodMap) {
793 final int currentUserId = mSettings.getCurrentUserId();
794 if (DEBUG) {
795 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
796 }
797 if (userId != currentUserId) {
798 return;
799 }
800 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
801 // We need to rebuild IMEs.
802 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
803 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
804 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800805 }
806
Seigo Nonaka7309b122015-08-17 18:34:13 -0700807 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900808 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800810 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700812 // Note: SettingsObserver doesn't register observers in its constructor.
813 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 mIWindowManager = IWindowManager.Stub.asInterface(
815 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700816 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800817 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900818 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 public void executeMessage(Message msg) {
820 handleMessage(msg);
821 }
Mita Yuned218c72012-12-06 17:18:25 -0800822 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800823 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800824 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900825 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700826 mHasFeature = context.getPackageManager().hasSystemFeature(
827 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500828 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900829
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400830 Bundle extras = new Bundle();
831 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
832 mImeSwitcherNotification = new Notification.Builder(mContext)
833 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
834 .setWhen(0)
835 .setOngoing(true)
836 .addExtras(extras)
837 .setCategory(Notification.CATEGORY_SYSTEM)
838 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400839
satok7cfc0ed2011-06-20 21:29:36 +0900840 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900841 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900842
843 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900844
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900845 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900846 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800847 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
848 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700849 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900850 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
satok7cfc0ed2011-06-20 21:29:36 +0900852 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900853 int userId = 0;
854 try {
855 ActivityManagerNative.getDefault().registerUserSwitchObserver(
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -0800856 new SynchronousUserSwitchObserver() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900857 @Override
Fyodor Kupolov6005b3f2015-11-23 17:41:50 -0800858 public void onUserSwitching(int newUserId)
859 throws RemoteException {
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900860 synchronized(mMethodMap) {
861 switchUserLocked(newUserId);
862 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900863 }
864
865 @Override
866 public void onUserSwitchComplete(int newUserId) throws RemoteException {
867 }
Kenny Guy42979622015-04-13 18:03:05 +0000868
869 @Override
870 public void onForegroundProfileSwitch(int newProfileId) {
871 // Ignore.
872 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900873 });
874 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
875 } catch (RemoteException e) {
876 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
877 }
satok81f8b7c2012-12-04 20:42:56 +0900878 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900879
satokd87c2592010-09-29 11:52:06 +0900880 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900881 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800882 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700883
Kenny Guy2a764942014-04-02 13:29:20 +0100884 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900885 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900886 synchronized (mMethodMap) {
887 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
888 mSettings, context);
889 }
satok0a1bcf42012-05-16 19:26:31 +0900890
891 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900892 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900893 if (DEBUG) {
894 Slog.d(TAG, "Initial default ime = " + defaultImiId);
895 }
satok0a1bcf42012-05-16 19:26:31 +0900896 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
897
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900898 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800899 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900900 }
satokd87c2592010-09-29 11:52:06 +0900901 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
satok0a1bcf42012-05-16 19:26:31 +0900903 if (!mImeSelectedOnBoot) {
904 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900905 synchronized (mMethodMap) {
906 resetDefaultImeLocked(context);
907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800909
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900910 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700911 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900912 updateFromSettingsLocked(true);
913 }
satok5b927c432012-05-01 20:09:34 +0900914
915 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
916 // according to the new system locale.
917 final IntentFilter filter = new IntentFilter();
918 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
919 mContext.registerReceiver(
920 new BroadcastReceiver() {
921 @Override
922 public void onReceive(Context context, Intent intent) {
923 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900924 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900925 }
926 }
927 }, filter);
928 }
929
satok5b927c432012-05-01 20:09:34 +0900930 private void resetDefaultImeLocked(Context context) {
931 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800932 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900933 return;
934 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800935 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
936 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
937 if (suitableImes.isEmpty()) {
938 Slog.i(TAG, "No default found");
939 return;
satok5b927c432012-05-01 20:09:34 +0900940 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800941 final InputMethodInfo defIm = suitableImes.get(0);
942 Slog.i(TAG, "Default found, using " + defIm.getId());
943 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900944 }
945
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900946 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
947 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900948 if (!mSystemReady) {
949 // not system ready
950 return;
951 }
952 final Locale newLocale = mRes.getConfiguration().locale;
953 if (!updateOnlyWhenLocaleChanged
954 || (newLocale != null && !newLocale.equals(mLastSystemLocale))) {
955 if (!updateOnlyWhenLocaleChanged) {
956 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800957 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900958 }
959 if (DEBUG) {
960 Slog.i(TAG, "Locale has been changed to " + newLocale);
961 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800962 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900963 if (!updateOnlyWhenLocaleChanged) {
964 final String selectedImiId = mSettings.getSelectedInputMethod();
965 if (TextUtils.isEmpty(selectedImiId)) {
966 // This is the first time of the user switch and
967 // set the current ime to the proper one.
968 resetDefaultImeLocked(mContext);
969 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900970 } else {
971 // If the locale is changed, needs to reset the default ime
972 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900973 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800974 updateFromSettingsLocked(true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900975 mLastSystemLocale = newLocale;
976 if (!updateOnlyWhenLocaleChanged) {
977 try {
978 startInputInnerLocked();
979 } catch (RuntimeException e) {
980 Slog.w(TAG, "Unexpected exception", e);
981 }
982 }
983 }
984 }
985
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900986 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900987 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
988 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900989 }
990
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900991 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700992 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
993 + " currentUserId=" + mSettings.getCurrentUserId());
994
Yohei Yukawa81482972015-06-04 00:58:59 -0700995 // ContentObserver should be registered again when the user is changed
996 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800997
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800998 // If the system is not ready or the device is not yed unlocked by the user, then we use
999 // copy-on-write settings.
1000 final boolean useCopyOnWriteSettings =
1001 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001002 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001003 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001004 // InputMethodFileManager should be reset when the user is changed
1005 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001006 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001007
1008 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1009 + " defaultImiId=" + defaultImiId);
1010
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001011 // For secondary users, the list of enabled IMEs may not have been updated since the
1012 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1013 // not be empty even if the IME has been uninstalled by the primary user.
1014 // Even in such cases, IMMS works fine because it will find the most applicable
1015 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001016 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001017 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001018 initialUserSwitch /* needsToResetDefaultIme */);
1019 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001020 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1021 mSettings.getEnabledInputMethodListLocked(), newUserId,
1022 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001023 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001024
1025 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1026 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001027 }
1028
Kenny Guy2a764942014-04-02 13:29:20 +01001029 void updateCurrentProfileIds() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001030 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001031 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1032 for (int i = 0; i < currentProfileIds.length; i++) {
1033 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001034 }
Kenny Guy2a764942014-04-02 13:29:20 +01001035 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001036 }
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 @Override
1039 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1040 throws RemoteException {
1041 try {
1042 return super.onTransact(code, data, reply, flags);
1043 } catch (RuntimeException e) {
1044 // The input method manager only throws security exceptions, so let's
1045 // log all others.
1046 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001047 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 throw e;
1050 }
1051 }
1052
Svetoslav Ganova0027152013-06-25 14:59:53 -07001053 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001054 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001055 if (DEBUG) {
1056 Slog.d(TAG, "--- systemReady");
1057 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001058 if (!mSystemReady) {
1059 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001060 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001061 mSettings.switchCurrentUser(currentUserId,
1062 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001063 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1064 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001065 mStatusBar = statusBar;
Jason Monk3e189872016-01-12 09:10:34 -05001066 statusBar.setIconVisibility(mSlotIme, false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001067 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001068 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1069 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001070 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001071 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001072 mHardKeyboardListener);
1073 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001074 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001075 if (!mImeSelectedOnBoot) {
1076 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001077 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001078 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1079 mSettings.getEnabledInputMethodListLocked(),
1080 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001081 }
1082 mLastSystemLocale = mRes.getConfiguration().locale;
Dianne Hackborncc278702009-09-02 23:07:23 -07001083 try {
1084 startInputInnerLocked();
1085 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001086 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001087 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001088 }
1089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001091
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001092 // ---------------------------------------------------------------------------------------
1093 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1094 // 1) it comes from the system process
1095 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1096 private boolean calledFromValidUser() {
1097 final int uid = Binder.getCallingUid();
1098 final int userId = UserHandle.getUserId(uid);
1099 if (DEBUG) {
1100 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1101 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1102 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001103 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1104 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 }
Kenny Guy2a764942014-04-02 13:29:20 +01001106 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001107 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001108 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001109
1110 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1111 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1112 // must not manage background users' states in any functions.
1113 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1114 // by a token.
1115 if (mContext.checkCallingOrSelfPermission(
1116 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1117 == PackageManager.PERMISSION_GRANTED) {
1118 if (DEBUG) {
1119 Slog.d(TAG, "--- Access granted because the calling process has "
1120 + "the INTERACT_ACROSS_USERS_FULL permission");
1121 }
1122 return true;
1123 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001124 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1125 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001126 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001127 }
1128
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001129
1130 /**
1131 * Returns true iff the caller is identified to be the current input method with the token.
1132 * @param token The window token given to the input method when it was started.
1133 * @return true if and only if non-null valid token is specified.
1134 */
1135 private boolean calledWithValidToken(IBinder token) {
1136 if (token == null || mCurToken != token) {
1137 return false;
1138 }
1139 return true;
1140 }
1141
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001142 private boolean bindCurrentInputMethodService(
1143 Intent service, ServiceConnection conn, int flags) {
1144 if (service == null || conn == null) {
1145 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1146 return false;
1147 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001148 return mContext.bindServiceAsUser(service, conn, flags,
1149 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001150 }
1151
satoke7c6998e2011-06-03 17:57:59 +09001152 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001154 // TODO: Make this work even for non-current users?
1155 if (!calledFromValidUser()) {
1156 return Collections.emptyList();
1157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001159 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161 }
1162
satoke7c6998e2011-06-03 17:57:59 +09001163 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001165 // TODO: Make this work even for non-current users?
1166 if (!calledFromValidUser()) {
1167 return Collections.emptyList();
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001170 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 }
1173
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001174 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001175 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001176 * @return enabled subtypes of the specified imi
1177 */
satoke7c6998e2011-06-03 17:57:59 +09001178 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001179 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001180 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001181 // TODO: Make this work even for non-current users?
1182 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001183 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001184 }
satok67ddf9c2010-11-17 09:45:54 +09001185 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001186 final InputMethodInfo imi;
1187 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001188 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001189 } else {
1190 imi = mMethodMap.get(imiId);
1191 }
1192 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001193 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001194 }
1195 return mSettings.getEnabledInputMethodSubtypeListLocked(
1196 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001197 }
1198 }
1199
satoke7c6998e2011-06-03 17:57:59 +09001200 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 public void addClient(IInputMethodClient client,
1202 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001203 if (!calledFromValidUser()) {
1204 return;
1205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 synchronized (mMethodMap) {
1207 mClients.put(client.asBinder(), new ClientState(client,
1208 inputContext, uid, pid));
1209 }
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
satoke7c6998e2011-06-03 17:57:59 +09001212 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001214 if (!calledFromValidUser()) {
1215 return;
1216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001218 ClientState cs = mClients.remove(client.asBinder());
1219 if (cs != null) {
1220 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001221 if (mCurClient == cs) {
1222 mCurClient = null;
1223 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001224 if (mCurFocusedWindowClient == cs) {
1225 mCurFocusedWindowClient = null;
1226 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 void executeOrSendMessage(IInterface target, Message msg) {
1232 if (target.asBinder() instanceof Binder) {
1233 mCaller.sendMessage(msg);
1234 } else {
1235 handleMessage(msg);
1236 msg.recycle();
1237 }
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
Yohei Yukawa33e81792015-11-17 21:14:42 -08001240 void unbindCurrentClientLocked(
1241 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001243 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 + mCurClient.client.asBinder());
1245 if (mBoundToMethod) {
1246 mBoundToMethod = false;
1247 if (mCurMethod != null) {
1248 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1249 MSG_UNBIND_INPUT, mCurMethod));
1250 }
1251 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001252
1253 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1254 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001255 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1256 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001259
The Android Open Source Project10592532009-03-18 17:39:46 -07001260 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 }
1262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 private int getImeShowFlags() {
1265 int flags = 0;
1266 if (mShowForced) {
1267 flags |= InputMethod.SHOW_FORCED
1268 | InputMethod.SHOW_EXPLICIT;
1269 } else if (mShowExplicitlyRequested) {
1270 flags |= InputMethod.SHOW_EXPLICIT;
1271 }
1272 return flags;
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 private int getAppShowFlags() {
1276 int flags = 0;
1277 if (mShowForced) {
1278 flags |= InputMethodManager.SHOW_FORCED;
1279 } else if (!mShowExplicitlyRequested) {
1280 flags |= InputMethodManager.SHOW_IMPLICIT;
1281 }
1282 return flags;
1283 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001284
Dianne Hackborn7663d802012-02-24 13:08:49 -08001285 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 if (!mBoundToMethod) {
1287 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1288 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1289 mBoundToMethod = true;
1290 }
1291 final SessionState session = mCurClient.curSession;
1292 if (initial) {
1293 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1294 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1295 } else {
1296 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1297 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1298 }
1299 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001300 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001301 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001303 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001304 (session.channel != null ? session.channel.dup() : null),
1305 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001307
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001308 InputBindResult startInputLocked(
1309 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1310 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1311 int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 // If no method is currently selected, do nothing.
1313 if (mCurMethodId == null) {
1314 return mNoBinding;
1315 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 ClientState cs = mClients.get(client.asBinder());
1318 if (cs == null) {
1319 throw new IllegalArgumentException("unknown client "
1320 + client.asBinder());
1321 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 try {
1324 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1325 // Check with the window manager to make sure this client actually
1326 // has a window with focus. If not, reject. This is thread safe
1327 // because if the focus changes some time before or after, the
1328 // next client receiving focus that has any interest in input will
1329 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001330 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1332 return null;
1333 }
1334 } catch (RemoteException e) {
1335 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336
Dianne Hackborn7663d802012-02-24 13:08:49 -08001337 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1338 }
1339
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001340 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
1341 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001342 // If no method is currently selected, do nothing.
1343 if (mCurMethodId == null) {
1344 return mNoBinding;
1345 }
1346
Yohei Yukawad57ba672015-06-08 16:39:46 -07001347 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1348 attribute.packageName)) {
1349 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1350 + " uid=" + cs.uid + " package=" + attribute.packageName);
1351 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001352 }
1353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001355 // Was the keyguard locked when switching over to the new client?
1356 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 // If the client is changing, we need to switch over to the new
1358 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001359 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001360 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001361 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
1363 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001364 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001365 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001366 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 }
1368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 // Bump up the sequence for this client and attach it.
1371 mCurSeq++;
1372 if (mCurSeq <= 0) mCurSeq = 1;
1373 mCurClient = cs;
1374 mCurInputContext = inputContext;
1375 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 // Check if the input method is changing.
1378 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1379 if (cs.curSession != null) {
1380 // Fast case: if we are already connected to the input method,
1381 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001382 return attachNewInputLocked(
1383 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385 if (mHaveConnection) {
1386 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 // Return to client, and we will get back with it when
1388 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001389 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001390 return new InputBindResult(null, null, mCurId, mCurSeq,
1391 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 } else if (SystemClock.uptimeMillis()
1393 < (mLastBindTime+TIME_TO_RECONNECT)) {
1394 // In this case we have connected to the service, but
1395 // don't yet have its interface. If it hasn't been too
1396 // long since we did the connection, we'll return to
1397 // the client and wait to get the service interface so
1398 // we can report back. If it has been too long, we want
1399 // to fall through so we can try a disconnect/reconnect
1400 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001401 return new InputBindResult(null, null, mCurId, mCurSeq,
1402 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001404 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1405 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
1407 }
1408 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001409
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001410 return startInputInnerLocked();
1411 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001412
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001413 InputBindResult startInputInnerLocked() {
1414 if (mCurMethodId == null) {
1415 return mNoBinding;
1416 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001417
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001418 if (!mSystemReady) {
1419 // If the system is not yet ready, we shouldn't be running third
1420 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001421 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1422 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001423 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1426 if (info == null) {
1427 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001430 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1433 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001434 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1435 com.android.internal.R.string.input_method_binding_label);
1436 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1437 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001438 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001439 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1440 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 mLastBindTime = SystemClock.uptimeMillis();
1442 mHaveConnection = true;
1443 mCurId = info.getId();
1444 mCurToken = new Binder();
1445 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001446 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 mIWindowManager.addWindowToken(mCurToken,
1448 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1449 } catch (RemoteException e) {
1450 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001451 return new InputBindResult(null, null, mCurId, mCurSeq,
1452 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 } else {
1454 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 + mCurIntent);
1457 }
1458 return null;
1459 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001460
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001461 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001462 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1463 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1464 int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001465 if (!calledFromValidUser()) {
1466 return null;
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001469 if (DEBUG) {
1470 Slog.v(TAG, "startInput: reason="
1471 + InputMethodClient.getStartInputReason(startInputReason)
1472 + " client = " + client.asBinder()
1473 + " inputContext=" + inputContext
1474 + " attribute=" + attribute
1475 + " controlFlags=#" + Integer.toHexString(controlFlags));
1476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 final long ident = Binder.clearCallingIdentity();
1478 try {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001479 return startInputLocked(startInputReason, client, inputContext, attribute,
1480 controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 } finally {
1482 Binder.restoreCallingIdentity(ident);
1483 }
1484 }
1485 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001486
satoke7c6998e2011-06-03 17:57:59 +09001487 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 public void finishInput(IInputMethodClient client) {
1489 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
satoke7c6998e2011-06-03 17:57:59 +09001491 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 public void onServiceConnected(ComponentName name, IBinder service) {
1493 synchronized (mMethodMap) {
1494 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1495 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001496 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001497 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001498 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001499 return;
1500 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001501 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001502 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1503 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001505 clearClientSessionLocked(mCurClient);
1506 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
1508 }
1509 }
1510 }
1511
Jeff Brownc28867a2013-03-26 15:42:39 -07001512 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1513 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 synchronized (mMethodMap) {
1515 if (mCurMethod != null && method != null
1516 && mCurMethod.asBinder() == method.asBinder()) {
1517 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001518 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001520 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001521 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 if (res.method != null) {
1523 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001524 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001526 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
1528 }
1529 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001530
1531 // Session abandoned. Close its associated input channel.
1532 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001534
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001535 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001536 if (mVisibleBound) {
1537 mContext.unbindService(mVisibleConnection);
1538 mVisibleBound = false;
1539 }
1540
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001541 if (mHaveConnection) {
1542 mContext.unbindService(this);
1543 mHaveConnection = false;
1544 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001545
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001546 if (mCurToken != null) {
1547 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001548 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001549 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001550 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001551 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001552 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001553 mIWindowManager.removeWindowToken(mCurToken);
1554 } catch (RemoteException e) {
1555 }
1556 mCurToken = null;
1557 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001558
The Android Open Source Project10592532009-03-18 17:39:46 -07001559 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001560 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001561 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001562
Yohei Yukawa33e81792015-11-17 21:14:42 -08001563 void resetCurrentMethodAndClient(
1564 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001565 mCurMethodId = null;
1566 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001567 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001568 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001569
1570 void requestClientSessionLocked(ClientState cs) {
1571 if (!cs.sessionRequested) {
1572 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1573 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1574 cs.sessionRequested = true;
1575 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1576 MSG_CREATE_SESSION, mCurMethod, channels[1],
1577 new MethodCallback(this, mCurMethod, channels[0])));
1578 }
1579 }
1580
1581 void clearClientSessionLocked(ClientState cs) {
1582 finishSessionLocked(cs.curSession);
1583 cs.curSession = null;
1584 cs.sessionRequested = false;
1585 }
1586
1587 private void finishSessionLocked(SessionState sessionState) {
1588 if (sessionState != null) {
1589 if (sessionState.session != null) {
1590 try {
1591 sessionState.session.finishSession();
1592 } catch (RemoteException e) {
1593 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001594 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001595 }
1596 sessionState.session = null;
1597 }
1598 if (sessionState.channel != null) {
1599 sessionState.channel.dispose();
1600 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001601 }
1602 }
1603 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001604
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001605 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 if (mCurMethod != null) {
1607 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001608 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001610
Jeff Brownc28867a2013-03-26 15:42:39 -07001611 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001612 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 mCurMethod = null;
1614 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001615 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001616 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001619
satoke7c6998e2011-06-03 17:57:59 +09001620 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 public void onServiceDisconnected(ComponentName name) {
1622 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001623 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 + " mCurIntent=" + mCurIntent);
1625 if (mCurMethod != null && mCurIntent != null
1626 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001627 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 // We consider this to be a new bind attempt, since the system
1629 // should now try to restart the service for us.
1630 mLastBindTime = SystemClock.uptimeMillis();
1631 mShowRequested = mInputShown;
1632 mInputShown = false;
1633 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001634 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1635 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1636 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638 }
1639 }
1640 }
1641
satokf9f01002011-05-19 21:31:50 +09001642 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1644 long ident = Binder.clearCallingIdentity();
1645 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001647 if (!calledWithValidToken(token)) {
1648 final int uid = Binder.getCallingUid();
1649 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1650 + " token:" + token);
1651 return;
1652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001654 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001655 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001656 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001659 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001660 CharSequence contentDescription = null;
1661 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001662 // Use PackageManager to load label
1663 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001664 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001665 mIPackageManager.getApplicationInfo(packageName, 0,
1666 mSettings.getCurrentUserId()));
1667 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001668 /* ignore */
1669 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001670 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001671 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001672 contentDescription != null
1673 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001674 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
1677 }
1678 } finally {
1679 Binder.restoreCallingIdentity(ident);
1680 }
1681 }
1682
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001683 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001684 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001685 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001686 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001687 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001688 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001689 // When physical keyboard is attached, we show the ime switcher (or notification if
1690 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1691 // exists in the IME switcher dialog. Might be OK to remove this condition once
1692 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1693 return true;
1694 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001695 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001696
1697 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1698 final int N = imis.size();
1699 if (N > 2) return true;
1700 if (N < 1) return false;
1701 int nonAuxCount = 0;
1702 int auxCount = 0;
1703 InputMethodSubtype nonAuxSubtype = null;
1704 InputMethodSubtype auxSubtype = null;
1705 for(int i = 0; i < N; ++i) {
1706 final InputMethodInfo imi = imis.get(i);
1707 final List<InputMethodSubtype> subtypes =
1708 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1709 final int subtypeCount = subtypes.size();
1710 if (subtypeCount == 0) {
1711 ++nonAuxCount;
1712 } else {
1713 for (int j = 0; j < subtypeCount; ++j) {
1714 final InputMethodSubtype subtype = subtypes.get(j);
1715 if (!subtype.isAuxiliary()) {
1716 ++nonAuxCount;
1717 nonAuxSubtype = subtype;
1718 } else {
1719 ++auxCount;
1720 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001721 }
1722 }
satok7cfc0ed2011-06-20 21:29:36 +09001723 }
1724 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001725 if (nonAuxCount > 1 || auxCount > 1) {
1726 return true;
1727 } else if (nonAuxCount == 1 && auxCount == 1) {
1728 if (nonAuxSubtype != null && auxSubtype != null
1729 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1730 || auxSubtype.overridesImplicitlyEnabledSubtype()
1731 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1732 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1733 return false;
1734 }
1735 return true;
1736 }
1737 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001738 }
1739
John Spurlocke0980502013-10-25 11:59:29 -04001740 private boolean isKeyguardLocked() {
1741 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1742 }
1743
satokdbf29502011-08-25 15:28:23 +09001744 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001745 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001746 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001747 if (!calledWithValidToken(token)) {
1748 final int uid = Binder.getCallingUid();
1749 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1750 + " token:" + token);
1751 return;
1752 }
1753
1754 synchronized (mMethodMap) {
1755 mImeWindowVis = vis;
1756 mBackDisposition = backDisposition;
1757 updateSystemUiLocked(token, vis, backDisposition);
1758 }
1759 }
1760
1761 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1762 synchronized (mMethodMap) {
1763 updateSystemUiLocked(token, vis, backDisposition);
1764 }
1765 }
1766
1767 // Caution! This method is called in this class. Handle multi-user carefully
1768 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1769 if (!calledWithValidToken(token)) {
1770 final int uid = Binder.getCallingUid();
1771 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1772 + " token:" + token);
1773 return;
1774 }
1775
1776 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1777 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001778 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001779 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001780 // apply policy for binder calls
1781 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1782 vis = 0;
satok06487a52010-10-29 11:37:18 +09001783 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001784 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1785 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1786 if (mStatusBar != null) {
1787 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1788 needsToShowImeSwitcher);
1789 }
1790 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1791 if (imi != null && needsToShowImeSwitcher) {
1792 // Used to load label
1793 final CharSequence title = mRes.getText(
1794 com.android.internal.R.string.select_input_method);
1795 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1796 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001797 mImeSwitcherNotification.setContentTitle(title)
1798 .setContentText(summary)
1799 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001800 try {
1801 if ((mNotificationManager != null)
1802 && !mIWindowManager.hasNavigationBar()) {
1803 if (DEBUG) {
1804 Slog.d(TAG, "--- show notification: label = " + summary);
1805 }
1806 mNotificationManager.notifyAsUser(null,
1807 com.android.internal.R.string.select_input_method,
1808 mImeSwitcherNotification.build(), UserHandle.ALL);
1809 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001810 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001811 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001812 }
1813 } else {
1814 if (mNotificationShown && mNotificationManager != null) {
1815 if (DEBUG) {
1816 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001817 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001818 mNotificationManager.cancelAsUser(null,
1819 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1820 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001821 }
satok06487a52010-10-29 11:37:18 +09001822 }
1823 } finally {
1824 Binder.restoreCallingIdentity(ident);
1825 }
1826 }
1827
satoke7c6998e2011-06-03 17:57:59 +09001828 @Override
satokf9f01002011-05-19 21:31:50 +09001829 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001830 if (!calledFromValidUser()) {
1831 return;
1832 }
satokf9f01002011-05-19 21:31:50 +09001833 synchronized (mMethodMap) {
1834 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1835 for (int i = 0; i < spans.length; ++i) {
1836 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001837 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001838 mSecureSuggestionSpans.put(ss, currentImi);
1839 }
1840 }
1841 }
1842 }
1843
satoke7c6998e2011-06-03 17:57:59 +09001844 @Override
satokf9f01002011-05-19 21:31:50 +09001845 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001846 if (!calledFromValidUser()) {
1847 return false;
1848 }
satokf9f01002011-05-19 21:31:50 +09001849 synchronized (mMethodMap) {
1850 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1851 // TODO: Do not send the intent if the process of the targetImi is already dead.
1852 if (targetImi != null) {
1853 final String[] suggestions = span.getSuggestions();
1854 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001855 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001856 final Intent intent = new Intent();
1857 // Ensures that only a class in the original IME package will receive the
1858 // notification.
satok42c5a162011-05-26 16:46:14 +09001859 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001860 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1861 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1862 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1863 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001864 final long ident = Binder.clearCallingIdentity();
1865 try {
1866 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1867 } finally {
1868 Binder.restoreCallingIdentity(ident);
1869 }
satokf9f01002011-05-19 21:31:50 +09001870 return true;
1871 }
1872 }
1873 return false;
1874 }
1875
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001876 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001877 updateInputMethodsFromSettingsLocked(enabledMayChange);
1878 updateKeyboardFromSettingsLocked();
1879 }
1880
1881 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001882 if (enabledMayChange) {
1883 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1884 for (int i=0; i<enabled.size(); i++) {
1885 // We allow the user to select "disabled until used" apps, so if they
1886 // are enabling one of those here we now need to make it enabled.
1887 InputMethodInfo imm = enabled.get(i);
1888 try {
1889 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1890 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1891 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001892 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001893 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001894 if (DEBUG) {
1895 Slog.d(TAG, "Update state(" + imm.getId()
1896 + "): DISABLED_UNTIL_USED -> DEFAULT");
1897 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001898 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1899 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001900 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1901 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001902 }
1903 } catch (RemoteException e) {
1904 }
1905 }
1906 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001907 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1908 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1909 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1910 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001911 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001912 // There is no input method selected, try to choose new applicable input method.
1913 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001914 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001915 }
1916 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001918 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001920 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001921 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
satokf3db1af2010-11-23 13:34:33 +09001923 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001924 } else {
1925 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001926 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001928 // Here is not the perfect place to reset the switching controller. Ideally
1929 // mSwitchingController and mSettings should be able to share the same state.
1930 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1931 // the same enabled IMEs list.
1932 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001933
1934 }
1935
1936 public void updateKeyboardFromSettingsLocked() {
1937 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1938 if (mSwitchingDialog != null
1939 && mSwitchingDialogTitleView != null
1940 && mSwitchingDialog.isShowing()) {
1941 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1942 com.android.internal.R.id.hard_keyboard_switch);
1943 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001946
Yohei Yukawab097b822015-12-01 10:43:08 -08001947 private void notifyInputMethodSubtypeChanged(final int userId,
1948 @Nullable final InputMethodInfo inputMethodInfo,
1949 @Nullable final InputMethodSubtype subtype) {
1950 final InputManagerInternal inputManagerInternal =
1951 LocalServices.getService(InputManagerInternal.class);
1952 if (inputManagerInternal != null) {
1953 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1954 }
1955 }
1956
satokab751aa2010-09-14 19:17:36 +09001957 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 InputMethodInfo info = mMethodMap.get(id);
1959 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001960 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001962
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001963 if (mCurClient != null && mCurAttribute != null) {
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001964 // We have already made sure that the package name belongs to the application's UID.
1965 // No further UID check is required.
1966 if (SystemConfig.getInstance().getFixedImeApps().contains(mCurAttribute.packageName)) {
1967 return;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +09001968 }
1969 }
1970
satokd81e9502012-05-21 12:58:45 +09001971 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001973 final int subtypeCount = info.getSubtypeCount();
1974 if (subtypeCount <= 0) {
1975 return;
satokcd7cd292010-11-20 15:46:23 +09001976 }
satokd81e9502012-05-21 12:58:45 +09001977 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1978 final InputMethodSubtype newSubtype;
1979 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1980 newSubtype = info.getSubtypeAt(subtypeId);
1981 } else {
1982 // If subtype is null, try to find the most applicable one from
1983 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001984 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001985 }
1986 if (newSubtype == null || oldSubtype == null) {
1987 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1988 + ", new subtype = " + newSubtype);
1989 return;
1990 }
1991 if (newSubtype != oldSubtype) {
1992 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1993 if (mCurMethod != null) {
1994 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001995 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001996 mCurMethod.changeInputMethodSubtype(newSubtype);
1997 } catch (RemoteException e) {
1998 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08001999 return;
satokab751aa2010-09-14 19:17:36 +09002000 }
2001 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002002 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09002003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 return;
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
satokd81e9502012-05-21 12:58:45 +09002007 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 final long ident = Binder.clearCallingIdentity();
2009 try {
satokab751aa2010-09-14 19:17:36 +09002010 // Set a subtype to this input method.
2011 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002012 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2013 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2014 // because mCurMethodId is stored as a history in
2015 // setSelectedInputMethodAndSubtypeLocked().
2016 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017
2018 if (ActivityManagerNative.isSystemReady()) {
2019 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002020 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002022 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002024 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 } finally {
2026 Binder.restoreCallingIdentity(ident);
2027 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002028
2029 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2030 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002032
satok42c5a162011-05-26 16:46:14 +09002033 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002034 public boolean showSoftInput(IInputMethodClient client, int flags,
2035 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002036 if (!calledFromValidUser()) {
2037 return false;
2038 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002039 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 long ident = Binder.clearCallingIdentity();
2041 try {
2042 synchronized (mMethodMap) {
2043 if (mCurClient == null || client == null
2044 || mCurClient.client.asBinder() != client.asBinder()) {
2045 try {
2046 // We need to check if this is the current client with
2047 // focus in the window manager, to allow this call to
2048 // be made before input is started in it.
2049 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002050 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002054 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 }
2056 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002057
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002058 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002059 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061 } finally {
2062 Binder.restoreCallingIdentity(ident);
2063 }
2064 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002065
The Android Open Source Project4df24232009-03-05 14:34:35 -08002066 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002068 if (mAccessibilityRequestingNoSoftKeyboard) {
2069 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
Anna Galusza9b278112016-01-04 11:37:37 -08002071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2073 mShowExplicitlyRequested = true;
2074 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002075 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2076 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002078
Dianne Hackborncc278702009-09-02 23:07:23 -07002079 if (!mSystemReady) {
2080 return false;
2081 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002082
The Android Open Source Project4df24232009-03-05 14:34:35 -08002083 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002085 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002086 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2087 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2088 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002090 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002091 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002092 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002093 | Context.BIND_TREAT_LIKE_ACTIVITY
2094 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002095 mVisibleBound = true;
2096 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002097 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002099 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 // The client has asked to have the input method shown, but
2101 // we have been sitting here too long with a connection to the
2102 // service and no interface received, so let's disconnect/connect
2103 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002104 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002106 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002108 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002109 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002110 } else {
2111 if (DEBUG) {
2112 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2113 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002116
The Android Open Source Project4df24232009-03-05 14:34:35 -08002117 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002119
satok42c5a162011-05-26 16:46:14 +09002120 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002121 public boolean hideSoftInput(IInputMethodClient client, int flags,
2122 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002123 if (!calledFromValidUser()) {
2124 return false;
2125 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002126 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 long ident = Binder.clearCallingIdentity();
2128 try {
2129 synchronized (mMethodMap) {
2130 if (mCurClient == null || client == null
2131 || mCurClient.client.asBinder() != client.asBinder()) {
2132 try {
2133 // We need to check if this is the current client with
2134 // focus in the window manager, to allow this call to
2135 // be made before input is started in it.
2136 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002137 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2138 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002139 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002142 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002145
Joe Onorato8a9b2202010-02-26 18:56:32 -08002146 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002147 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149 } finally {
2150 Binder.restoreCallingIdentity(ident);
2151 }
2152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
The Android Open Source Project4df24232009-03-05 14:34:35 -08002154 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2156 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002157 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 -08002158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
2160 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002161 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 -08002162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002164
2165 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2166 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2167 // to be updated with the new value sent from IME process. Even in such a transient state
2168 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2169 // application process as a valid request, and have even promised such a behavior with CTS
2170 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2171 // IMMS#InputShown indicates that the software keyboard is shown.
2172 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2173 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2174 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002175 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002176 if (shouldHideSoftInput) {
2177 // The IME will report its visible state again after the following message finally
2178 // delivered to the IME process as an IPC. Hence the inconsistency between
2179 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2180 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002181 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2182 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2183 res = true;
2184 } else {
2185 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002187 if (mHaveConnection && mVisibleBound) {
2188 mContext.unbindService(mVisibleConnection);
2189 mVisibleBound = false;
2190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 mInputShown = false;
2192 mShowRequested = false;
2193 mShowExplicitlyRequested = false;
2194 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002195 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002197
satok42c5a162011-05-26 16:46:14 +09002198 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002199 public InputBindResult startInputOrWindowGainedFocus(
2200 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2201 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2202 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
2203 if (windowToken != null) {
2204 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
2205 softInputMode, windowFlags, attribute, inputContext);
2206 } else {
2207 return startInput(startInputReason, client, inputContext, attribute, controlFlags);
2208 }
2209 }
2210
2211 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002212 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2213 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2214 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002215 // Needs to check the validity before clearing calling identity
2216 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002217 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 long ident = Binder.clearCallingIdentity();
2219 try {
2220 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002221 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2222 + InputMethodClient.getStartInputReason(startInputReason)
2223 + " client=" + client.asBinder()
2224 + " inputContext=" + inputContext
2225 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002226 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002228 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002229
Dianne Hackborn7663d802012-02-24 13:08:49 -08002230 ClientState cs = mClients.get(client.asBinder());
2231 if (cs == null) {
2232 throw new IllegalArgumentException("unknown client "
2233 + client.asBinder());
2234 }
2235
2236 try {
2237 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2238 // Check with the window manager to make sure this client actually
2239 // has a window with focus. If not, reject. This is thread safe
2240 // because if the focus changes some time before or after, the
2241 // next client receiving focus that has any interest in input will
2242 // be calling through here after that change happens.
2243 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2244 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2245 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002247 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002249
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002250 if (!calledFromValidUser) {
2251 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2252 Slog.w(TAG, "If you want to interect with IME, you need "
2253 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2254 hideCurrentInputLocked(0, null);
2255 return null;
2256 }
2257
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002258 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002259 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002260 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002261 if (attribute != null) {
2262 return startInputUncheckedLocked(cs, inputContext, attribute,
2263 controlFlags);
2264 }
2265 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002266 }
2267 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002268 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002269
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002270 // Should we auto-show the IME even if the caller has not
2271 // specified what should be done with it?
2272 // We only do this automatically if the window can resize
2273 // to accommodate the IME (so what the user sees will give
2274 // them good context without input information being obscured
2275 // by the IME) or if running on a large screen where there
2276 // is more room for the target window + IME.
2277 final boolean doAutoShow =
2278 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2279 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2280 || mRes.getConfiguration().isLayoutSizeAtLeast(
2281 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002282 final boolean isTextEditor =
2283 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2284
2285 // We want to start input before showing the IME, but after closing
2286 // it. We want to do this after closing it to help the IME disappear
2287 // more quickly (not get stuck behind it initializing itself for the
2288 // new focused input, even if its window wants to hide the IME).
2289 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2292 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002293 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2295 // There is no focus view, and this window will
2296 // be behind any soft input window, so hide the
2297 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002298 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002299 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002301 } else if (isTextEditor && doAutoShow && (softInputMode &
2302 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 // There is a focus view, and we are navigating forward
2304 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002305 // We only do this automatically if the window can resize
2306 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002307 // them good context without input information being obscured
2308 // by the IME) or if running on a large screen where there
2309 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002310 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002311 if (attribute != null) {
2312 res = startInputUncheckedLocked(cs, inputContext, attribute,
2313 controlFlags);
2314 didStart = true;
2315 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002316 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 }
2318 break;
2319 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2320 // Do nothing.
2321 break;
2322 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2323 if ((softInputMode &
2324 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002325 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002326 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
2328 break;
2329 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002330 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002331 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 break;
2333 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2334 if ((softInputMode &
2335 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002336 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002337 if (attribute != null) {
2338 res = startInputUncheckedLocked(cs, inputContext, attribute,
2339 controlFlags);
2340 didStart = true;
2341 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002342 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344 break;
2345 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002346 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002347 if (attribute != null) {
2348 res = startInputUncheckedLocked(cs, inputContext, attribute,
2349 controlFlags);
2350 didStart = true;
2351 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002352 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 break;
2354 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002355
2356 if (!didStart && attribute != null) {
2357 res = startInputUncheckedLocked(cs, inputContext, attribute,
2358 controlFlags);
2359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
2361 } finally {
2362 Binder.restoreCallingIdentity(ident);
2363 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002364
2365 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
satok42c5a162011-05-26 16:46:14 +09002368 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002369 public void showInputMethodPickerFromClient(
2370 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002371 if (!calledFromValidUser()) {
2372 return;
2373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 synchronized (mMethodMap) {
2375 if (mCurClient == null || client == null
2376 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002377 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002378 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 }
2380
satok440aab52010-11-25 09:43:11 +09002381 // Always call subtype picker, because subtype picker is a superset of input method
2382 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002383 mHandler.sendMessage(mCaller.obtainMessageI(
2384 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002385 }
2386 }
2387
satok42c5a162011-05-26 16:46:14 +09002388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002390 if (!calledFromValidUser()) {
2391 return;
2392 }
satok28203512010-11-24 11:06:49 +09002393 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2394 }
2395
satok42c5a162011-05-26 16:46:14 +09002396 @Override
satok28203512010-11-24 11:06:49 +09002397 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002398 if (!calledFromValidUser()) {
2399 return;
2400 }
satok28203512010-11-24 11:06:49 +09002401 synchronized (mMethodMap) {
2402 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002403 setInputMethodWithSubtypeIdLocked(token, id,
2404 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2405 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002406 } else {
2407 setInputMethod(token, id);
2408 }
2409 }
satokab751aa2010-09-14 19:17:36 +09002410 }
2411
satok42c5a162011-05-26 16:46:14 +09002412 @Override
satokb416a712010-11-25 20:42:14 +09002413 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002414 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002415 if (!calledFromValidUser()) {
2416 return;
2417 }
satokb416a712010-11-25 20:42:14 +09002418 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002419 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2420 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002421 }
2422 }
2423
satok4fc87d62011-05-20 16:13:43 +09002424 @Override
satok735cf382010-11-11 20:40:09 +09002425 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002426 if (!calledFromValidUser()) {
2427 return false;
2428 }
satok735cf382010-11-11 20:40:09 +09002429 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002430 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002431 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002432 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002433 lastImi = mMethodMap.get(lastIme.first);
2434 } else {
2435 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002436 }
satok4fc87d62011-05-20 16:13:43 +09002437 String targetLastImiId = null;
2438 int subtypeId = NOT_A_SUBTYPE_ID;
2439 if (lastIme != null && lastImi != null) {
2440 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2441 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2442 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2443 : mCurrentSubtype.hashCode();
2444 // If the last IME is the same as the current IME and the last subtype is not
2445 // defined, there is no need to switch to the last IME.
2446 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2447 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002448 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002449 }
2450 }
2451
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002452 if (TextUtils.isEmpty(targetLastImiId)
2453 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002454 // This is a safety net. If the currentSubtype can't be added to the history
2455 // and the framework couldn't find the last ime, we will make the last ime be
2456 // the most applicable enabled keyboard subtype of the system imes.
2457 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2458 if (enabled != null) {
2459 final int N = enabled.size();
2460 final String locale = mCurrentSubtype == null
2461 ? mRes.getConfiguration().locale.toString()
2462 : mCurrentSubtype.getLocale();
2463 for (int i = 0; i < N; ++i) {
2464 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002465 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002466 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002467 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2468 InputMethodUtils.getSubtypes(imi),
2469 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002470 if (keyboardSubtype != null) {
2471 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002472 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002473 imi, keyboardSubtype.hashCode());
2474 if(keyboardSubtype.getLocale().equals(locale)) {
2475 break;
2476 }
2477 }
2478 }
2479 }
2480 }
2481 }
2482
2483 if (!TextUtils.isEmpty(targetLastImiId)) {
2484 if (DEBUG) {
2485 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2486 + ", from: " + mCurMethodId + ", " + subtypeId);
2487 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002488 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002489 return true;
2490 } else {
2491 return false;
2492 }
satok735cf382010-11-11 20:40:09 +09002493 }
2494 }
2495
satoke7c6998e2011-06-03 17:57:59 +09002496 @Override
satok688bd472012-02-09 20:09:17 +09002497 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002498 if (!calledFromValidUser()) {
2499 return false;
2500 }
satok688bd472012-02-09 20:09:17 +09002501 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002502 if (!calledWithValidToken(token)) {
2503 final int uid = Binder.getCallingUid();
2504 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2505 + " token:" + token);
2506 return false;
2507 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002508 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002509 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2510 if (nextSubtype == null) {
2511 return false;
2512 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002513 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2514 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002515 return true;
2516 }
2517 }
2518
2519 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002520 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2521 if (!calledFromValidUser()) {
2522 return false;
2523 }
2524 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002525 if (!calledWithValidToken(token)) {
2526 final int uid = Binder.getCallingUid();
2527 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2528 + "token. uid:" + uid + " token:" + token);
2529 return false;
2530 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002531 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002532 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2533 if (nextSubtype == null) {
2534 return false;
2535 }
2536 return true;
2537 }
2538 }
2539
2540 @Override
satok68f1b782011-04-11 14:26:04 +09002541 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002542 if (!calledFromValidUser()) {
2543 return null;
2544 }
satok68f1b782011-04-11 14:26:04 +09002545 synchronized (mMethodMap) {
2546 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2547 // TODO: Handle the case of the last IME with no subtypes
2548 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2549 || TextUtils.isEmpty(lastIme.second)) return null;
2550 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2551 if (lastImi == null) return null;
2552 try {
2553 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002554 final int lastSubtypeId =
2555 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002556 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2557 return null;
2558 }
2559 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002560 } catch (NumberFormatException e) {
2561 return null;
2562 }
2563 }
2564 }
2565
satoke7c6998e2011-06-03 17:57:59 +09002566 @Override
satokee5e77c2011-09-02 18:50:15 +09002567 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002568 if (!calledFromValidUser()) {
2569 return;
2570 }
satok91e88122011-07-18 11:11:42 +09002571 // By this IPC call, only a process which shares the same uid with the IME can add
2572 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002573 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002574 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002575 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002576 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002577 final String[] packageInfos;
2578 try {
2579 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2580 } catch (RemoteException e) {
2581 Slog.e(TAG, "Failed to get package infos");
2582 return;
2583 }
satok91e88122011-07-18 11:11:42 +09002584 if (packageInfos != null) {
2585 final int packageNum = packageInfos.length;
2586 for (int i = 0; i < packageNum; ++i) {
2587 if (packageInfos[i].equals(imi.getPackageName())) {
2588 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002589 final long ident = Binder.clearCallingIdentity();
2590 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002591 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002592 } finally {
2593 Binder.restoreCallingIdentity(ident);
2594 }
satokee5e77c2011-09-02 18:50:15 +09002595 return;
satok91e88122011-07-18 11:11:42 +09002596 }
2597 }
2598 }
satoke7c6998e2011-06-03 17:57:59 +09002599 }
satokee5e77c2011-09-02 18:50:15 +09002600 return;
satoke7c6998e2011-06-03 17:57:59 +09002601 }
2602
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002603 @Override
2604 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002605 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002606 }
2607
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002608 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002609 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002610 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002611 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002612 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002613 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002614 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2615 if (DEBUG) {
2616 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2617 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2618 + " actual: " + sequenceNumber);
2619 }
2620 return;
2621 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002622 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2623 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002624 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002625 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002626 }
2627 }
2628
satok28203512010-11-24 11:06:49 +09002629 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002631 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2632 }
2633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002635 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2636 if (token == null) {
2637 if (mContext.checkCallingOrSelfPermission(
2638 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2639 != PackageManager.PERMISSION_GRANTED) {
2640 throw new SecurityException(
2641 "Using null token requires permission "
2642 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002644 } else if (mCurToken != token) {
2645 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2646 + " token: " + token);
2647 return;
2648 }
2649
2650 final long ident = Binder.clearCallingIdentity();
2651 try {
2652 setInputMethodLocked(id, subtypeId);
2653 } finally {
2654 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
2656 }
2657
satok42c5a162011-05-26 16:46:14 +09002658 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002660 if (!calledFromValidUser()) {
2661 return;
2662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002664 if (!calledWithValidToken(token)) {
2665 final int uid = Binder.getCallingUid();
2666 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2667 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 return;
2669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 long ident = Binder.clearCallingIdentity();
2671 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002672 hideCurrentInputLocked(flags, null);
2673 } finally {
2674 Binder.restoreCallingIdentity(ident);
2675 }
2676 }
2677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002678
satok42c5a162011-05-26 16:46:14 +09002679 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002680 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002681 if (!calledFromValidUser()) {
2682 return;
2683 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002684 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002685 if (!calledWithValidToken(token)) {
2686 final int uid = Binder.getCallingUid();
2687 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2688 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002689 return;
2690 }
2691 long ident = Binder.clearCallingIdentity();
2692 try {
2693 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 } finally {
2695 Binder.restoreCallingIdentity(ident);
2696 }
2697 }
2698 }
2699
2700 void setEnabledSessionInMainThread(SessionState session) {
2701 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002702 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002704 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002705 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 } catch (RemoteException e) {
2707 }
2708 }
2709 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002710 if (mEnabledSession != null && mEnabledSession.session != null) {
2711 try {
2712 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2713 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2714 } catch (RemoteException e) {
2715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
2717 }
2718 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002719
satok42c5a162011-05-26 16:46:14 +09002720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002722 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002724 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002725 final boolean showAuxSubtypes;
2726 switch (msg.arg1) {
2727 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2728 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2729 // implemented so that auxiliary subtypes will be excluded when the soft
2730 // keyboard is invisible.
2731 showAuxSubtypes = mInputShown;
2732 break;
2733 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2734 showAuxSubtypes = true;
2735 break;
2736 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2737 showAuxSubtypes = false;
2738 break;
2739 default:
2740 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2741 return false;
2742 }
2743 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002744 return true;
2745
satok47a44912010-10-06 16:03:58 +09002746 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002747 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002748 return true;
2749
2750 case MSG_SHOW_IM_CONFIG:
2751 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002752 return true;
2753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 case MSG_UNBIND_INPUT:
2757 try {
2758 ((IInputMethod)msg.obj).unbindInput();
2759 } catch (RemoteException e) {
2760 // There is nothing interesting about the method dying.
2761 }
2762 return true;
2763 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002764 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 try {
2766 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2767 } catch (RemoteException e) {
2768 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002769 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 return true;
2771 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002772 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002774 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002775 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002776 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 } catch (RemoteException e) {
2778 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002779 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 return true;
2781 case MSG_HIDE_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002782 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002784 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002785 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002786 ((IInputMethod)args.arg1).hideSoftInput(0, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 } catch (RemoteException e) {
2788 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002789 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 return true;
2791 case MSG_ATTACH_TOKEN:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002792 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002794 if (DEBUG) Slog.v(TAG, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2796 } catch (RemoteException e) {
2797 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002798 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002800 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002801 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002802 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002803 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002805 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002807 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002808 // Dispose the channel if the input method is not local to this process
2809 // because the remote proxy will get its own copy when unparceled.
2810 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002811 channel.dispose();
2812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002814 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002820 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 try {
2822 SessionState session = (SessionState)args.arg1;
2823 setEnabledSessionInMainThread(session);
2824 session.method.startInput((IInputContext)args.arg2,
2825 (EditorInfo)args.arg3);
2826 } catch (RemoteException e) {
2827 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002828 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 return true;
2830 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002831 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 try {
2833 SessionState session = (SessionState)args.arg1;
2834 setEnabledSessionInMainThread(session);
2835 session.method.restartInput((IInputContext)args.arg2,
2836 (EditorInfo)args.arg3);
2837 } catch (RemoteException e) {
2838 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002839 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002843
Yohei Yukawa33e81792015-11-17 21:14:42 -08002844 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002846 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 } catch (RemoteException e) {
2848 // There is nothing interesting about the last client dying.
2849 }
2850 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002851 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002852 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002853 IInputMethodClient client = (IInputMethodClient)args.arg1;
2854 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002856 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002858 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002859 } finally {
2860 // Dispose the channel if the input method is not local to this process
2861 // because the remote proxy will get its own copy when unparceled.
2862 if (res.channel != null && Binder.isProxy(client)) {
2863 res.channel.dispose();
2864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002866 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002868 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002869 case MSG_SET_ACTIVE:
2870 try {
2871 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2872 } catch (RemoteException e) {
2873 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2874 + ((ClientState)msg.obj).pid + " uid "
2875 + ((ClientState)msg.obj).uid);
2876 }
2877 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002878 case MSG_SET_INTERACTIVE:
2879 handleSetInteractive(msg.arg1 != 0);
2880 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002881 case MSG_SWITCH_IME:
2882 handleSwitchInputMethod(msg.arg1 != 0);
2883 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002884 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2885 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002886 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002887 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002888 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002889 } catch (RemoteException e) {
2890 Slog.w(TAG, "Got RemoteException sending "
2891 + "setUserActionNotificationSequenceNumber("
2892 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002893 + clientState.pid + " uid "
2894 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002895 }
2896 return true;
2897 }
satok01038492012-04-09 21:08:27 +09002898
2899 // --------------------------------------------------------------
2900 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002901 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002902 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 }
2904 return false;
2905 }
2906
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002907 private void handleSetInteractive(final boolean interactive) {
2908 synchronized (mMethodMap) {
2909 mIsInteractive = interactive;
2910 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2911
2912 // Inform the current client of the change in active status
2913 if (mCurClient != null && mCurClient.client != null) {
2914 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2915 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2916 }
2917 }
2918 }
2919
Yohei Yukawaae61f712015-12-09 13:00:10 -08002920 private void handleSwitchInputMethod(final boolean forwardDirection) {
2921 synchronized (mMethodMap) {
2922 // TODO: Support forwardDirection.
2923 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2924 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2925 if (nextSubtype == null) {
2926 return;
2927 }
2928 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2929 }
2930 }
2931
satokdc9ddae2011-10-06 12:22:36 +09002932 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002933 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2934 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002935 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002936 if (DEBUG) {
2937 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2938 }
satok723a27e2010-11-11 14:58:11 +09002939 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002940 return true;
2941 }
2942
2943 return false;
2944 }
2945
Yohei Yukawa94e33302016-02-12 19:37:03 -08002946 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002947 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002948 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002949 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002950 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002951 mMethodList.clear();
2952 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002953
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002954 // Use for queryIntentServicesAsUser
2955 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956
Yohei Yukawaed4952a2016-02-17 07:57:25 -08002957 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
2958 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
2959 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002960 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002962 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2963 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002964
satoke7c6998e2011-06-03 17:57:59 +09002965 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2966 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 for (int i = 0; i < services.size(); ++i) {
2968 ResolveInfo ri = services.get(i);
2969 ServiceInfo si = ri.serviceInfo;
2970 ComponentName compName = new ComponentName(si.packageName, si.name);
2971 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2972 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002973 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 + ": it does not require the permission "
2975 + android.Manifest.permission.BIND_INPUT_METHOD);
2976 continue;
2977 }
2978
Joe Onorato8a9b2202010-02-26 18:56:32 -08002979 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980
2981 try {
satoke7c6998e2011-06-03 17:57:59 +09002982 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08002983 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002984 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08002985 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
2987 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002988 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002990 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002991 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 }
2993 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002994
Yohei Yukawa859df052016-02-17 07:56:46 -08002995 // TODO: The following code should find better place to live.
2996 if (!resetDefaultEnabledIme) {
2997 boolean enabledImeFound = false;
2998 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
2999 final int N = enabledImes.size();
3000 for (int i = 0; i < N; ++i) {
3001 final InputMethodInfo imi = enabledImes.get(i);
3002 if (mMethodList.contains(imi)) {
3003 enabledImeFound = true;
3004 break;
3005 }
3006 }
3007 if (!enabledImeFound) {
3008 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
3009 resetDefaultEnabledIme = true;
3010 resetSelectedInputMethodAndSubtypeLocked("");
3011 }
3012 }
3013
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003014 if (resetDefaultEnabledIme) {
3015 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003016 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003017 final int N = defaultEnabledIme.size();
3018 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003019 final InputMethodInfo imi = defaultEnabledIme.get(i);
3020 if (DEBUG) {
3021 Slog.d(TAG, "--- enable ime = " + imi);
3022 }
3023 setInputMethodEnabledLocked(imi.getId(), true);
3024 }
3025 }
3026
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003027 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003028 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003029 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003030 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3031 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003032 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003033 }
3034 } else {
3035 // Double check that the default IME is certainly enabled.
3036 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003037 }
3038 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003039 // Here is not the perfect place to reset the switching controller. Ideally
3040 // mSwitchingController and mSettings should be able to share the same state.
3041 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3042 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003043 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003047
satok217f5482010-12-15 05:19:19 +09003048 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003049 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003050 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003051 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3052 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003053 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003054 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003055 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003056 final int userId;
3057 synchronized (mMethodMap) {
3058 userId = mSettings.getCurrentUserId();
3059 }
3060 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003061 }
3062
3063 private void showConfigureInputMethods() {
3064 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3065 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3066 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3067 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003068 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003069 }
3070
satok2c93efc2012-04-02 19:33:47 +09003071 private boolean isScreenLocked() {
3072 return mKeyguardManager != null
3073 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3074 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003075
Seigo Nonaka14e13912015-05-06 21:04:13 -07003076 private void showInputMethodMenu(boolean showAuxSubtypes) {
3077 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003080 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003081
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003082 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003083 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003084 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003085
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003086 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003087 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003088 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3089 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003090 if (immis == null || immis.size() == 0) {
3091 return;
3092 }
3093
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003094 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095
satok688bd472012-02-09 20:09:17 +09003096 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003097 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003098 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003099
satokc3690562012-01-10 20:14:43 +09003100 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003101 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003102 if (currentSubtype != null) {
3103 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003104 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3105 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003106 }
3107 }
3108
Ken Wakasa761eb372011-03-04 19:06:18 +09003109 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003110 mIms = new InputMethodInfo[N];
3111 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003112 int checkedItem = 0;
3113 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003114 final ImeSubtypeListItem item = imList.get(i);
3115 mIms[i] = item.mImi;
3116 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003117 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003118 int subtypeId = mSubtypeIds[i];
3119 if ((subtypeId == NOT_A_SUBTYPE_ID)
3120 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3121 || (subtypeId == lastInputMethodSubtypeId)) {
3122 checkedItem = i;
3123 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003126
3127 final Context settingsContext = new ContextThemeWrapper(context,
3128 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3129
3130 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003131 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3132 @Override
3133 public void onCancel(DialogInterface dialog) {
3134 hideInputMethodMenu();
3135 }
3136 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003137
3138 final Context dialogContext = mDialogBuilder.getContext();
3139 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3140 com.android.internal.R.styleable.DialogPreference,
3141 com.android.internal.R.attr.alertDialogStyle, 0);
3142 final Drawable dialogIcon = a.getDrawable(
3143 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3144 a.recycle();
3145
3146 mDialogBuilder.setIcon(dialogIcon);
3147
Yohei Yukawad34e1482016-02-11 08:03:52 -08003148 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003149 final View tv = inflater.inflate(
3150 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3151 mDialogBuilder.setCustomTitle(tv);
3152
3153 // Setup layout for a toggle switch of the hardware keyboard
3154 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003155 mSwitchingDialogTitleView
3156 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003157 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003158 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003159 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003160 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003161 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003162 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3163 @Override
3164 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003165 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003166 // Ensure that the input method dialog is dismissed when changing
3167 // the hardware keyboard state.
3168 hideInputMethodMenu();
3169 }
3170 });
3171
Alan Viverette505e3ab2014-11-24 15:22:11 -08003172 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003173 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3174 final OnClickListener choiceListener = new OnClickListener() {
3175 @Override
3176 public void onClick(final DialogInterface dialog, final int which) {
3177 synchronized (mMethodMap) {
3178 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3179 || mSubtypeIds.length <= which) {
3180 return;
satok01038492012-04-09 21:08:27 +09003181 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003182 final InputMethodInfo im = mIms[which];
3183 int subtypeId = mSubtypeIds[which];
3184 adapter.mCheckedItem = which;
3185 adapter.notifyDataSetChanged();
3186 hideInputMethodMenu();
3187 if (im != null) {
3188 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3189 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003190 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003191 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003193 }
3194 }
3195 };
3196 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003198 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003199 final OnClickListener positiveListener = new OnClickListener() {
3200 @Override
3201 public void onClick(DialogInterface dialog, int whichButton) {
3202 showConfigureInputMethods();
3203 }
3204 };
satok82beadf2010-12-27 19:03:06 +09003205 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003206 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003209 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 mSwitchingDialog.getWindow().setType(
3211 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003212 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3213 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003214 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003215 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 mSwitchingDialog.show();
3217 }
3218 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003219
Ken Wakasa05dbb652011-08-22 15:22:43 +09003220 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3221 private final LayoutInflater mInflater;
3222 private final int mTextViewResourceId;
3223 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003224 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003225 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3226 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3227 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003228
Ken Wakasa05dbb652011-08-22 15:22:43 +09003229 mTextViewResourceId = textViewResourceId;
3230 mItemsList = itemsList;
3231 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003232 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003233 }
3234
3235 @Override
3236 public View getView(int position, View convertView, ViewGroup parent) {
3237 final View view = convertView != null ? convertView
3238 : mInflater.inflate(mTextViewResourceId, null);
3239 if (position < 0 || position >= mItemsList.size()) return view;
3240 final ImeSubtypeListItem item = mItemsList.get(position);
3241 final CharSequence imeName = item.mImeName;
3242 final CharSequence subtypeName = item.mSubtypeName;
3243 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3244 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3245 if (TextUtils.isEmpty(subtypeName)) {
3246 firstTextView.setText(imeName);
3247 secondTextView.setVisibility(View.GONE);
3248 } else {
3249 firstTextView.setText(subtypeName);
3250 secondTextView.setText(imeName);
3251 secondTextView.setVisibility(View.VISIBLE);
3252 }
3253 final RadioButton radioButton =
3254 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3255 radioButton.setChecked(position == mCheckedItem);
3256 return view;
3257 }
3258 }
3259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003261 synchronized (mMethodMap) {
3262 hideInputMethodMenuLocked();
3263 }
3264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003265
The Android Open Source Project10592532009-03-18 17:39:46 -07003266 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003267 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268
The Android Open Source Project10592532009-03-18 17:39:46 -07003269 if (mSwitchingDialog != null) {
3270 mSwitchingDialog.dismiss();
3271 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003273
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003274 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003275 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003276 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003280
satok42c5a162011-05-26 16:46:14 +09003281 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003283 // TODO: Make this work even for non-current users?
3284 if (!calledFromValidUser()) {
3285 return false;
3286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 synchronized (mMethodMap) {
3288 if (mContext.checkCallingOrSelfPermission(
3289 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3290 != PackageManager.PERMISSION_GRANTED) {
3291 throw new SecurityException(
3292 "Requires permission "
3293 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3294 }
Anna Galusza9b278112016-01-04 11:37:37 -08003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 long ident = Binder.clearCallingIdentity();
3297 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003298 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 } finally {
3300 Binder.restoreCallingIdentity(ident);
3301 }
3302 }
3303 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003304
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003305 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3306 // Make sure this is a valid input method.
3307 InputMethodInfo imm = mMethodMap.get(id);
3308 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003309 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003310 }
3311
satokd87c2592010-09-29 11:52:06 +09003312 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3313 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003314
satokd87c2592010-09-29 11:52:06 +09003315 if (enabled) {
3316 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3317 if (pair.first.equals(id)) {
3318 // We are enabling this input method, but it is already enabled.
3319 // Nothing to do. The previous state was enabled.
3320 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003321 }
3322 }
satokd87c2592010-09-29 11:52:06 +09003323 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3324 // Previous state was disabled.
3325 return false;
3326 } else {
3327 StringBuilder builder = new StringBuilder();
3328 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3329 builder, enabledInputMethodsList, id)) {
3330 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003331 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003332 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3333 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3334 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003335 }
3336 // Previous state was enabled.
3337 return true;
3338 } else {
3339 // We are disabling the input method but it is already disabled.
3340 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003341 return false;
3342 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003343 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003344 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003345
satok723a27e2010-11-11 14:58:11 +09003346 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3347 boolean setSubtypeOnly) {
3348 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003349 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003350
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003351 mCurUserActionNotificationSequenceNumber =
3352 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3353 if (DEBUG) {
3354 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3355 + mCurUserActionNotificationSequenceNumber);
3356 }
3357
3358 if (mCurClient != null && mCurClient.client != null) {
3359 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3360 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003361 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003362 }
3363
satok723a27e2010-11-11 14:58:11 +09003364 // Set Subtype here
3365 if (imi == null || subtypeId < 0) {
3366 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003367 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003368 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003369 if (subtypeId < imi.getSubtypeCount()) {
3370 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3371 mSettings.putSelectedSubtype(subtype.hashCode());
3372 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003373 } else {
3374 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003375 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003376 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003377 }
satokab751aa2010-09-14 19:17:36 +09003378 }
satok723a27e2010-11-11 14:58:11 +09003379
Yohei Yukawa68645a62016-02-17 07:54:20 -08003380 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003381 // Set InputMethod here
3382 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3383 }
3384 }
3385
3386 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3387 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3388 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3389 // newDefaultIme is empty when there is no candidate for the selected IME.
3390 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3391 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3392 if (subtypeHashCode != null) {
3393 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003394 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003395 imi, Integer.valueOf(subtypeHashCode));
3396 } catch (NumberFormatException e) {
3397 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3398 }
3399 }
3400 }
3401 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003402 }
3403
satok4e4569d2010-11-19 18:45:53 +09003404 // If there are no selected shortcuts, tries finding the most applicable ones.
3405 private Pair<InputMethodInfo, InputMethodSubtype>
3406 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3407 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3408 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003409 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003410 boolean foundInSystemIME = false;
3411
3412 // Search applicable subtype for each InputMethodInfo
3413 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003414 final String imiId = imi.getId();
3415 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3416 continue;
3417 }
satokcd7cd292010-11-20 15:46:23 +09003418 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003419 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003420 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003421 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003422 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003424 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003425 }
satokdf31ae62011-01-15 06:19:44 +09003426 // 2. Search by the system locale from enabledSubtypes.
3427 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003428 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003429 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003430 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003431 }
satoka86f5e42011-09-02 17:12:42 +09003432 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003433 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003434 final ArrayList<InputMethodSubtype> subtypesForSearch =
3435 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003436 ? InputMethodUtils.getSubtypes(imi)
3437 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003438 // 4. Search by the current subtype's locale from all subtypes.
3439 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003440 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003441 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003442 }
3443 // 5. Search by the system locale from all subtypes.
3444 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003445 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003446 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003447 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003448 }
satokcd7cd292010-11-20 15:46:23 +09003449 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003450 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003451 // The current input method is the most applicable IME.
3452 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003453 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003454 break;
satok7599a7f2010-12-22 13:45:23 +09003455 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003456 // The system input method is 2nd applicable IME.
3457 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003458 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003459 if ((imi.getServiceInfo().applicationInfo.flags
3460 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3461 foundInSystemIME = true;
3462 }
satok4e4569d2010-11-19 18:45:53 +09003463 }
3464 }
3465 }
3466 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003467 if (mostApplicableIMI != null) {
3468 Slog.w(TAG, "Most applicable shortcut input method was:"
3469 + mostApplicableIMI.getId());
3470 if (mostApplicableSubtype != null) {
3471 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3472 + "," + mostApplicableSubtype.getMode() + ","
3473 + mostApplicableSubtype.getLocale());
3474 }
3475 }
satok4e4569d2010-11-19 18:45:53 +09003476 }
satokcd7cd292010-11-20 15:46:23 +09003477 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003478 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003479 } else {
3480 return null;
3481 }
3482 }
3483
satokab751aa2010-09-14 19:17:36 +09003484 /**
3485 * @return Return the current subtype of this input method.
3486 */
satok42c5a162011-05-26 16:46:14 +09003487 @Override
satokab751aa2010-09-14 19:17:36 +09003488 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003489 // TODO: Make this work even for non-current users?
3490 if (!calledFromValidUser()) {
3491 return null;
3492 }
3493 synchronized (mMethodMap) {
3494 return getCurrentInputMethodSubtypeLocked();
3495 }
3496 }
3497
3498 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003499 if (mCurMethodId == null) {
3500 return null;
3501 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003502 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003503 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3504 if (imi == null || imi.getSubtypeCount() == 0) {
3505 return null;
satok4e4569d2010-11-19 18:45:53 +09003506 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003507 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003508 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3509 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003510 if (subtypeId == NOT_A_SUBTYPE_ID) {
3511 // If there are no selected subtypes, the framework will try to find
3512 // the most applicable subtype from explicitly or implicitly enabled
3513 // subtypes.
3514 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003515 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003516 // If there is only one explicitly or implicitly enabled subtype,
3517 // just returns it.
3518 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3519 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3520 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003521 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003522 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003523 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003524 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003525 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003526 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3527 true);
satok4e4569d2010-11-19 18:45:53 +09003528 }
satok3ef8b292010-11-23 06:06:29 +09003529 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003530 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003531 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003532 }
3533 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003534 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003535 }
3536
satok4e4569d2010-11-19 18:45:53 +09003537 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003538 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003539 @Override
satok4e4569d2010-11-19 18:45:53 +09003540 public List getShortcutInputMethodsAndSubtypes() {
3541 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003542 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003543 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003544 // If there are no selected shortcut subtypes, the framework will try to find
3545 // the most applicable subtype from all subtypes whose mode is
3546 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003547 Pair<InputMethodInfo, InputMethodSubtype> info =
3548 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003549 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003550 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003551 ret.add(info.first);
3552 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003553 }
satok3da92232011-01-11 22:46:30 +09003554 return ret;
satokf3db1af2010-11-23 13:34:33 +09003555 }
satokf3db1af2010-11-23 13:34:33 +09003556 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3557 ret.add(imi);
3558 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3559 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003560 }
3561 }
satokf3db1af2010-11-23 13:34:33 +09003562 return ret;
satok4e4569d2010-11-19 18:45:53 +09003563 }
3564 }
3565
satok42c5a162011-05-26 16:46:14 +09003566 @Override
satokb66d2872010-11-10 01:04:04 +09003567 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003568 // TODO: Make this work even for non-current users?
3569 if (!calledFromValidUser()) {
3570 return false;
3571 }
satokb66d2872010-11-10 01:04:04 +09003572 synchronized (mMethodMap) {
3573 if (subtype != null && mCurMethodId != null) {
3574 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003575 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003576 if (subtypeId != NOT_A_SUBTYPE_ID) {
3577 setInputMethodLocked(mCurMethodId, subtypeId);
3578 return true;
3579 }
3580 }
3581 return false;
3582 }
3583 }
3584
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003585 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003586 private static class InputMethodFileManager {
3587 private static final String SYSTEM_PATH = "system";
3588 private static final String INPUT_METHOD_PATH = "inputmethod";
3589 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3590 private static final String NODE_SUBTYPES = "subtypes";
3591 private static final String NODE_SUBTYPE = "subtype";
3592 private static final String NODE_IMI = "imi";
3593 private static final String ATTR_ID = "id";
3594 private static final String ATTR_LABEL = "label";
3595 private static final String ATTR_ICON = "icon";
3596 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003597 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003598 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3599 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3600 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
3601 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3602 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003603 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003604 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003605 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003606 if (methodMap == null) {
3607 throw new NullPointerException("methodMap is null");
3608 }
3609 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003610 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003611 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3612 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003613 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003614 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003615 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3616 }
3617 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3618 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3619 if (!subtypeFile.exists()) {
3620 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003621 writeAdditionalInputMethodSubtypes(
3622 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003623 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003624 readAdditionalInputMethodSubtypes(
3625 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003626 }
3627 }
3628
3629 private void deleteAllInputMethodSubtypes(String imiId) {
3630 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003631 mAdditionalSubtypesMap.remove(imiId);
3632 writeAdditionalInputMethodSubtypes(
3633 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003634 }
3635 }
3636
3637 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003638 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003639 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003640 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003641 final int N = additionalSubtypes.length;
3642 for (int i = 0; i < N; ++i) {
3643 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003644 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003645 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003646 } else {
3647 Slog.w(TAG, "Duplicated subtype definition found: "
3648 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003649 }
3650 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003651 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3652 writeAdditionalInputMethodSubtypes(
3653 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003654 }
3655 }
3656
3657 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3658 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003659 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003660 }
3661 }
3662
3663 private static void writeAdditionalInputMethodSubtypes(
3664 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3665 HashMap<String, InputMethodInfo> methodMap) {
3666 // Safety net for the case that this function is called before methodMap is set.
3667 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3668 FileOutputStream fos = null;
3669 try {
3670 fos = subtypesFile.startWrite();
3671 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003672 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003673 out.startDocument(null, true);
3674 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3675 out.startTag(null, NODE_SUBTYPES);
3676 for (String imiId : allSubtypes.keySet()) {
3677 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3678 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3679 continue;
3680 }
3681 out.startTag(null, NODE_IMI);
3682 out.attribute(null, ATTR_ID, imiId);
3683 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3684 final int N = subtypesList.size();
3685 for (int i = 0; i < N; ++i) {
3686 final InputMethodSubtype subtype = subtypesList.get(i);
3687 out.startTag(null, NODE_SUBTYPE);
3688 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3689 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3690 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003691 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3692 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003693 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3694 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3695 out.attribute(null, ATTR_IS_AUXILIARY,
3696 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
3697 out.endTag(null, NODE_SUBTYPE);
3698 }
3699 out.endTag(null, NODE_IMI);
3700 }
3701 out.endTag(null, NODE_SUBTYPES);
3702 out.endDocument();
3703 subtypesFile.finishWrite(fos);
3704 } catch (java.io.IOException e) {
3705 Slog.w(TAG, "Error writing subtypes", e);
3706 if (fos != null) {
3707 subtypesFile.failWrite(fos);
3708 }
3709 }
3710 }
3711
3712 private static void readAdditionalInputMethodSubtypes(
3713 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3714 if (allSubtypes == null || subtypesFile == null) return;
3715 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003716 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003717 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003718 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003719 int type = parser.getEventType();
3720 // Skip parsing until START_TAG
3721 while ((type = parser.next()) != XmlPullParser.START_TAG
3722 && type != XmlPullParser.END_DOCUMENT) {}
3723 String firstNodeName = parser.getName();
3724 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3725 throw new XmlPullParserException("Xml doesn't start with subtypes");
3726 }
3727 final int depth =parser.getDepth();
3728 String currentImiId = null;
3729 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3730 while (((type = parser.next()) != XmlPullParser.END_TAG
3731 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3732 if (type != XmlPullParser.START_TAG)
3733 continue;
3734 final String nodeName = parser.getName();
3735 if (NODE_IMI.equals(nodeName)) {
3736 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3737 if (TextUtils.isEmpty(currentImiId)) {
3738 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3739 continue;
3740 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003741 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003742 allSubtypes.put(currentImiId, tempSubtypesArray);
3743 } else if (NODE_SUBTYPE.equals(nodeName)) {
3744 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3745 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3746 continue;
3747 }
3748 final int icon = Integer.valueOf(
3749 parser.getAttributeValue(null, ATTR_ICON));
3750 final int label = Integer.valueOf(
3751 parser.getAttributeValue(null, ATTR_LABEL));
3752 final String imeSubtypeLocale =
3753 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003754 final String languageTag =
3755 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003756 final String imeSubtypeMode =
3757 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3758 final String imeSubtypeExtraValue =
3759 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003760 final boolean isAuxiliary = "1".equals(String.valueOf(
3761 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003762 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3763 .setSubtypeNameResId(label)
3764 .setSubtypeIconResId(icon)
3765 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003766 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003767 .setSubtypeMode(imeSubtypeMode)
3768 .setSubtypeExtraValue(imeSubtypeExtraValue)
3769 .setIsAuxiliary(isAuxiliary)
3770 .build();
satoke7c6998e2011-06-03 17:57:59 +09003771 tempSubtypesArray.add(subtype);
3772 }
3773 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003774 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3775 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003776 return;
satoke7c6998e2011-06-03 17:57:59 +09003777 }
3778 }
3779 }
3780
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003781 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3782 @NonNull
3783 private final Handler mHandler;
3784
3785 LocalServiceImpl(@NonNull final Handler handler) {
3786 mHandler = handler;
3787 }
3788
3789 @Override
3790 public void setInteractive(boolean interactive) {
3791 // Do everything in handler so as not to block the caller.
3792 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3793 interactive ? 1 : 0, 0));
3794 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003795
3796 @Override
3797 public void switchInputMethod(boolean forwardDirection) {
3798 // Do everything in handler so as not to block the caller.
3799 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3800 forwardDirection ? 1 : 0, 0));
3801 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003802 }
3803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 @Override
3805 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3806 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3807 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3810 + Binder.getCallingPid()
3811 + ", uid=" + Binder.getCallingUid());
3812 return;
3813 }
3814
3815 IInputMethod method;
3816 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003817 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 synchronized (mMethodMap) {
3822 p.println("Current Input Method Manager state:");
3823 int N = mMethodList.size();
3824 p.println(" Input Methods:");
3825 for (int i=0; i<N; i++) {
3826 InputMethodInfo info = mMethodList.get(i);
3827 p.println(" InputMethod #" + i + ":");
3828 info.dump(p, " ");
3829 }
3830 p.println(" Clients:");
3831 for (ClientState ci : mClients.values()) {
3832 p.println(" Client " + ci + ":");
3833 p.println(" client=" + ci.client);
3834 p.println(" inputContext=" + ci.inputContext);
3835 p.println(" sessionRequested=" + ci.sessionRequested);
3836 p.println(" curSession=" + ci.curSession);
3837 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003838 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003840 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3841 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003842 focusedWindowClient = mCurFocusedWindowClient;
3843 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3845 + " mBoundToMethod=" + mBoundToMethod);
3846 p.println(" mCurToken=" + mCurToken);
3847 p.println(" mCurIntent=" + mCurIntent);
3848 method = mCurMethod;
3849 p.println(" mCurMethod=" + mCurMethod);
3850 p.println(" mEnabledSession=" + mEnabledSession);
3851 p.println(" mShowRequested=" + mShowRequested
3852 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3853 + " mShowForced=" + mShowForced
3854 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003855 p.println(" mCurUserActionNotificationSequenceNumber="
3856 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003857 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003858 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003859 p.println(" mSwitchingController:");
3860 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003861 p.println(" mSettings:");
3862 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003864
Jeff Brownb88102f2010-09-08 11:49:43 -07003865 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 pw.flush();
3868 try {
3869 client.client.asBinder().dump(fd, args);
3870 } catch (RemoteException e) {
3871 p.println("Input method client dead: " + e);
3872 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003873 } else {
3874 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003876
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003877 if (focusedWindowClient != null && client != focusedWindowClient) {
3878 p.println(" ");
3879 p.println("Warning: Current input method client doesn't match the last focused. "
3880 + "window.");
3881 p.println("Dumping input method client in the last focused window just in case.");
3882 p.println(" ");
3883 pw.flush();
3884 try {
3885 focusedWindowClient.client.asBinder().dump(fd, args);
3886 } catch (RemoteException e) {
3887 p.println("Input method client in focused window dead: " + e);
3888 }
3889 }
3890
Jeff Brownb88102f2010-09-08 11:49:43 -07003891 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 pw.flush();
3894 try {
3895 method.asBinder().dump(fd, args);
3896 } catch (RemoteException e) {
3897 p.println("Input method service dead: " + e);
3898 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003899 } else {
3900 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 }
3902 }
3903}