blob: 9b8f2d2d7d573c121b5a507c2328a12a5493af1a [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;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080042import android.annotation.UserIdInt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.app.ActivityManagerNative;
44import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070045import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090046import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090047import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090048import android.app.Notification;
49import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070050import android.app.PendingIntent;
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;
Yohei Yukawae985c242016-02-24 18:27:04 -080098import android.util.LocaleList;
satokf9f01002011-05-19 21:31:50 +090099import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +0900100import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.PrintWriterPrinter;
102import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900103import android.util.Slog;
104import android.util.Xml;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900105import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700107import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900108import android.view.LayoutInflater;
109import android.view.View;
110import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import android.view.WindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700112import android.view.WindowManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900113import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.inputmethod.InputBinding;
115import android.view.inputmethod.InputMethod;
116import android.view.inputmethod.InputMethodInfo;
117import android.view.inputmethod.InputMethodManager;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700118import android.view.inputmethod.InputMethodManagerInternal;
satokab751aa2010-09-14 19:17:36 +0900119import android.view.inputmethod.InputMethodSubtype;
Yohei Yukawa443c2ba2014-09-10 14:10:30 +0900120import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900121import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900122import android.widget.CompoundButton;
123import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900125import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900126import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
satoke7c6998e2011-06-03 17:57:59 +0900128import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import java.io.FileDescriptor;
satoke7c6998e2011-06-03 17:57:59 +0900130import java.io.FileInputStream;
131import java.io.FileOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.io.IOException;
133import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100134import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.ArrayList;
satok688bd472012-02-09 20:09:17 +0900136import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.HashMap;
138import java.util.List;
139
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;
Yohei Yukawae985c242016-02-24 18:27:04 -0800449 private LocaleList mLastSystemLocales;
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
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800468 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700469 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
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800777 public void onSwitchUser(@UserIdInt int userHandle) {
778 // Called on the system server's main looper thread.
779 // TODO: Dispatch this to a worker thread as needed.
780 mService.onSwitchUser(userHandle);
781 }
782
783 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800784 public void onBootPhase(int phase) {
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800785 // Called on the system server's main looper thread.
786 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800787 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
788 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
789 .getService(Context.STATUS_BAR_SERVICE);
790 mService.systemRunning(statusBarService);
791 }
792 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800793
794 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800795 public void onUnlockUser(@UserIdInt int userHandle) {
796 // Called on the system server's main looper thread.
797 // TODO: Dispatch this to a worker thread as needed.
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800798 mService.onUnlockUser(userHandle);
799 }
800 }
801
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800802 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800803 synchronized(mMethodMap) {
804 final int currentUserId = mSettings.getCurrentUserId();
805 if (DEBUG) {
806 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
807 }
808 if (userId != currentUserId) {
809 return;
810 }
811 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
812 // We need to rebuild IMEs.
813 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
814 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
815 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -0800816 }
817
Yohei Yukawa7b18aec2016-03-07 13:04:32 -0800818 void onSwitchUser(@UserIdInt int userId) {
819 synchronized (mMethodMap) {
820 switchUserLocked(userId);
821 }
822 }
823
Seigo Nonaka7309b122015-08-17 18:34:13 -0700824 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900825 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800827 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -0700829 // Note: SettingsObserver doesn't register observers in its constructor.
830 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 mIWindowManager = IWindowManager.Stub.asInterface(
832 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -0700833 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Mita Yuned218c72012-12-06 17:18:25 -0800834 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +0900835 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 public void executeMessage(Message msg) {
837 handleMessage(msg);
838 }
Mita Yuned218c72012-12-06 17:18:25 -0800839 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -0800840 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800841 mUserManager = mContext.getSystemService(UserManager.class);
satok01038492012-04-09 21:08:27 +0900842 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700843 mHasFeature = context.getPackageManager().hasSystemFeature(
844 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -0500845 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
satok7cfc0ed2011-06-20 21:29:36 +0900846
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400847 Bundle extras = new Bundle();
848 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
849 mImeSwitcherNotification = new Notification.Builder(mContext)
850 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
851 .setWhen(0)
852 .setOngoing(true)
853 .addExtras(extras)
854 .setCategory(Notification.CATEGORY_SYSTEM)
855 .setColor(com.android.internal.R.color.system_notification_accent_color);
Daniel Sandler590d5152012-06-14 16:10:13 -0400856
satok7cfc0ed2011-06-20 21:29:36 +0900857 Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
satok683e2382011-07-12 08:28:52 +0900858 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +0900859
860 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +0900861
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900862 final IntentFilter broadcastFilter = new IntentFilter();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900863 broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Amith Yamasani734983f2014-03-04 16:48:05 -0800864 broadcastFilter.addAction(Intent.ACTION_USER_ADDED);
865 broadcastFilter.addAction(Intent.ACTION_USER_REMOVED);
Christopher Tate7b9a28c2015-03-18 13:06:16 -0700866 broadcastFilter.addAction(Intent.ACTION_SETTING_RESTORED);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900867 mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800868
satok7cfc0ed2011-06-20 21:29:36 +0900869 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900870 int userId = 0;
871 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900872 userId = ActivityManagerNative.getDefault().getCurrentUser().id;
873 } catch (RemoteException e) {
874 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
875 }
satok81f8b7c2012-12-04 20:42:56 +0900876 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
satok913a8922010-08-26 21:53:41 +0900877
satokd87c2592010-09-29 11:52:06 +0900878 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +0900879 mSettings = new InputMethodSettings(
Yohei Yukawa68645a62016-02-17 07:54:20 -0800880 mRes, context.getContentResolver(), mMethodMap, mMethodList, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -0700881
Kenny Guy2a764942014-04-02 13:29:20 +0100882 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900883 mFileManager = new InputMethodFileManager(mMethodMap, userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900884 synchronized (mMethodMap) {
885 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
886 mSettings, context);
887 }
satok0a1bcf42012-05-16 19:26:31 +0900888
889 // Just checking if defaultImiId is empty or not
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900890 final String defaultImiId = mSettings.getSelectedInputMethod();
Satoshi Kataokaf1367b72013-01-25 17:20:12 +0900891 if (DEBUG) {
892 Slog.d(TAG, "Initial default ime = " + defaultImiId);
893 }
satok0a1bcf42012-05-16 19:26:31 +0900894 mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
895
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900896 synchronized (mMethodMap) {
Yohei Yukawa94e33302016-02-12 19:37:03 -0800897 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900898 }
satokd87c2592010-09-29 11:52:06 +0900899 mSettings.enableAllIMEsIfThereIsNoEnabledIME();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
satok0a1bcf42012-05-16 19:26:31 +0900901 if (!mImeSelectedOnBoot) {
902 Slog.w(TAG, "No IME selected. Choose the most applicable IME.");
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900903 synchronized (mMethodMap) {
904 resetDefaultImeLocked(context);
905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800907
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900908 synchronized (mMethodMap) {
Yohei Yukawa81482972015-06-04 00:58:59 -0700909 mSettingsObserver.registerContentObserverLocked(userId);
Yohei Yukawa5a647b692014-05-22 12:49:00 +0900910 updateFromSettingsLocked(true);
911 }
satok5b927c432012-05-01 20:09:34 +0900912
913 // IMMS wants to receive Intent.ACTION_LOCALE_CHANGED in order to update the current IME
914 // according to the new system locale.
915 final IntentFilter filter = new IntentFilter();
916 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
917 mContext.registerReceiver(
918 new BroadcastReceiver() {
919 @Override
920 public void onReceive(Context context, Intent intent) {
921 synchronized(mMethodMap) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900922 resetStateIfCurrentLocaleChangedLocked();
satok5b927c432012-05-01 20:09:34 +0900923 }
924 }
925 }, filter);
926 }
927
satok5b927c432012-05-01 20:09:34 +0900928 private void resetDefaultImeLocked(Context context) {
929 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800930 if (mCurMethodId != null && !InputMethodUtils.isSystemIme(mMethodMap.get(mCurMethodId))) {
satok5b927c432012-05-01 20:09:34 +0900931 return;
932 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800933 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
934 context, mSystemReady, mSettings.getEnabledInputMethodListLocked());
935 if (suitableImes.isEmpty()) {
936 Slog.i(TAG, "No default found");
937 return;
satok5b927c432012-05-01 20:09:34 +0900938 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -0800939 final InputMethodInfo defIm = suitableImes.get(0);
940 Slog.i(TAG, "Default found, using " + defIm.getId());
941 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +0900942 }
943
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900944 private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged,
945 final boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900946 if (!mSystemReady) {
947 // not system ready
948 return;
949 }
Yohei Yukawae985c242016-02-24 18:27:04 -0800950 final LocaleList newLocales = mRes.getConfiguration().getLocales();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900951 if (!updateOnlyWhenLocaleChanged
Yohei Yukawae985c242016-02-24 18:27:04 -0800952 || (newLocales != null && !newLocales.equals(mLastSystemLocales))) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900953 if (!updateOnlyWhenLocaleChanged) {
954 hideCurrentInputLocked(0, null);
Yohei Yukawa33e81792015-11-17 21:14:42 -0800955 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900956 }
957 if (DEBUG) {
Yohei Yukawae985c242016-02-24 18:27:04 -0800958 Slog.i(TAG, "LocaleList has been changed to " + newLocales);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900959 }
Yohei Yukawa94e33302016-02-12 19:37:03 -0800960 buildInputMethodListLocked(resetDefaultEnabledIme);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900961 if (!updateOnlyWhenLocaleChanged) {
962 final String selectedImiId = mSettings.getSelectedInputMethod();
963 if (TextUtils.isEmpty(selectedImiId)) {
964 // This is the first time of the user switch and
965 // set the current ime to the proper one.
966 resetDefaultImeLocked(mContext);
967 }
Satoshi Kataokad08a9232012-09-28 15:59:58 +0900968 } else {
969 // If the locale is changed, needs to reset the default ime
970 resetDefaultImeLocked(mContext);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900971 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -0800972 updateFromSettingsLocked(true);
Yohei Yukawae985c242016-02-24 18:27:04 -0800973 mLastSystemLocales = newLocales;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900974 if (!updateOnlyWhenLocaleChanged) {
975 try {
976 startInputInnerLocked();
977 } catch (RuntimeException e) {
978 Slog.w(TAG, "Unexpected exception", e);
979 }
980 }
981 }
982 }
983
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +0900984 private void resetStateIfCurrentLocaleChangedLocked() {
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +0900985 resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */,
986 true /* resetDefaultImeLocked */);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900987 }
988
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +0900989 private void switchUserLocked(int newUserId) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -0700990 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
991 + " currentUserId=" + mSettings.getCurrentUserId());
992
Yohei Yukawa81482972015-06-04 00:58:59 -0700993 // ContentObserver should be registered again when the user is changed
994 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -0800995
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800996 // If the system is not ready or the device is not yed unlocked by the user, then we use
997 // copy-on-write settings.
998 final boolean useCopyOnWriteSettings =
999 !mSystemReady || !mUserManager.isUserUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001000 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001001 updateCurrentProfileIds();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09001002 // InputMethodFileManager should be reset when the user is changed
1003 mFileManager = new InputMethodFileManager(mMethodMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001004 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001005
1006 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1007 + " defaultImiId=" + defaultImiId);
1008
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001009 // For secondary users, the list of enabled IMEs may not have been updated since the
1010 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1011 // not be empty even if the IME has been uninstalled by the primary user.
1012 // Even in such cases, IMMS works fine because it will find the most applicable
1013 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001014 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001015 resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */,
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001016 initialUserSwitch /* needsToResetDefaultIme */);
1017 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001018 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1019 mSettings.getEnabledInputMethodListLocked(), newUserId,
1020 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001021 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001022
1023 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1024 + " selectedIme=" + mSettings.getSelectedInputMethod());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001025 }
1026
Kenny Guy2a764942014-04-02 13:29:20 +01001027 void updateCurrentProfileIds() {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001028 List<UserInfo> profiles = mUserManager.getProfiles(mSettings.getCurrentUserId());
Kenny Guy2a764942014-04-02 13:29:20 +01001029 int[] currentProfileIds = new int[profiles.size()]; // profiles will not be null
1030 for (int i = 0; i < currentProfileIds.length; i++) {
1031 currentProfileIds[i] = profiles.get(i).id;
Amith Yamasani734983f2014-03-04 16:48:05 -08001032 }
Kenny Guy2a764942014-04-02 13:29:20 +01001033 mSettings.setCurrentProfileIds(currentProfileIds);
Amith Yamasani734983f2014-03-04 16:48:05 -08001034 }
1035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 @Override
1037 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1038 throws RemoteException {
1039 try {
1040 return super.onTransact(code, data, reply, flags);
1041 } catch (RuntimeException e) {
1042 // The input method manager only throws security exceptions, so let's
1043 // log all others.
1044 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001045 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 throw e;
1048 }
1049 }
1050
Svetoslav Ganova0027152013-06-25 14:59:53 -07001051 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001052 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001053 if (DEBUG) {
1054 Slog.d(TAG, "--- systemReady");
1055 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001056 if (!mSystemReady) {
1057 mSystemReady = true;
Yohei Yukawa68645a62016-02-17 07:54:20 -08001058 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001059 mSettings.switchCurrentUser(currentUserId,
1060 !mUserManager.isUserUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001061 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1062 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001063 mStatusBar = statusBar;
Jason Monk3e189872016-01-12 09:10:34 -05001064 statusBar.setIconVisibility(mSlotIme, false);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001065 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001066 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1067 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001068 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001069 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001070 mHardKeyboardListener);
1071 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08001072 buildInputMethodListLocked(!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
satok0a1bcf42012-05-16 19:26:31 +09001073 if (!mImeSelectedOnBoot) {
1074 Slog.w(TAG, "Reset the default IME as \"Resource\" is ready here.");
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001075 resetStateIfCurrentLocaleChangedLocked();
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001076 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1077 mSettings.getEnabledInputMethodListLocked(),
1078 mSettings.getCurrentUserId(), mContext.getBasePackageName());
satok0a1bcf42012-05-16 19:26:31 +09001079 }
Yohei Yukawae985c242016-02-24 18:27:04 -08001080 mLastSystemLocales = mRes.getConfiguration().getLocales();
Dianne Hackborncc278702009-09-02 23:07:23 -07001081 try {
1082 startInputInnerLocked();
1083 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 Slog.w(TAG, "Unexpected exception", e);
Dianne Hackborncc278702009-09-02 23:07:23 -07001085 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001086 }
1087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001089
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001090 // ---------------------------------------------------------------------------------------
1091 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1092 // 1) it comes from the system process
1093 // 2) the calling process' user id is identical to the current user id IMMS thinks.
1094 private boolean calledFromValidUser() {
1095 final int uid = Binder.getCallingUid();
1096 final int userId = UserHandle.getUserId(uid);
1097 if (DEBUG) {
1098 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1099 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1100 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001101 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1102 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001103 }
Kenny Guy2a764942014-04-02 13:29:20 +01001104 if (uid == Process.SYSTEM_UID || mSettings.isCurrentProfile(userId)) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001105 return true;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001106 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001107
1108 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1109 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1110 // must not manage background users' states in any functions.
1111 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1112 // by a token.
1113 if (mContext.checkCallingOrSelfPermission(
1114 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1115 == PackageManager.PERMISSION_GRANTED) {
1116 if (DEBUG) {
1117 Slog.d(TAG, "--- Access granted because the calling process has "
1118 + "the INTERACT_ACROSS_USERS_FULL permission");
1119 }
1120 return true;
1121 }
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001122 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1123 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001124 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001125 }
1126
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001127
1128 /**
1129 * Returns true iff the caller is identified to be the current input method with the token.
1130 * @param token The window token given to the input method when it was started.
1131 * @return true if and only if non-null valid token is specified.
1132 */
1133 private boolean calledWithValidToken(IBinder token) {
1134 if (token == null || mCurToken != token) {
1135 return false;
1136 }
1137 return true;
1138 }
1139
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001140 private boolean bindCurrentInputMethodService(
1141 Intent service, ServiceConnection conn, int flags) {
1142 if (service == null || conn == null) {
1143 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1144 return false;
1145 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001146 return mContext.bindServiceAsUser(service, conn, flags,
1147 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001148 }
1149
satoke7c6998e2011-06-03 17:57:59 +09001150 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 public List<InputMethodInfo> getInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001152 // TODO: Make this work even for non-current users?
1153 if (!calledFromValidUser()) {
1154 return Collections.emptyList();
1155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001157 return new ArrayList<>(mMethodList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 }
1160
satoke7c6998e2011-06-03 17:57:59 +09001161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public List<InputMethodInfo> getEnabledInputMethodList() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001163 // TODO: Make this work even for non-current users?
1164 if (!calledFromValidUser()) {
1165 return Collections.emptyList();
1166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 synchronized (mMethodMap) {
satokd87c2592010-09-29 11:52:06 +09001168 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170 }
1171
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001172 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001173 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001174 * @return enabled subtypes of the specified imi
1175 */
satoke7c6998e2011-06-03 17:57:59 +09001176 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001177 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09001178 boolean allowsImplicitlySelectedSubtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001179 // TODO: Make this work even for non-current users?
1180 if (!calledFromValidUser()) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001181 return Collections.emptyList();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001182 }
satok67ddf9c2010-11-17 09:45:54 +09001183 synchronized (mMethodMap) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001184 final InputMethodInfo imi;
1185 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001186 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09001187 } else {
1188 imi = mMethodMap.get(imiId);
1189 }
1190 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07001191 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001192 }
1193 return mSettings.getEnabledInputMethodSubtypeListLocked(
1194 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09001195 }
1196 }
1197
satoke7c6998e2011-06-03 17:57:59 +09001198 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 public void addClient(IInputMethodClient client,
1200 IInputContext inputContext, int uid, int pid) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001201 if (!calledFromValidUser()) {
1202 return;
1203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 synchronized (mMethodMap) {
1205 mClients.put(client.asBinder(), new ClientState(client,
1206 inputContext, uid, pid));
1207 }
1208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
satoke7c6998e2011-06-03 17:57:59 +09001210 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 public void removeClient(IInputMethodClient client) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001212 if (!calledFromValidUser()) {
1213 return;
1214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001216 ClientState cs = mClients.remove(client.asBinder());
1217 if (cs != null) {
1218 clearClientSessionLocked(cs);
Yohei Yukawa072b1b52015-11-18 15:54:34 -08001219 if (mCurClient == cs) {
1220 mCurClient = null;
1221 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08001222 if (mCurFocusedWindowClient == cs) {
1223 mCurFocusedWindowClient = null;
1224 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 void executeOrSendMessage(IInterface target, Message msg) {
1230 if (target.asBinder() instanceof Binder) {
1231 mCaller.sendMessage(msg);
1232 } else {
1233 handleMessage(msg);
1234 msg.recycle();
1235 }
1236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001237
Yohei Yukawa33e81792015-11-17 21:14:42 -08001238 void unbindCurrentClientLocked(
1239 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001241 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 + mCurClient.client.asBinder());
1243 if (mBoundToMethod) {
1244 mBoundToMethod = false;
1245 if (mCurMethod != null) {
1246 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
1247 MSG_UNBIND_INPUT, mCurMethod));
1248 }
1249 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07001250
1251 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
1252 MSG_SET_ACTIVE, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08001253 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1254 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 mCurClient = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001257
The Android Open Source Project10592532009-03-18 17:39:46 -07001258 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
1260 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 private int getImeShowFlags() {
1263 int flags = 0;
1264 if (mShowForced) {
1265 flags |= InputMethod.SHOW_FORCED
1266 | InputMethod.SHOW_EXPLICIT;
1267 } else if (mShowExplicitlyRequested) {
1268 flags |= InputMethod.SHOW_EXPLICIT;
1269 }
1270 return flags;
1271 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 private int getAppShowFlags() {
1274 int flags = 0;
1275 if (mShowForced) {
1276 flags |= InputMethodManager.SHOW_FORCED;
1277 } else if (!mShowExplicitlyRequested) {
1278 flags |= InputMethodManager.SHOW_IMPLICIT;
1279 }
1280 return flags;
1281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001282
Dianne Hackborn7663d802012-02-24 13:08:49 -08001283 InputBindResult attachNewInputLocked(boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 if (!mBoundToMethod) {
1285 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1286 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
1287 mBoundToMethod = true;
1288 }
1289 final SessionState session = mCurClient.curSession;
1290 if (initial) {
1291 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1292 MSG_START_INPUT, session, mCurInputContext, mCurAttribute));
1293 } else {
1294 executeOrSendMessage(session.method, mCaller.obtainMessageOOO(
1295 MSG_RESTART_INPUT, session, mCurInputContext, mCurAttribute));
1296 }
1297 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001298 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08001299 showCurrentInputLocked(getAppShowFlags(), null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
Jeff Brown1951ce82013-04-04 22:45:12 -07001301 return new InputBindResult(session.session,
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001302 (session.channel != null ? session.channel.dup() : null),
1303 mCurId, mCurSeq, mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001306 InputBindResult startInputLocked(
1307 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1308 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1309 int controlFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 // If no method is currently selected, do nothing.
1311 if (mCurMethodId == null) {
1312 return mNoBinding;
1313 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 ClientState cs = mClients.get(client.asBinder());
1316 if (cs == null) {
1317 throw new IllegalArgumentException("unknown client "
1318 + client.asBinder());
1319 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 try {
1322 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
1323 // Check with the window manager to make sure this client actually
1324 // has a window with focus. If not, reject. This is thread safe
1325 // because if the focus changes some time before or after, the
1326 // next client receiving focus that has any interest in input will
1327 // be calling through here after that change happens.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001328 Slog.w(TAG, "Starting input on non-focused client " + cs.client
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
1330 return null;
1331 }
1332 } catch (RemoteException e) {
1333 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001334
Dianne Hackborn7663d802012-02-24 13:08:49 -08001335 return startInputUncheckedLocked(cs, inputContext, attribute, controlFlags);
1336 }
1337
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001338 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
1339 @NonNull EditorInfo attribute, int controlFlags) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08001340 // If no method is currently selected, do nothing.
1341 if (mCurMethodId == null) {
1342 return mNoBinding;
1343 }
1344
Yohei Yukawad57ba672015-06-08 16:39:46 -07001345 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
1346 attribute.packageName)) {
1347 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
1348 + " uid=" + cs.uid + " package=" + attribute.packageName);
1349 return mNoBinding;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07001350 }
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04001353 // Was the keyguard locked when switching over to the new client?
1354 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 // If the client is changing, we need to switch over to the new
1356 // one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001357 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001358 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04001359 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360
1361 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001362 if (mIsInteractive) {
Dianne Hackborna6e41342012-05-22 16:30:34 -07001363 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07001364 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 // Bump up the sequence for this client and attach it.
1369 mCurSeq++;
1370 if (mCurSeq <= 0) mCurSeq = 1;
1371 mCurClient = cs;
1372 mCurInputContext = inputContext;
1373 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 // Check if the input method is changing.
1376 if (mCurId != null && mCurId.equals(mCurMethodId)) {
1377 if (cs.curSession != null) {
1378 // Fast case: if we are already connected to the input method,
1379 // then just return it.
Dianne Hackborn7663d802012-02-24 13:08:49 -08001380 return attachNewInputLocked(
1381 (controlFlags&InputMethodManager.CONTROL_START_INITIAL) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383 if (mHaveConnection) {
1384 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 // Return to client, and we will get back with it when
1386 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07001387 requestClientSessionLocked(cs);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001388 return new InputBindResult(null, null, mCurId, mCurSeq,
1389 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 } else if (SystemClock.uptimeMillis()
1391 < (mLastBindTime+TIME_TO_RECONNECT)) {
1392 // In this case we have connected to the service, but
1393 // don't yet have its interface. If it hasn't been too
1394 // long since we did the connection, we'll return to
1395 // the client and wait to get the service interface so
1396 // we can report back. If it has been too long, we want
1397 // to fall through so we can try a disconnect/reconnect
1398 // to see if we can get back in touch with the service.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001399 return new InputBindResult(null, null, mCurId, mCurSeq,
1400 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
1403 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
1405 }
1406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001407
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001408 return startInputInnerLocked();
1409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001411 InputBindResult startInputInnerLocked() {
1412 if (mCurMethodId == null) {
1413 return mNoBinding;
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001416 if (!mSystemReady) {
1417 // If the system is not yet ready, we shouldn't be running third
1418 // party code.
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001419 return new InputBindResult(null, null, mCurMethodId, mCurSeq,
1420 mCurUserActionNotificationSequenceNumber);
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001421 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 InputMethodInfo info = mMethodMap.get(mCurMethodId);
1424 if (info == null) {
1425 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
1426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001428 unbindCurrentMethodLocked(true);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
1431 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001432 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1433 com.android.internal.R.string.input_method_binding_label);
1434 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
1435 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001436 if (bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07001437 | Context.BIND_NOT_VISIBLE | Context.BIND_NOT_FOREGROUND
1438 | Context.BIND_SHOWING_UI)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 mLastBindTime = SystemClock.uptimeMillis();
1440 mHaveConnection = true;
1441 mCurId = info.getId();
1442 mCurToken = new Binder();
1443 try {
Craig Mautnerca0ac712013-03-14 09:43:02 -07001444 if (true || DEBUG) Slog.v(TAG, "Adding window token: " + mCurToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 mIWindowManager.addWindowToken(mCurToken,
1446 WindowManager.LayoutParams.TYPE_INPUT_METHOD);
1447 } catch (RemoteException e) {
1448 }
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09001449 return new InputBindResult(null, null, mCurId, mCurSeq,
1450 mCurUserActionNotificationSequenceNumber);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 } else {
1452 mCurIntent = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001453 Slog.w(TAG, "Failure connecting to input method service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 + mCurIntent);
1455 }
1456 return null;
1457 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001458
Yohei Yukawa05c25f82016-02-22 12:41:17 -08001459 private InputBindResult startInput(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001460 /* @InputMethodClient.StartInputReason */ final int startInputReason,
1461 IInputMethodClient client, IInputContext inputContext, EditorInfo attribute,
1462 int controlFlags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001463 if (!calledFromValidUser()) {
1464 return null;
1465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001467 if (DEBUG) {
1468 Slog.v(TAG, "startInput: reason="
1469 + InputMethodClient.getStartInputReason(startInputReason)
1470 + " client = " + client.asBinder()
1471 + " inputContext=" + inputContext
1472 + " attribute=" + attribute
1473 + " controlFlags=#" + Integer.toHexString(controlFlags));
1474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 final long ident = Binder.clearCallingIdentity();
1476 try {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08001477 return startInputLocked(startInputReason, client, inputContext, attribute,
1478 controlFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 } finally {
1480 Binder.restoreCallingIdentity(ident);
1481 }
1482 }
1483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
satoke7c6998e2011-06-03 17:57:59 +09001485 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 public void finishInput(IInputMethodClient client) {
1487 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001488
satoke7c6998e2011-06-03 17:57:59 +09001489 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 public void onServiceConnected(ComponentName name, IBinder service) {
1491 synchronized (mMethodMap) {
1492 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
1493 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07001494 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001496 unbindCurrentMethodLocked(false);
Dianne Hackborncc278702009-09-02 23:07:23 -07001497 return;
1498 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07001499 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
Dianne Hackborncc278702009-09-02 23:07:23 -07001500 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
1501 MSG_ATTACH_TOKEN, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001503 clearClientSessionLocked(mCurClient);
1504 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 }
1507 }
1508 }
1509
Jeff Brownc28867a2013-03-26 15:42:39 -07001510 void onSessionCreated(IInputMethod method, IInputMethodSession session,
1511 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 synchronized (mMethodMap) {
1513 if (mCurMethod != null && method != null
1514 && mCurMethod.asBinder() == method.asBinder()) {
1515 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001516 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07001518 method, session, channel);
Dianne Hackborn7663d802012-02-24 13:08:49 -08001519 InputBindResult res = attachNewInputLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 if (res.method != null) {
1521 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08001522 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001524 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526 }
1527 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001528
1529 // Session abandoned. Close its associated input channel.
1530 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001533 void unbindCurrentMethodLocked(boolean savePosition) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001534 if (mVisibleBound) {
1535 mContext.unbindService(mVisibleConnection);
1536 mVisibleBound = false;
1537 }
1538
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001539 if (mHaveConnection) {
1540 mContext.unbindService(this);
1541 mHaveConnection = false;
1542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001543
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001544 if (mCurToken != null) {
1545 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001546 if (DEBUG) Slog.v(TAG, "Removing window token: " + mCurToken);
Dianne Hackborn2ea9bae2012-11-02 18:43:48 -07001547 if ((mImeWindowVis & InputMethodService.IME_ACTIVE) != 0 && savePosition) {
satoke0a99412012-05-10 02:22:58 +09001548 // The current IME is shown. Hence an IME switch (transition) is happening.
Seigo Nonaka7309b122015-08-17 18:34:13 -07001549 mWindowManagerInternal.saveLastInputMethodWindowForTransition();
satoke0a99412012-05-10 02:22:58 +09001550 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001551 mIWindowManager.removeWindowToken(mCurToken);
1552 } catch (RemoteException e) {
1553 }
1554 mCurToken = null;
1555 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001556
The Android Open Source Project10592532009-03-18 17:39:46 -07001557 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001558 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001559 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001560
Yohei Yukawa33e81792015-11-17 21:14:42 -08001561 void resetCurrentMethodAndClient(
1562 /* @InputMethodClient.UnbindReason */ final int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08001563 mCurMethodId = null;
1564 unbindCurrentMethodLocked(false);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001565 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001566 }
Jeff Brownc28867a2013-03-26 15:42:39 -07001567
1568 void requestClientSessionLocked(ClientState cs) {
1569 if (!cs.sessionRequested) {
1570 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
1571 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
1572 cs.sessionRequested = true;
1573 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
1574 MSG_CREATE_SESSION, mCurMethod, channels[1],
1575 new MethodCallback(this, mCurMethod, channels[0])));
1576 }
1577 }
1578
1579 void clearClientSessionLocked(ClientState cs) {
1580 finishSessionLocked(cs.curSession);
1581 cs.curSession = null;
1582 cs.sessionRequested = false;
1583 }
1584
1585 private void finishSessionLocked(SessionState sessionState) {
1586 if (sessionState != null) {
1587 if (sessionState.session != null) {
1588 try {
1589 sessionState.session.finishSession();
1590 } catch (RemoteException e) {
1591 Slog.w(TAG, "Session failed to close due to remote exception", e);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001592 updateSystemUiLocked(mCurToken, 0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07001593 }
1594 sessionState.session = null;
1595 }
1596 if (sessionState.channel != null) {
1597 sessionState.channel.dispose();
1598 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06001599 }
1600 }
1601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001603 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 if (mCurMethod != null) {
1605 for (ClientState cs : mClients.values()) {
Jeff Brownc28867a2013-03-26 15:42:39 -07001606 clearClientSessionLocked(cs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06001608
Jeff Brownc28867a2013-03-26 15:42:39 -07001609 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06001610 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 mCurMethod = null;
1612 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001613 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001614 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
satoke7c6998e2011-06-03 17:57:59 +09001618 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 public void onServiceDisconnected(ComponentName name) {
1620 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001621 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 + " mCurIntent=" + mCurIntent);
1623 if (mCurMethod != null && mCurIntent != null
1624 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001625 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 // We consider this to be a new bind attempt, since the system
1627 // should now try to restart the service for us.
1628 mLastBindTime = SystemClock.uptimeMillis();
1629 mShowRequested = mInputShown;
1630 mInputShown = false;
1631 if (mCurClient != null) {
Yohei Yukawa33e81792015-11-17 21:14:42 -08001632 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
1633 MSG_UNBIND_CLIENT, InputMethodClient.UNBIND_REASON_DISCONNECT_IME,
1634 mCurSeq, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
1636 }
1637 }
1638 }
1639
satokf9f01002011-05-19 21:31:50 +09001640 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 public void updateStatusIcon(IBinder token, String packageName, int iconId) {
1642 long ident = Binder.clearCallingIdentity();
1643 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09001645 if (!calledWithValidToken(token)) {
1646 final int uid = Binder.getCallingUid();
1647 Slog.e(TAG, "Ignoring updateStatusIcon due to an invalid token. uid:" + uid
1648 + " token:" + token);
1649 return;
1650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001652 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07001653 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001654 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001657 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001658 CharSequence contentDescription = null;
1659 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001660 // Use PackageManager to load label
1661 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001662 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001663 mIPackageManager.getApplicationInfo(packageName, 0,
1664 mSettings.getCurrentUserId()));
1665 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07001666 /* ignore */
1667 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07001668 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05001669 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07001670 contentDescription != null
1671 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05001672 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675 }
1676 } finally {
1677 Binder.restoreCallingIdentity(ident);
1678 }
1679 }
1680
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001681 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09001682 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04001683 if (mSwitchingDialog != null) return false;
satok2c93efc2012-04-02 19:33:47 +09001684 if (isScreenLocked()) return false;
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001685 if ((visibility & InputMethodService.IME_ACTIVE) == 0) return false;
Seigo Nonaka7309b122015-08-17 18:34:13 -07001686 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001687 // When physical keyboard is attached, we show the ime switcher (or notification if
1688 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
1689 // exists in the IME switcher dialog. Might be OK to remove this condition once
1690 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
1691 return true;
1692 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001693 if ((visibility & InputMethodService.IME_VISIBLE) == 0) return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001694
1695 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
1696 final int N = imis.size();
1697 if (N > 2) return true;
1698 if (N < 1) return false;
1699 int nonAuxCount = 0;
1700 int auxCount = 0;
1701 InputMethodSubtype nonAuxSubtype = null;
1702 InputMethodSubtype auxSubtype = null;
1703 for(int i = 0; i < N; ++i) {
1704 final InputMethodInfo imi = imis.get(i);
1705 final List<InputMethodSubtype> subtypes =
1706 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
1707 final int subtypeCount = subtypes.size();
1708 if (subtypeCount == 0) {
1709 ++nonAuxCount;
1710 } else {
1711 for (int j = 0; j < subtypeCount; ++j) {
1712 final InputMethodSubtype subtype = subtypes.get(j);
1713 if (!subtype.isAuxiliary()) {
1714 ++nonAuxCount;
1715 nonAuxSubtype = subtype;
1716 } else {
1717 ++auxCount;
1718 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09001719 }
1720 }
satok7cfc0ed2011-06-20 21:29:36 +09001721 }
1722 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07001723 if (nonAuxCount > 1 || auxCount > 1) {
1724 return true;
1725 } else if (nonAuxCount == 1 && auxCount == 1) {
1726 if (nonAuxSubtype != null && auxSubtype != null
1727 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
1728 || auxSubtype.overridesImplicitlyEnabledSubtype()
1729 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
1730 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
1731 return false;
1732 }
1733 return true;
1734 }
1735 return false;
satok7cfc0ed2011-06-20 21:29:36 +09001736 }
1737
John Spurlocke0980502013-10-25 11:59:29 -04001738 private boolean isKeyguardLocked() {
1739 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
1740 }
1741
satokdbf29502011-08-25 15:28:23 +09001742 @SuppressWarnings("deprecation")
satokf9f01002011-05-19 21:31:50 +09001743 @Override
Joe Onorato857fd9b2011-01-27 15:08:35 -08001744 public void setImeWindowStatus(IBinder token, int vis, int backDisposition) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001745 if (!calledWithValidToken(token)) {
1746 final int uid = Binder.getCallingUid();
1747 Slog.e(TAG, "Ignoring setImeWindowStatus due to an invalid token. uid:" + uid
1748 + " token:" + token);
1749 return;
1750 }
1751
1752 synchronized (mMethodMap) {
1753 mImeWindowVis = vis;
1754 mBackDisposition = backDisposition;
1755 updateSystemUiLocked(token, vis, backDisposition);
1756 }
1757 }
1758
1759 private void updateSystemUi(IBinder token, int vis, int backDisposition) {
1760 synchronized (mMethodMap) {
1761 updateSystemUiLocked(token, vis, backDisposition);
1762 }
1763 }
1764
1765 // Caution! This method is called in this class. Handle multi-user carefully
1766 private void updateSystemUiLocked(IBinder token, int vis, int backDisposition) {
1767 if (!calledWithValidToken(token)) {
1768 final int uid = Binder.getCallingUid();
1769 Slog.e(TAG, "Ignoring updateSystemUiLocked due to an invalid token. uid:" + uid
1770 + " token:" + token);
1771 return;
1772 }
1773
1774 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
1775 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001776 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09001777 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001778 // apply policy for binder calls
1779 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
1780 vis = 0;
satok06487a52010-10-29 11:37:18 +09001781 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001782 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
1783 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
1784 if (mStatusBar != null) {
1785 mStatusBar.setImeWindowStatus(token, vis, backDisposition,
1786 needsToShowImeSwitcher);
1787 }
1788 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1789 if (imi != null && needsToShowImeSwitcher) {
1790 // Used to load label
1791 final CharSequence title = mRes.getText(
1792 com.android.internal.R.string.select_input_method);
1793 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
1794 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001795 mImeSwitcherNotification.setContentTitle(title)
1796 .setContentText(summary)
1797 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07001798 try {
1799 if ((mNotificationManager != null)
1800 && !mIWindowManager.hasNavigationBar()) {
1801 if (DEBUG) {
1802 Slog.d(TAG, "--- show notification: label = " + summary);
1803 }
1804 mNotificationManager.notifyAsUser(null,
1805 com.android.internal.R.string.select_input_method,
1806 mImeSwitcherNotification.build(), UserHandle.ALL);
1807 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07001808 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07001809 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001810 }
1811 } else {
1812 if (mNotificationShown && mNotificationManager != null) {
1813 if (DEBUG) {
1814 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09001815 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001816 mNotificationManager.cancelAsUser(null,
1817 com.android.internal.R.string.select_input_method, UserHandle.ALL);
1818 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09001819 }
satok06487a52010-10-29 11:37:18 +09001820 }
1821 } finally {
1822 Binder.restoreCallingIdentity(ident);
1823 }
1824 }
1825
satoke7c6998e2011-06-03 17:57:59 +09001826 @Override
satokf9f01002011-05-19 21:31:50 +09001827 public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001828 if (!calledFromValidUser()) {
1829 return;
1830 }
satokf9f01002011-05-19 21:31:50 +09001831 synchronized (mMethodMap) {
1832 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
1833 for (int i = 0; i < spans.length; ++i) {
1834 SuggestionSpan ss = spans[i];
satok42c5a162011-05-26 16:46:14 +09001835 if (!TextUtils.isEmpty(ss.getNotificationTargetClassName())) {
satokf9f01002011-05-19 21:31:50 +09001836 mSecureSuggestionSpans.put(ss, currentImi);
1837 }
1838 }
1839 }
1840 }
1841
satoke7c6998e2011-06-03 17:57:59 +09001842 @Override
satokf9f01002011-05-19 21:31:50 +09001843 public boolean notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001844 if (!calledFromValidUser()) {
1845 return false;
1846 }
satokf9f01002011-05-19 21:31:50 +09001847 synchronized (mMethodMap) {
1848 final InputMethodInfo targetImi = mSecureSuggestionSpans.get(span);
1849 // TODO: Do not send the intent if the process of the targetImi is already dead.
1850 if (targetImi != null) {
1851 final String[] suggestions = span.getSuggestions();
1852 if (index < 0 || index >= suggestions.length) return false;
satok42c5a162011-05-26 16:46:14 +09001853 final String className = span.getNotificationTargetClassName();
satokf9f01002011-05-19 21:31:50 +09001854 final Intent intent = new Intent();
1855 // Ensures that only a class in the original IME package will receive the
1856 // notification.
satok42c5a162011-05-26 16:46:14 +09001857 intent.setClassName(targetImi.getPackageName(), className);
satokf9f01002011-05-19 21:31:50 +09001858 intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
1859 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, originalString);
1860 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, suggestions[index]);
1861 intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, span.hashCode());
Amith Yamasanif043de92012-10-24 06:42:40 -07001862 final long ident = Binder.clearCallingIdentity();
1863 try {
1864 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
1865 } finally {
1866 Binder.restoreCallingIdentity(ident);
1867 }
satokf9f01002011-05-19 21:31:50 +09001868 return true;
1869 }
1870 }
1871 return false;
1872 }
1873
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001874 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001875 updateInputMethodsFromSettingsLocked(enabledMayChange);
1876 updateKeyboardFromSettingsLocked();
1877 }
1878
1879 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001880 if (enabledMayChange) {
1881 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
1882 for (int i=0; i<enabled.size(); i++) {
1883 // We allow the user to select "disabled until used" apps, so if they
1884 // are enabling one of those here we now need to make it enabled.
1885 InputMethodInfo imm = enabled.get(i);
1886 try {
1887 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
1888 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
1889 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09001890 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001891 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001892 if (DEBUG) {
1893 Slog.d(TAG, "Update state(" + imm.getId()
1894 + "): DISABLED_UNTIL_USED -> DEFAULT");
1895 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001896 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
1897 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001898 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
1899 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001900 }
1901 } catch (RemoteException e) {
1902 }
1903 }
1904 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001905 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
1906 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
1907 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
1908 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001909 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001910 // There is no input method selected, try to choose new applicable input method.
1911 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001912 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09001913 }
1914 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09001916 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001918 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawa33e81792015-11-17 21:14:42 -08001919 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
satokf3db1af2010-11-23 13:34:33 +09001921 mShortcutInputMethodsAndSubtypes.clear();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001922 } else {
1923 // There is no longer an input method set, so stop any current one.
Yohei Yukawa33e81792015-11-17 21:14:42 -08001924 resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09001926 // Here is not the perfect place to reset the switching controller. Ideally
1927 // mSwitchingController and mSettings should be able to share the same state.
1928 // TODO: Make sure that mSwitchingController and mSettings are sharing the
1929 // the same enabled IMEs list.
1930 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001931
1932 }
1933
1934 public void updateKeyboardFromSettingsLocked() {
1935 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
1936 if (mSwitchingDialog != null
1937 && mSwitchingDialogTitleView != null
1938 && mSwitchingDialog.isShowing()) {
1939 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
1940 com.android.internal.R.id.hard_keyboard_switch);
1941 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
1942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001944
Yohei Yukawab097b822015-12-01 10:43:08 -08001945 private void notifyInputMethodSubtypeChanged(final int userId,
1946 @Nullable final InputMethodInfo inputMethodInfo,
1947 @Nullable final InputMethodSubtype subtype) {
1948 final InputManagerInternal inputManagerInternal =
1949 LocalServices.getService(InputManagerInternal.class);
1950 if (inputManagerInternal != null) {
1951 inputManagerInternal.onInputMethodSubtypeChanged(userId, inputMethodInfo, subtype);
1952 }
1953 }
1954
satokab751aa2010-09-14 19:17:36 +09001955 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 InputMethodInfo info = mMethodMap.get(id);
1957 if (info == null) {
satok913a8922010-08-26 21:53:41 +09001958 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001960
satokd81e9502012-05-21 12:58:45 +09001961 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09001963 final int subtypeCount = info.getSubtypeCount();
1964 if (subtypeCount <= 0) {
1965 return;
satokcd7cd292010-11-20 15:46:23 +09001966 }
satokd81e9502012-05-21 12:58:45 +09001967 final InputMethodSubtype oldSubtype = mCurrentSubtype;
1968 final InputMethodSubtype newSubtype;
1969 if (subtypeId >= 0 && subtypeId < subtypeCount) {
1970 newSubtype = info.getSubtypeAt(subtypeId);
1971 } else {
1972 // If subtype is null, try to find the most applicable one from
1973 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001974 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09001975 }
1976 if (newSubtype == null || oldSubtype == null) {
1977 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
1978 + ", new subtype = " + newSubtype);
1979 return;
1980 }
1981 if (newSubtype != oldSubtype) {
1982 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
1983 if (mCurMethod != null) {
1984 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09001985 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09001986 mCurMethod.changeInputMethodSubtype(newSubtype);
1987 } catch (RemoteException e) {
1988 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
Yohei Yukawab097b822015-12-01 10:43:08 -08001989 return;
satokab751aa2010-09-14 19:17:36 +09001990 }
1991 }
Yohei Yukawab097b822015-12-01 10:43:08 -08001992 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info, newSubtype);
satokab751aa2010-09-14 19:17:36 +09001993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 return;
1995 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001996
satokd81e9502012-05-21 12:58:45 +09001997 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 final long ident = Binder.clearCallingIdentity();
1999 try {
satokab751aa2010-09-14 19:17:36 +09002000 // Set a subtype to this input method.
2001 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09002002 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
2003 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
2004 // because mCurMethodId is stored as a history in
2005 // setSelectedInputMethodAndSubtypeLocked().
2006 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007
2008 if (ActivityManagerNative.isSystemReady()) {
2009 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08002010 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07002012 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
Yohei Yukawa33e81792015-11-17 21:14:42 -08002014 unbindCurrentClientLocked(InputMethodClient.UNBIND_REASON_SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 } finally {
2016 Binder.restoreCallingIdentity(ident);
2017 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002018
2019 notifyInputMethodSubtypeChanged(mSettings.getCurrentUserId(), info,
2020 getCurrentInputMethodSubtypeLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002022
satok42c5a162011-05-26 16:46:14 +09002023 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002024 public boolean showSoftInput(IInputMethodClient client, int flags,
2025 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002026 if (!calledFromValidUser()) {
2027 return false;
2028 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002029 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 long ident = Binder.clearCallingIdentity();
2031 try {
2032 synchronized (mMethodMap) {
2033 if (mCurClient == null || client == null
2034 || mCurClient.client.asBinder() != client.asBinder()) {
2035 try {
2036 // We need to check if this is the current client with
2037 // focus in the window manager, to allow this call to
2038 // be made before input is started in it.
2039 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002040 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002041 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 }
2043 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002044 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
2046 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002047
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002048 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002049 return showCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
2051 } finally {
2052 Binder.restoreCallingIdentity(ident);
2053 }
2054 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002055
The Android Open Source Project4df24232009-03-05 14:34:35 -08002056 boolean showCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002058 if (mAccessibilityRequestingNoSoftKeyboard) {
2059 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
Anna Galusza9b278112016-01-04 11:37:37 -08002061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
2063 mShowExplicitlyRequested = true;
2064 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08002065 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
2066 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002068
Dianne Hackborncc278702009-09-02 23:07:23 -07002069 if (!mSystemReady) {
2070 return false;
2071 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002072
The Android Open Source Project4df24232009-03-05 14:34:35 -08002073 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002075 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002076 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2077 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), mCurMethod,
2078 resultReceiver));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002080 if (mHaveConnection && !mVisibleBound) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002081 bindCurrentInputMethodService(
Dianne Hackbornf0f94d12014-03-17 16:04:21 -07002082 mCurIntent, mVisibleConnection, Context.BIND_AUTO_CREATE
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002083 | Context.BIND_TREAT_LIKE_ACTIVITY
2084 | Context.BIND_FOREGROUND_SERVICE);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002085 mVisibleBound = true;
2086 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002087 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09002089 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 // The client has asked to have the input method shown, but
2091 // we have been sitting here too long with a connection to the
2092 // service and no interface received, so let's disconnect/connect
2093 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002094 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09002096 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 mContext.unbindService(this);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002098 bindCurrentInputMethodService(mCurIntent, this, Context.BIND_AUTO_CREATE
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002099 | Context.BIND_NOT_VISIBLE);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002100 } else {
2101 if (DEBUG) {
2102 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
2103 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
2104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002106
The Android Open Source Project4df24232009-03-05 14:34:35 -08002107 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002109
satok42c5a162011-05-26 16:46:14 +09002110 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002111 public boolean hideSoftInput(IInputMethodClient client, int flags,
2112 ResultReceiver resultReceiver) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002113 if (!calledFromValidUser()) {
2114 return false;
2115 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002116 int uid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 long ident = Binder.clearCallingIdentity();
2118 try {
2119 synchronized (mMethodMap) {
2120 if (mCurClient == null || client == null
2121 || mCurClient.client.asBinder() != client.asBinder()) {
2122 try {
2123 // We need to check if this is the current client with
2124 // focus in the window manager, to allow this call to
2125 // be made before input is started in it.
2126 if (!mIWindowManager.inputMethodClientHasFocus(client)) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002127 if (DEBUG) Slog.w(TAG, "Ignoring hideSoftInput of uid "
2128 + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002129 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131 } catch (RemoteException e) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002132 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 }
2134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
Joe Onorato8a9b2202010-02-26 18:56:32 -08002136 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002137 return hideCurrentInputLocked(flags, resultReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139 } finally {
2140 Binder.restoreCallingIdentity(ident);
2141 }
2142 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002143
The Android Open Source Project4df24232009-03-05 14:34:35 -08002144 boolean hideCurrentInputLocked(int flags, ResultReceiver resultReceiver) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
2146 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002147 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 -08002148 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
2150 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002151 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 -08002152 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 }
Seigo Nonakaec928652015-06-10 15:31:20 +09002154
2155 // There is a chance that IMM#hideSoftInput() is called in a transient state where
2156 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
2157 // to be updated with the new value sent from IME process. Even in such a transient state
2158 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
2159 // application process as a valid request, and have even promised such a behavior with CTS
2160 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
2161 // IMMS#InputShown indicates that the software keyboard is shown.
2162 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
2163 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
2164 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002165 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09002166 if (shouldHideSoftInput) {
2167 // The IME will report its visible state again after the following message finally
2168 // delivered to the IME process as an IPC. Hence the inconsistency between
2169 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
2170 // the final state.
The Android Open Source Project4df24232009-03-05 14:34:35 -08002171 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2172 MSG_HIDE_SOFT_INPUT, mCurMethod, resultReceiver));
2173 res = true;
2174 } else {
2175 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002177 if (mHaveConnection && mVisibleBound) {
2178 mContext.unbindService(mVisibleConnection);
2179 mVisibleBound = false;
2180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 mInputShown = false;
2182 mShowRequested = false;
2183 mShowExplicitlyRequested = false;
2184 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08002185 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002187
satok42c5a162011-05-26 16:46:14 +09002188 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08002189 public InputBindResult startInputOrWindowGainedFocus(
2190 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2191 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2192 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
2193 if (windowToken != null) {
2194 return windowGainedFocus(startInputReason, client, windowToken, controlFlags,
2195 softInputMode, windowFlags, attribute, inputContext);
2196 } else {
2197 return startInput(startInputReason, client, inputContext, attribute, controlFlags);
2198 }
2199 }
2200
2201 private InputBindResult windowGainedFocus(
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002202 /* @InputMethodClient.StartInputReason */ final int startInputReason,
2203 IInputMethodClient client, IBinder windowToken, int controlFlags, int softInputMode,
2204 int windowFlags, EditorInfo attribute, IInputContext inputContext) {
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002205 // Needs to check the validity before clearing calling identity
2206 final boolean calledFromValidUser = calledFromValidUser();
Dianne Hackborn7663d802012-02-24 13:08:49 -08002207 InputBindResult res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 long ident = Binder.clearCallingIdentity();
2209 try {
2210 synchronized (mMethodMap) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002211 if (DEBUG) Slog.v(TAG, "windowGainedFocus: reason="
2212 + InputMethodClient.getStartInputReason(startInputReason)
2213 + " client=" + client.asBinder()
2214 + " inputContext=" + inputContext
2215 + " attribute=" + attribute
Dianne Hackborn7663d802012-02-24 13:08:49 -08002216 + " controlFlags=#" + Integer.toHexString(controlFlags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 + " softInputMode=#" + Integer.toHexString(softInputMode)
Dianne Hackborn7663d802012-02-24 13:08:49 -08002218 + " windowFlags=#" + Integer.toHexString(windowFlags));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002219
Dianne Hackborn7663d802012-02-24 13:08:49 -08002220 ClientState cs = mClients.get(client.asBinder());
2221 if (cs == null) {
2222 throw new IllegalArgumentException("unknown client "
2223 + client.asBinder());
2224 }
2225
2226 try {
2227 if (!mIWindowManager.inputMethodClientHasFocus(cs.client)) {
2228 // Check with the window manager to make sure this client actually
2229 // has a window with focus. If not, reject. This is thread safe
2230 // because if the focus changes some time before or after, the
2231 // next client receiving focus that has any interest in input will
2232 // be calling through here after that change happens.
2233 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
2234 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
2235 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002237 } catch (RemoteException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002239
Satoshi Kataoka8d033052012-11-19 17:30:40 +09002240 if (!calledFromValidUser) {
2241 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
2242 Slog.w(TAG, "If you want to interect with IME, you need "
2243 + "android.permission.INTERACT_ACROSS_USERS_FULL");
2244 hideCurrentInputLocked(0, null);
2245 return null;
2246 }
2247
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002248 if (mCurFocusedWindow == windowToken) {
Dianne Hackbornac920872012-05-22 11:49:49 -07002249 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
Satoshi Kataoka35739502012-10-02 19:00:26 +09002250 + " attribute=" + attribute + ", token = " + windowToken);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002251 if (attribute != null) {
2252 return startInputUncheckedLocked(cs, inputContext, attribute,
2253 controlFlags);
2254 }
2255 return null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002256 }
2257 mCurFocusedWindow = windowToken;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002258 mCurFocusedWindowClient = cs;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002259
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002260 // Should we auto-show the IME even if the caller has not
2261 // specified what should be done with it?
2262 // We only do this automatically if the window can resize
2263 // to accommodate the IME (so what the user sees will give
2264 // them good context without input information being obscured
2265 // by the IME) or if running on a large screen where there
2266 // is more room for the target window + IME.
2267 final boolean doAutoShow =
2268 (softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
2269 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
2270 || mRes.getConfiguration().isLayoutSizeAtLeast(
2271 Configuration.SCREENLAYOUT_SIZE_LARGE);
Dianne Hackborn7663d802012-02-24 13:08:49 -08002272 final boolean isTextEditor =
2273 (controlFlags&InputMethodManager.CONTROL_WINDOW_IS_TEXT_EDITOR) != 0;
2274
2275 // We want to start input before showing the IME, but after closing
2276 // it. We want to do this after closing it to help the IME disappear
2277 // more quickly (not get stuck behind it initializing itself for the
2278 // new focused input, even if its window wants to hide the IME).
2279 boolean didStart = false;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 switch (softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE) {
2282 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002283 if (!isTextEditor || !doAutoShow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 if (WindowManager.LayoutParams.mayUseInputMethod(windowFlags)) {
2285 // There is no focus view, and this window will
2286 // be behind any soft input window, so hide the
2287 // soft input window if it is shown.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002288 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002289 hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 }
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002291 } else if (isTextEditor && doAutoShow && (softInputMode &
2292 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 // There is a focus view, and we are navigating forward
2294 // into the window, so show the input window for the user.
Dianne Hackborn7663d802012-02-24 13:08:49 -08002295 // We only do this automatically if the window can resize
2296 // to accommodate the IME (so what the user sees will give
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08002297 // them good context without input information being obscured
2298 // by the IME) or if running on a large screen where there
2299 // is more room for the target window + IME.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002300 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002301 if (attribute != null) {
2302 res = startInputUncheckedLocked(cs, inputContext, attribute,
2303 controlFlags);
2304 didStart = true;
2305 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002306 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
2308 break;
2309 case WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
2310 // Do nothing.
2311 break;
2312 case WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN:
2313 if ((softInputMode &
2314 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002315 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002316 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 }
2318 break;
2319 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002320 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
The Android Open Source Project4df24232009-03-05 14:34:35 -08002321 hideCurrentInputLocked(0, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 break;
2323 case WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE:
2324 if ((softInputMode &
2325 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002326 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Dianne Hackborn7663d802012-02-24 13:08:49 -08002327 if (attribute != null) {
2328 res = startInputUncheckedLocked(cs, inputContext, attribute,
2329 controlFlags);
2330 didStart = true;
2331 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002332 showCurrentInputLocked(InputMethodManager.SHOW_IMPLICIT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 }
2334 break;
2335 case WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Joe Onorato8a9b2202010-02-26 18:56:32 -08002336 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
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 break;
2344 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002345
2346 if (!didStart && attribute != null) {
2347 res = startInputUncheckedLocked(cs, inputContext, attribute,
2348 controlFlags);
2349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 }
2351 } finally {
2352 Binder.restoreCallingIdentity(ident);
2353 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08002354
2355 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002357
satok42c5a162011-05-26 16:46:14 +09002358 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07002359 public void showInputMethodPickerFromClient(
2360 IInputMethodClient client, int auxiliarySubtypeMode) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002361 if (!calledFromValidUser()) {
2362 return;
2363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 synchronized (mMethodMap) {
2365 if (mCurClient == null || client == null
2366 || mCurClient.client.asBinder() != client.asBinder()) {
satok47a44912010-10-06 16:03:58 +09002367 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002368 + Binder.getCallingUid() + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370
satok440aab52010-11-25 09:43:11 +09002371 // Always call subtype picker, because subtype picker is a superset of input method
2372 // picker.
Seigo Nonaka14e13912015-05-06 21:04:13 -07002373 mHandler.sendMessage(mCaller.obtainMessageI(
2374 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode));
satokab751aa2010-09-14 19:17:36 +09002375 }
2376 }
2377
satok42c5a162011-05-26 16:46:14 +09002378 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 public void setInputMethod(IBinder token, String id) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002380 if (!calledFromValidUser()) {
2381 return;
2382 }
satok28203512010-11-24 11:06:49 +09002383 setInputMethodWithSubtypeId(token, id, NOT_A_SUBTYPE_ID);
2384 }
2385
satok42c5a162011-05-26 16:46:14 +09002386 @Override
satok28203512010-11-24 11:06:49 +09002387 public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002388 if (!calledFromValidUser()) {
2389 return;
2390 }
satok28203512010-11-24 11:06:49 +09002391 synchronized (mMethodMap) {
2392 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002393 setInputMethodWithSubtypeIdLocked(token, id,
2394 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
2395 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09002396 } else {
2397 setInputMethod(token, id);
2398 }
2399 }
satokab751aa2010-09-14 19:17:36 +09002400 }
2401
satok42c5a162011-05-26 16:46:14 +09002402 @Override
satokb416a712010-11-25 20:42:14 +09002403 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09002404 IInputMethodClient client, String inputMethodId) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002405 if (!calledFromValidUser()) {
2406 return;
2407 }
satokb416a712010-11-25 20:42:14 +09002408 synchronized (mMethodMap) {
satok7fee71f2010-12-17 18:54:26 +09002409 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2410 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09002411 }
2412 }
2413
satok4fc87d62011-05-20 16:13:43 +09002414 @Override
satok735cf382010-11-11 20:40:09 +09002415 public boolean switchToLastInputMethod(IBinder token) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002416 if (!calledFromValidUser()) {
2417 return false;
2418 }
satok735cf382010-11-11 20:40:09 +09002419 synchronized (mMethodMap) {
satokc445bcd2011-01-25 18:57:24 +09002420 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09002421 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09002422 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09002423 lastImi = mMethodMap.get(lastIme.first);
2424 } else {
2425 lastImi = null;
satok735cf382010-11-11 20:40:09 +09002426 }
satok4fc87d62011-05-20 16:13:43 +09002427 String targetLastImiId = null;
2428 int subtypeId = NOT_A_SUBTYPE_ID;
2429 if (lastIme != null && lastImi != null) {
2430 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
2431 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
2432 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
2433 : mCurrentSubtype.hashCode();
2434 // If the last IME is the same as the current IME and the last subtype is not
2435 // defined, there is no need to switch to the last IME.
2436 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
2437 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002438 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09002439 }
2440 }
2441
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002442 if (TextUtils.isEmpty(targetLastImiId)
2443 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09002444 // This is a safety net. If the currentSubtype can't be added to the history
2445 // and the framework couldn't find the last ime, we will make the last ime be
2446 // the most applicable enabled keyboard subtype of the system imes.
2447 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2448 if (enabled != null) {
2449 final int N = enabled.size();
2450 final String locale = mCurrentSubtype == null
2451 ? mRes.getConfiguration().locale.toString()
2452 : mCurrentSubtype.getLocale();
2453 for (int i = 0; i < N; ++i) {
2454 final InputMethodInfo imi = enabled.get(i);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002455 if (imi.getSubtypeCount() > 0 && InputMethodUtils.isSystemIme(imi)) {
satok4fc87d62011-05-20 16:13:43 +09002456 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002457 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
2458 InputMethodUtils.getSubtypes(imi),
2459 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09002460 if (keyboardSubtype != null) {
2461 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002462 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09002463 imi, keyboardSubtype.hashCode());
2464 if(keyboardSubtype.getLocale().equals(locale)) {
2465 break;
2466 }
2467 }
2468 }
2469 }
2470 }
2471 }
2472
2473 if (!TextUtils.isEmpty(targetLastImiId)) {
2474 if (DEBUG) {
2475 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
2476 + ", from: " + mCurMethodId + ", " + subtypeId);
2477 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002478 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09002479 return true;
2480 } else {
2481 return false;
2482 }
satok735cf382010-11-11 20:40:09 +09002483 }
2484 }
2485
satoke7c6998e2011-06-03 17:57:59 +09002486 @Override
satok688bd472012-02-09 20:09:17 +09002487 public boolean switchToNextInputMethod(IBinder token, boolean onlyCurrentIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002488 if (!calledFromValidUser()) {
2489 return false;
2490 }
satok688bd472012-02-09 20:09:17 +09002491 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002492 if (!calledWithValidToken(token)) {
2493 final int uid = Binder.getCallingUid();
2494 Slog.e(TAG, "Ignoring switchToNextInputMethod due to an invalid token. uid:" + uid
2495 + " token:" + token);
2496 return false;
2497 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002498 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
satok688bd472012-02-09 20:09:17 +09002499 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2500 if (nextSubtype == null) {
2501 return false;
2502 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002503 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
2504 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09002505 return true;
2506 }
2507 }
2508
2509 @Override
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002510 public boolean shouldOfferSwitchingToNextInputMethod(IBinder token) {
2511 if (!calledFromValidUser()) {
2512 return false;
2513 }
2514 synchronized (mMethodMap) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09002515 if (!calledWithValidToken(token)) {
2516 final int uid = Binder.getCallingUid();
2517 Slog.e(TAG, "Ignoring shouldOfferSwitchingToNextInputMethod due to an invalid "
2518 + "token. uid:" + uid + " token:" + token);
2519 return false;
2520 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002521 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09002522 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2523 if (nextSubtype == null) {
2524 return false;
2525 }
2526 return true;
2527 }
2528 }
2529
2530 @Override
satok68f1b782011-04-11 14:26:04 +09002531 public InputMethodSubtype getLastInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002532 if (!calledFromValidUser()) {
2533 return null;
2534 }
satok68f1b782011-04-11 14:26:04 +09002535 synchronized (mMethodMap) {
2536 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
2537 // TODO: Handle the case of the last IME with no subtypes
2538 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
2539 || TextUtils.isEmpty(lastIme.second)) return null;
2540 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
2541 if (lastImi == null) return null;
2542 try {
2543 final int lastSubtypeHash = Integer.valueOf(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002544 final int lastSubtypeId =
2545 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09002546 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
2547 return null;
2548 }
2549 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09002550 } catch (NumberFormatException e) {
2551 return null;
2552 }
2553 }
2554 }
2555
satoke7c6998e2011-06-03 17:57:59 +09002556 @Override
satokee5e77c2011-09-02 18:50:15 +09002557 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002558 if (!calledFromValidUser()) {
2559 return;
2560 }
satok91e88122011-07-18 11:11:42 +09002561 // By this IPC call, only a process which shares the same uid with the IME can add
2562 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08002563 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
satoke7c6998e2011-06-03 17:57:59 +09002564 synchronized (mMethodMap) {
satok91e88122011-07-18 11:11:42 +09002565 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09002566 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002567 final String[] packageInfos;
2568 try {
2569 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
2570 } catch (RemoteException e) {
2571 Slog.e(TAG, "Failed to get package infos");
2572 return;
2573 }
satok91e88122011-07-18 11:11:42 +09002574 if (packageInfos != null) {
2575 final int packageNum = packageInfos.length;
2576 for (int i = 0; i < packageNum; ++i) {
2577 if (packageInfos[i].equals(imi.getPackageName())) {
2578 mFileManager.addInputMethodSubtypes(imi, subtypes);
satokc5933802011-08-31 21:26:04 +09002579 final long ident = Binder.clearCallingIdentity();
2580 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08002581 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09002582 } finally {
2583 Binder.restoreCallingIdentity(ident);
2584 }
satokee5e77c2011-09-02 18:50:15 +09002585 return;
satok91e88122011-07-18 11:11:42 +09002586 }
2587 }
2588 }
satoke7c6998e2011-06-03 17:57:59 +09002589 }
satokee5e77c2011-09-02 18:50:15 +09002590 return;
satoke7c6998e2011-06-03 17:57:59 +09002591 }
2592
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002593 @Override
2594 public int getInputMethodWindowVisibleHeight() {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002595 return mWindowManagerInternal.getInputMethodWindowVisibleHeight();
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09002596 }
2597
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002598 @Override
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002599 public void notifyUserAction(int sequenceNumber) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002600 if (DEBUG) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002601 Slog.d(TAG, "Got the notification of a user action. sequenceNumber:" + sequenceNumber);
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002602 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002603 synchronized (mMethodMap) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002604 if (mCurUserActionNotificationSequenceNumber != sequenceNumber) {
2605 if (DEBUG) {
2606 Slog.d(TAG, "Ignoring the user action notification due to the sequence number "
2607 + "mismatch. expected:" + mCurUserActionNotificationSequenceNumber
2608 + " actual: " + sequenceNumber);
2609 }
2610 return;
2611 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002612 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2613 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09002614 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09002615 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09002616 }
2617 }
2618
satok28203512010-11-24 11:06:49 +09002619 private void setInputMethodWithSubtypeId(IBinder token, String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 synchronized (mMethodMap) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002621 setInputMethodWithSubtypeIdLocked(token, id, subtypeId);
2622 }
2623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002625 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
2626 if (token == null) {
2627 if (mContext.checkCallingOrSelfPermission(
2628 android.Manifest.permission.WRITE_SECURE_SETTINGS)
2629 != PackageManager.PERMISSION_GRANTED) {
2630 throw new SecurityException(
2631 "Using null token requires permission "
2632 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002633 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09002634 } else if (mCurToken != token) {
2635 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
2636 + " token: " + token);
2637 return;
2638 }
2639
2640 final long ident = Binder.clearCallingIdentity();
2641 try {
2642 setInputMethodLocked(id, subtypeId);
2643 } finally {
2644 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646 }
2647
satok42c5a162011-05-26 16:46:14 +09002648 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 public void hideMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002650 if (!calledFromValidUser()) {
2651 return;
2652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002654 if (!calledWithValidToken(token)) {
2655 final int uid = Binder.getCallingUid();
2656 Slog.e(TAG, "Ignoring hideInputMethod due to an invalid token. uid:"
2657 + uid + " token:" + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 return;
2659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 long ident = Binder.clearCallingIdentity();
2661 try {
The Android Open Source Project4df24232009-03-05 14:34:35 -08002662 hideCurrentInputLocked(flags, null);
2663 } finally {
2664 Binder.restoreCallingIdentity(ident);
2665 }
2666 }
2667 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002668
satok42c5a162011-05-26 16:46:14 +09002669 @Override
The Android Open Source Project4df24232009-03-05 14:34:35 -08002670 public void showMySoftInput(IBinder token, int flags) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002671 if (!calledFromValidUser()) {
2672 return;
2673 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08002674 synchronized (mMethodMap) {
Yohei Yukawa22c97be2014-06-04 19:43:36 +09002675 if (!calledWithValidToken(token)) {
2676 final int uid = Binder.getCallingUid();
2677 Slog.e(TAG, "Ignoring showMySoftInput due to an invalid token. uid:"
2678 + uid + " token:" + token);
The Android Open Source Project4df24232009-03-05 14:34:35 -08002679 return;
2680 }
2681 long ident = Binder.clearCallingIdentity();
2682 try {
2683 showCurrentInputLocked(flags, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 } finally {
2685 Binder.restoreCallingIdentity(ident);
2686 }
2687 }
2688 }
2689
2690 void setEnabledSessionInMainThread(SessionState session) {
2691 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002692 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002694 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002695 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 } catch (RemoteException e) {
2697 }
2698 }
2699 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09002700 if (mEnabledSession != null && mEnabledSession.session != null) {
2701 try {
2702 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
2703 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
2704 } catch (RemoteException e) {
2705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707 }
2708 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002709
satok42c5a162011-05-26 16:46:14 +09002710 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002712 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09002714 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07002715 final boolean showAuxSubtypes;
2716 switch (msg.arg1) {
2717 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
2718 // This is undocumented so far, but IMM#showInputMethodPicker() has been
2719 // implemented so that auxiliary subtypes will be excluded when the soft
2720 // keyboard is invisible.
2721 showAuxSubtypes = mInputShown;
2722 break;
2723 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
2724 showAuxSubtypes = true;
2725 break;
2726 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
2727 showAuxSubtypes = false;
2728 break;
2729 default:
2730 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
2731 return false;
2732 }
2733 showInputMethodMenu(showAuxSubtypes);
satokab751aa2010-09-14 19:17:36 +09002734 return true;
2735
satok47a44912010-10-06 16:03:58 +09002736 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08002737 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09002738 return true;
2739
2740 case MSG_SHOW_IM_CONFIG:
2741 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09002742 return true;
2743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 case MSG_UNBIND_INPUT:
2747 try {
2748 ((IInputMethod)msg.obj).unbindInput();
2749 } catch (RemoteException e) {
2750 // There is nothing interesting about the method dying.
2751 }
2752 return true;
2753 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002754 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 try {
2756 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
2757 } catch (RemoteException e) {
2758 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002759 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 return true;
2761 case MSG_SHOW_SOFT_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002762 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 try {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002764 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Craig Mautner6efb4c72013-03-13 10:17:41 -07002765 + msg.arg1 + ", " + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002766 ((IInputMethod)args.arg1).showSoftInput(msg.arg1, (ResultReceiver)args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 } 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_HIDE_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 + ".hideSoftInput(0, "
Craig Mautner6efb4c72013-03-13 10:17:41 -07002775 + args.arg2 + ")");
Craig Mautnerca0ac712013-03-14 09:43:02 -07002776 ((IInputMethod)args.arg1).hideSoftInput(0, (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_ATTACH_TOKEN:
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, "Sending attach of token: " + args.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 ((IInputMethod)args.arg1).attachToken((IBinder)args.arg2);
2786 } catch (RemoteException e) {
2787 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002788 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002790 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002791 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002792 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07002793 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002795 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002797 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07002798 // Dispose the channel if the input method is not local to this process
2799 // because the remote proxy will get its own copy when unparceled.
2800 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002801 channel.dispose();
2802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002804 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07002806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 case MSG_START_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002810 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 try {
2812 SessionState session = (SessionState)args.arg1;
2813 setEnabledSessionInMainThread(session);
2814 session.method.startInput((IInputContext)args.arg2,
2815 (EditorInfo)args.arg3);
2816 } catch (RemoteException e) {
2817 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002818 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 return true;
2820 case MSG_RESTART_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002821 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 try {
2823 SessionState session = (SessionState)args.arg1;
2824 setEnabledSessionInMainThread(session);
2825 session.method.restartInput((IInputContext)args.arg2,
2826 (EditorInfo)args.arg3);
2827 } catch (RemoteException e) {
2828 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002829 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 return true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002833
Yohei Yukawa33e81792015-11-17 21:14:42 -08002834 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08002836 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 } catch (RemoteException e) {
2838 // There is nothing interesting about the last client dying.
2839 }
2840 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08002841 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002842 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07002843 IInputMethodClient client = (IInputMethodClient)args.arg1;
2844 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07002846 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002848 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07002849 } finally {
2850 // Dispose the channel if the input method is not local to this process
2851 // because the remote proxy will get its own copy when unparceled.
2852 if (res.channel != null && Binder.isProxy(client)) {
2853 res.channel.dispose();
2854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07002856 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07002858 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002859 case MSG_SET_ACTIVE:
2860 try {
2861 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0);
2862 } catch (RemoteException e) {
2863 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
2864 + ((ClientState)msg.obj).pid + " uid "
2865 + ((ClientState)msg.obj).uid);
2866 }
2867 return true;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002868 case MSG_SET_INTERACTIVE:
2869 handleSetInteractive(msg.arg1 != 0);
2870 return true;
Yohei Yukawaae61f712015-12-09 13:00:10 -08002871 case MSG_SWITCH_IME:
2872 handleSwitchInputMethod(msg.arg1 != 0);
2873 return true;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002874 case MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER: {
2875 final int sequenceNumber = msg.arg1;
Yohei Yukawa080fa342014-08-31 16:10:05 -07002876 final ClientState clientState = (ClientState)msg.obj;
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002877 try {
Yohei Yukawa080fa342014-08-31 16:10:05 -07002878 clientState.client.setUserActionNotificationSequenceNumber(sequenceNumber);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002879 } catch (RemoteException e) {
2880 Slog.w(TAG, "Got RemoteException sending "
2881 + "setUserActionNotificationSequenceNumber("
2882 + sequenceNumber + ") notification to pid "
Yohei Yukawa080fa342014-08-31 16:10:05 -07002883 + clientState.pid + " uid "
2884 + clientState.uid);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09002885 }
2886 return true;
2887 }
satok01038492012-04-09 21:08:27 +09002888
2889 // --------------------------------------------------------------
2890 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07002891 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09002892 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
2894 return false;
2895 }
2896
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002897 private void handleSetInteractive(final boolean interactive) {
2898 synchronized (mMethodMap) {
2899 mIsInteractive = interactive;
2900 updateSystemUiLocked(mCurToken, interactive ? mImeWindowVis : 0, mBackDisposition);
2901
2902 // Inform the current client of the change in active status
2903 if (mCurClient != null && mCurClient.client != null) {
2904 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
2905 MSG_SET_ACTIVE, mIsInteractive ? 1 : 0, mCurClient));
2906 }
2907 }
2908 }
2909
Yohei Yukawaae61f712015-12-09 13:00:10 -08002910 private void handleSwitchInputMethod(final boolean forwardDirection) {
2911 synchronized (mMethodMap) {
2912 // TODO: Support forwardDirection.
2913 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
2914 false, mMethodMap.get(mCurMethodId), mCurrentSubtype);
2915 if (nextSubtype == null) {
2916 return;
2917 }
2918 setInputMethodLocked(nextSubtype.mImi.getId(), nextSubtype.mSubtypeId);
2919 }
2920 }
2921
satokdc9ddae2011-10-06 12:22:36 +09002922 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002923 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
2924 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09002925 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09002926 if (DEBUG) {
2927 Slog.d(TAG, "New default IME was selected: " + imi.getId());
2928 }
satok723a27e2010-11-11 14:58:11 +09002929 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002930 return true;
2931 }
2932
2933 return false;
2934 }
2935
Yohei Yukawa94e33302016-02-12 19:37:03 -08002936 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002937 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002938 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07002939 + " \n ------ caller=" + Debug.getCallers(10));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002940 }
Yohei Yukawa94e33302016-02-12 19:37:03 -08002941 mMethodList.clear();
2942 mMethodMap.clear();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002943
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002944 // Use for queryIntentServicesAsUser
2945 final PackageManager pm = mContext.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946
Yohei Yukawaed4952a2016-02-17 07:57:25 -08002947 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
2948 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
2949 // services depending on the unlock state for the specified user.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002950 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 new Intent(InputMethod.SERVICE_INTERFACE),
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002952 PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2953 mSettings.getCurrentUserId());
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002954
satoke7c6998e2011-06-03 17:57:59 +09002955 final HashMap<String, List<InputMethodSubtype>> additionalSubtypes =
2956 mFileManager.getAllAdditionalInputMethodSubtypes();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 for (int i = 0; i < services.size(); ++i) {
2958 ResolveInfo ri = services.get(i);
2959 ServiceInfo si = ri.serviceInfo;
2960 ComponentName compName = new ComponentName(si.packageName, si.name);
2961 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(
2962 si.permission)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002963 Slog.w(TAG, "Skipping input method " + compName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 + ": it does not require the permission "
2965 + android.Manifest.permission.BIND_INPUT_METHOD);
2966 continue;
2967 }
2968
Joe Onorato8a9b2202010-02-26 18:56:32 -08002969 if (DEBUG) Slog.d(TAG, "Checking " + compName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970
2971 try {
satoke7c6998e2011-06-03 17:57:59 +09002972 InputMethodInfo p = new InputMethodInfo(mContext, ri, additionalSubtypes);
Yohei Yukawa94e33302016-02-12 19:37:03 -08002973 mMethodList.add(p);
Amith Yamasanie861ec12010-03-24 21:39:27 -07002974 final String id = p.getId();
Yohei Yukawa94e33302016-02-12 19:37:03 -08002975 mMethodMap.put(id, p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976
2977 if (DEBUG) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09002978 Slog.d(TAG, "Found an input method " + p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07002980 } catch (XmlPullParserException | IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002981 Slog.w(TAG, "Unable to load input method " + compName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
2983 }
Brandon Ballinger6da35a02009-10-21 00:38:13 -07002984
Yohei Yukawa859df052016-02-17 07:56:46 -08002985 // TODO: The following code should find better place to live.
2986 if (!resetDefaultEnabledIme) {
2987 boolean enabledImeFound = false;
2988 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
2989 final int N = enabledImes.size();
2990 for (int i = 0; i < N; ++i) {
2991 final InputMethodInfo imi = enabledImes.get(i);
2992 if (mMethodList.contains(imi)) {
2993 enabledImeFound = true;
2994 break;
2995 }
2996 }
2997 if (!enabledImeFound) {
2998 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
2999 resetDefaultEnabledIme = true;
3000 resetSelectedInputMethodAndSubtypeLocked("");
3001 }
3002 }
3003
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003004 if (resetDefaultEnabledIme) {
3005 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa94e33302016-02-12 19:37:03 -08003006 InputMethodUtils.getDefaultEnabledImes(mContext, mSystemReady, mMethodList);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003007 final int N = defaultEnabledIme.size();
3008 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09003009 final InputMethodInfo imi = defaultEnabledIme.get(i);
3010 if (DEBUG) {
3011 Slog.d(TAG, "--- enable ime = " + imi);
3012 }
3013 setInputMethodEnabledLocked(imi.getId(), true);
3014 }
3015 }
3016
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003017 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09003018 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003019 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09003020 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
3021 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003022 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09003023 }
3024 } else {
3025 // Double check that the default IME is certainly enabled.
3026 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07003027 }
3028 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09003029 // Here is not the perfect place to reset the switching controller. Ideally
3030 // mSwitchingController and mSettings should be able to share the same state.
3031 // TODO: Make sure that mSwitchingController and mSettings are sharing the
3032 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09003033 mSwitchingController.resetCircularListLocked(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003037
satok217f5482010-12-15 05:19:19 +09003038 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09003039 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09003040 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09003041 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3042 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09003043 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09003044 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09003045 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08003046 final int userId;
3047 synchronized (mMethodMap) {
3048 userId = mSettings.getCurrentUserId();
3049 }
3050 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09003051 }
3052
3053 private void showConfigureInputMethods() {
3054 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
3055 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
3056 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
3057 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09003058 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09003059 }
3060
satok2c93efc2012-04-02 19:33:47 +09003061 private boolean isScreenLocked() {
3062 return mKeyguardManager != null
3063 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
3064 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003065
Seigo Nonaka14e13912015-05-06 21:04:13 -07003066 private void showInputMethodMenu(boolean showAuxSubtypes) {
3067 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 final Context context = mContext;
satok2c93efc2012-04-02 19:33:47 +09003070 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003071
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003072 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003073 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003074 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003075
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003076 synchronized (mMethodMap) {
satokbb4aa062011-01-19 21:40:27 +09003077 final HashMap<InputMethodInfo, List<InputMethodSubtype>> immis =
Satoshi Kataokad787f692013-10-26 04:44:21 +09003078 mSettings.getExplicitlyOrImplicitlyEnabledInputMethodsAndSubtypeListLocked(
3079 mContext);
satok7f35c8c2010-10-07 21:13:11 +09003080 if (immis == null || immis.size() == 0) {
3081 return;
3082 }
3083
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003084 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085
satok688bd472012-02-09 20:09:17 +09003086 final List<ImeSubtypeListItem> imList =
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003087 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
Yohei Yukawa5f8e7312015-12-10 00:58:55 -08003088 showAuxSubtypes, isScreenLocked);
satok913a8922010-08-26 21:53:41 +09003089
satokc3690562012-01-10 20:14:43 +09003090 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003091 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09003092 if (currentSubtype != null) {
3093 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003094 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
3095 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09003096 }
3097 }
3098
Ken Wakasa761eb372011-03-04 19:06:18 +09003099 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09003100 mIms = new InputMethodInfo[N];
3101 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003102 int checkedItem = 0;
3103 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09003104 final ImeSubtypeListItem item = imList.get(i);
3105 mIms[i] = item.mImi;
3106 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003107 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09003108 int subtypeId = mSubtypeIds[i];
3109 if ((subtypeId == NOT_A_SUBTYPE_ID)
3110 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
3111 || (subtypeId == lastInputMethodSubtypeId)) {
3112 checkedItem = i;
3113 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07003114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08003116
3117 final Context settingsContext = new ContextThemeWrapper(context,
3118 com.android.internal.R.style.Theme_DeviceDefault_Settings);
3119
3120 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003121 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
3122 @Override
3123 public void onCancel(DialogInterface dialog) {
3124 hideInputMethodMenu();
3125 }
3126 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08003127
3128 final Context dialogContext = mDialogBuilder.getContext();
3129 final TypedArray a = dialogContext.obtainStyledAttributes(null,
3130 com.android.internal.R.styleable.DialogPreference,
3131 com.android.internal.R.attr.alertDialogStyle, 0);
3132 final Drawable dialogIcon = a.getDrawable(
3133 com.android.internal.R.styleable.DialogPreference_dialogIcon);
3134 a.recycle();
3135
3136 mDialogBuilder.setIcon(dialogIcon);
3137
Yohei Yukawad34e1482016-02-11 08:03:52 -08003138 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09003139 final View tv = inflater.inflate(
3140 com.android.internal.R.layout.input_method_switch_dialog_title, null);
3141 mDialogBuilder.setCustomTitle(tv);
3142
3143 // Setup layout for a toggle switch of the hardware keyboard
3144 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003145 mSwitchingDialogTitleView
3146 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07003147 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003148 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003149 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003150 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07003151 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003152 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
3153 @Override
3154 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07003155 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003156 // Ensure that the input method dialog is dismissed when changing
3157 // the hardware keyboard state.
3158 hideInputMethodMenu();
3159 }
3160 });
3161
Alan Viverette505e3ab2014-11-24 15:22:11 -08003162 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003163 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
3164 final OnClickListener choiceListener = new OnClickListener() {
3165 @Override
3166 public void onClick(final DialogInterface dialog, final int which) {
3167 synchronized (mMethodMap) {
3168 if (mIms == null || mIms.length <= which || mSubtypeIds == null
3169 || mSubtypeIds.length <= which) {
3170 return;
satok01038492012-04-09 21:08:27 +09003171 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003172 final InputMethodInfo im = mIms[which];
3173 int subtypeId = mSubtypeIds[which];
3174 adapter.mCheckedItem = which;
3175 adapter.notifyDataSetChanged();
3176 hideInputMethodMenu();
3177 if (im != null) {
3178 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
3179 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08003180 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003181 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003183 }
3184 }
3185 };
3186 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187
Seigo Nonakad4474cb2015-05-04 16:53:24 -07003188 if (!isScreenLocked) {
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003189 final OnClickListener positiveListener = new OnClickListener() {
3190 @Override
3191 public void onClick(DialogInterface dialog, int whichButton) {
3192 showConfigureInputMethods();
3193 }
3194 };
satok82beadf2010-12-27 19:03:06 +09003195 mDialogBuilder.setPositiveButton(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09003196 com.android.internal.R.string.configure_input_methods, positiveListener);
satok7f35c8c2010-10-07 21:13:11 +09003197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003199 mSwitchingDialog.setCanceledOnTouchOutside(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 mSwitchingDialog.getWindow().setType(
3201 WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Satoshi Kataokac86884c2012-10-09 15:20:29 +09003202 mSwitchingDialog.getWindow().getAttributes().privateFlags |=
3203 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Dianne Hackborne3a7f622011-03-03 21:48:24 -08003204 mSwitchingDialog.getWindow().getAttributes().setTitle("Select input method");
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003205 updateSystemUi(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 mSwitchingDialog.show();
3207 }
3208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003209
Ken Wakasa05dbb652011-08-22 15:22:43 +09003210 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
3211 private final LayoutInflater mInflater;
3212 private final int mTextViewResourceId;
3213 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09003214 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09003215 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
3216 List<ImeSubtypeListItem> itemsList, int checkedItem) {
3217 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08003218
Ken Wakasa05dbb652011-08-22 15:22:43 +09003219 mTextViewResourceId = textViewResourceId;
3220 mItemsList = itemsList;
3221 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08003222 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09003223 }
3224
3225 @Override
3226 public View getView(int position, View convertView, ViewGroup parent) {
3227 final View view = convertView != null ? convertView
3228 : mInflater.inflate(mTextViewResourceId, null);
3229 if (position < 0 || position >= mItemsList.size()) return view;
3230 final ImeSubtypeListItem item = mItemsList.get(position);
3231 final CharSequence imeName = item.mImeName;
3232 final CharSequence subtypeName = item.mSubtypeName;
3233 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
3234 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
3235 if (TextUtils.isEmpty(subtypeName)) {
3236 firstTextView.setText(imeName);
3237 secondTextView.setVisibility(View.GONE);
3238 } else {
3239 firstTextView.setText(subtypeName);
3240 secondTextView.setText(imeName);
3241 secondTextView.setVisibility(View.VISIBLE);
3242 }
3243 final RadioButton radioButton =
3244 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
3245 radioButton.setChecked(position == mCheckedItem);
3246 return view;
3247 }
3248 }
3249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003251 synchronized (mMethodMap) {
3252 hideInputMethodMenuLocked();
3253 }
3254 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003255
The Android Open Source Project10592532009-03-18 17:39:46 -07003256 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003257 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258
The Android Open Source Project10592532009-03-18 17:39:46 -07003259 if (mSwitchingDialog != null) {
3260 mSwitchingDialog.dismiss();
3261 mSwitchingDialog = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003263
Seigo Nonakad9eb9112015-05-26 20:54:43 +09003264 updateSystemUiLocked(mCurToken, mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07003265 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003266 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003270
satok42c5a162011-05-26 16:46:14 +09003271 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 public boolean setInputMethodEnabled(String id, boolean enabled) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003273 // TODO: Make this work even for non-current users?
3274 if (!calledFromValidUser()) {
3275 return false;
3276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 synchronized (mMethodMap) {
3278 if (mContext.checkCallingOrSelfPermission(
3279 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3280 != PackageManager.PERMISSION_GRANTED) {
3281 throw new SecurityException(
3282 "Requires permission "
3283 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
3284 }
Anna Galusza9b278112016-01-04 11:37:37 -08003285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 long ident = Binder.clearCallingIdentity();
3287 try {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003288 return setInputMethodEnabledLocked(id, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 } finally {
3290 Binder.restoreCallingIdentity(ident);
3291 }
3292 }
3293 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003294
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003295 boolean setInputMethodEnabledLocked(String id, boolean enabled) {
3296 // Make sure this is a valid input method.
3297 InputMethodInfo imm = mMethodMap.get(id);
3298 if (imm == null) {
satokd87c2592010-09-29 11:52:06 +09003299 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003300 }
3301
satokd87c2592010-09-29 11:52:06 +09003302 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
3303 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003304
satokd87c2592010-09-29 11:52:06 +09003305 if (enabled) {
3306 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
3307 if (pair.first.equals(id)) {
3308 // We are enabling this input method, but it is already enabled.
3309 // Nothing to do. The previous state was enabled.
3310 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003311 }
3312 }
satokd87c2592010-09-29 11:52:06 +09003313 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
3314 // Previous state was disabled.
3315 return false;
3316 } else {
3317 StringBuilder builder = new StringBuilder();
3318 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
3319 builder, enabledInputMethodsList, id)) {
3320 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003321 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09003322 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
3323 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
3324 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09003325 }
3326 // Previous state was enabled.
3327 return true;
3328 } else {
3329 // We are disabling the input method but it is already disabled.
3330 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003331 return false;
3332 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003333 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003334 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003335
satok723a27e2010-11-11 14:58:11 +09003336 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
3337 boolean setSubtypeOnly) {
3338 // Update the history of InputMethod and Subtype
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003339 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
satok723a27e2010-11-11 14:58:11 +09003340
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003341 mCurUserActionNotificationSequenceNumber =
3342 Math.max(mCurUserActionNotificationSequenceNumber + 1, 1);
3343 if (DEBUG) {
3344 Slog.d(TAG, "Bump mCurUserActionNotificationSequenceNumber:"
3345 + mCurUserActionNotificationSequenceNumber);
3346 }
3347
3348 if (mCurClient != null && mCurClient.client != null) {
3349 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
3350 MSG_SET_USER_ACTION_NOTIFICATION_SEQUENCE_NUMBER,
Yohei Yukawa080fa342014-08-31 16:10:05 -07003351 mCurUserActionNotificationSequenceNumber, mCurClient));
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003352 }
3353
satok723a27e2010-11-11 14:58:11 +09003354 // Set Subtype here
3355 if (imi == null || subtypeId < 0) {
3356 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08003357 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09003358 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09003359 if (subtypeId < imi.getSubtypeCount()) {
3360 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
3361 mSettings.putSelectedSubtype(subtype.hashCode());
3362 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09003363 } else {
3364 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09003365 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003366 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09003367 }
satokab751aa2010-09-14 19:17:36 +09003368 }
satok723a27e2010-11-11 14:58:11 +09003369
Yohei Yukawa68645a62016-02-17 07:54:20 -08003370 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09003371 // Set InputMethod here
3372 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
3373 }
3374 }
3375
3376 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
3377 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
3378 int lastSubtypeId = NOT_A_SUBTYPE_ID;
3379 // newDefaultIme is empty when there is no candidate for the selected IME.
3380 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
3381 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
3382 if (subtypeHashCode != null) {
3383 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003384 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok723a27e2010-11-11 14:58:11 +09003385 imi, Integer.valueOf(subtypeHashCode));
3386 } catch (NumberFormatException e) {
3387 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
3388 }
3389 }
3390 }
3391 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09003392 }
3393
satok4e4569d2010-11-19 18:45:53 +09003394 // If there are no selected shortcuts, tries finding the most applicable ones.
3395 private Pair<InputMethodInfo, InputMethodSubtype>
3396 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(String mode) {
3397 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
3398 InputMethodInfo mostApplicableIMI = null;
satokcd7cd292010-11-20 15:46:23 +09003399 InputMethodSubtype mostApplicableSubtype = null;
satok4e4569d2010-11-19 18:45:53 +09003400 boolean foundInSystemIME = false;
3401
3402 // Search applicable subtype for each InputMethodInfo
3403 for (InputMethodInfo imi: imis) {
satok7599a7f2010-12-22 13:45:23 +09003404 final String imiId = imi.getId();
3405 if (foundInSystemIME && !imiId.equals(mCurMethodId)) {
3406 continue;
3407 }
satokcd7cd292010-11-20 15:46:23 +09003408 InputMethodSubtype subtype = null;
satokdf31ae62011-01-15 06:19:44 +09003409 final List<InputMethodSubtype> enabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003410 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
satokdf31ae62011-01-15 06:19:44 +09003411 // 1. Search by the current subtype's locale from enabledSubtypes.
satok4e4569d2010-11-19 18:45:53 +09003412 if (mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003413 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003414 mRes, enabledSubtypes, mode, mCurrentSubtype.getLocale(), false);
satok4e4569d2010-11-19 18:45:53 +09003415 }
satokdf31ae62011-01-15 06:19:44 +09003416 // 2. Search by the system locale from enabledSubtypes.
3417 // 3. Search the first enabled subtype matched with mode from enabledSubtypes.
satokcd7cd292010-11-20 15:46:23 +09003418 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003419 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satokdf31ae62011-01-15 06:19:44 +09003420 mRes, enabledSubtypes, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003421 }
satoka86f5e42011-09-02 17:12:42 +09003422 final ArrayList<InputMethodSubtype> overridingImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003423 InputMethodUtils.getOverridingImplicitlyEnabledSubtypes(imi, mode);
satoka86f5e42011-09-02 17:12:42 +09003424 final ArrayList<InputMethodSubtype> subtypesForSearch =
3425 overridingImplicitlyEnabledSubtypes.isEmpty()
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003426 ? InputMethodUtils.getSubtypes(imi)
3427 : overridingImplicitlyEnabledSubtypes;
satok7599a7f2010-12-22 13:45:23 +09003428 // 4. Search by the current subtype's locale from all subtypes.
3429 if (subtype == null && mCurrentSubtype != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003430 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003431 mRes, subtypesForSearch, mode, mCurrentSubtype.getLocale(), false);
satok7599a7f2010-12-22 13:45:23 +09003432 }
3433 // 5. Search by the system locale from all subtypes.
3434 // 6. Search the first enabled subtype matched with mode from all subtypes.
satokcd7cd292010-11-20 15:46:23 +09003435 if (subtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003436 subtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
satoka86f5e42011-09-02 17:12:42 +09003437 mRes, subtypesForSearch, mode, null, true);
satok4e4569d2010-11-19 18:45:53 +09003438 }
satokcd7cd292010-11-20 15:46:23 +09003439 if (subtype != null) {
satok7599a7f2010-12-22 13:45:23 +09003440 if (imiId.equals(mCurMethodId)) {
satok4e4569d2010-11-19 18:45:53 +09003441 // The current input method is the most applicable IME.
3442 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003443 mostApplicableSubtype = subtype;
satok4e4569d2010-11-19 18:45:53 +09003444 break;
satok7599a7f2010-12-22 13:45:23 +09003445 } else if (!foundInSystemIME) {
satok4e4569d2010-11-19 18:45:53 +09003446 // The system input method is 2nd applicable IME.
3447 mostApplicableIMI = imi;
satokcd7cd292010-11-20 15:46:23 +09003448 mostApplicableSubtype = subtype;
satok7599a7f2010-12-22 13:45:23 +09003449 if ((imi.getServiceInfo().applicationInfo.flags
3450 & ApplicationInfo.FLAG_SYSTEM) != 0) {
3451 foundInSystemIME = true;
3452 }
satok4e4569d2010-11-19 18:45:53 +09003453 }
3454 }
3455 }
3456 if (DEBUG) {
satokcd7cd292010-11-20 15:46:23 +09003457 if (mostApplicableIMI != null) {
3458 Slog.w(TAG, "Most applicable shortcut input method was:"
3459 + mostApplicableIMI.getId());
3460 if (mostApplicableSubtype != null) {
3461 Slog.w(TAG, "Most applicable shortcut input method subtype was:"
3462 + "," + mostApplicableSubtype.getMode() + ","
3463 + mostApplicableSubtype.getLocale());
3464 }
3465 }
satok4e4569d2010-11-19 18:45:53 +09003466 }
satokcd7cd292010-11-20 15:46:23 +09003467 if (mostApplicableIMI != null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003468 return new Pair<> (mostApplicableIMI, mostApplicableSubtype);
satok4e4569d2010-11-19 18:45:53 +09003469 } else {
3470 return null;
3471 }
3472 }
3473
satokab751aa2010-09-14 19:17:36 +09003474 /**
3475 * @return Return the current subtype of this input method.
3476 */
satok42c5a162011-05-26 16:46:14 +09003477 @Override
satokab751aa2010-09-14 19:17:36 +09003478 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003479 // TODO: Make this work even for non-current users?
3480 if (!calledFromValidUser()) {
3481 return null;
3482 }
3483 synchronized (mMethodMap) {
3484 return getCurrentInputMethodSubtypeLocked();
3485 }
3486 }
3487
3488 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09003489 if (mCurMethodId == null) {
3490 return null;
3491 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003492 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003493 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3494 if (imi == null || imi.getSubtypeCount() == 0) {
3495 return null;
satok4e4569d2010-11-19 18:45:53 +09003496 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003497 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003498 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
3499 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003500 if (subtypeId == NOT_A_SUBTYPE_ID) {
3501 // If there are no selected subtypes, the framework will try to find
3502 // the most applicable subtype from explicitly or implicitly enabled
3503 // subtypes.
3504 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003505 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003506 // If there is only one explicitly or implicitly enabled subtype,
3507 // just returns it.
3508 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
3509 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
3510 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003511 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003512 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003513 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003514 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003515 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003516 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
3517 true);
satok4e4569d2010-11-19 18:45:53 +09003518 }
satok3ef8b292010-11-23 06:06:29 +09003519 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003520 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003521 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09003522 }
3523 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003524 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09003525 }
3526
satok4e4569d2010-11-19 18:45:53 +09003527 // TODO: We should change the return type from List to List<Parcelable>
satokdbf29502011-08-25 15:28:23 +09003528 @SuppressWarnings("rawtypes")
satoke7c6998e2011-06-03 17:57:59 +09003529 @Override
satok4e4569d2010-11-19 18:45:53 +09003530 public List getShortcutInputMethodsAndSubtypes() {
3531 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003532 ArrayList<Object> ret = new ArrayList<>();
satokf3db1af2010-11-23 13:34:33 +09003533 if (mShortcutInputMethodsAndSubtypes.size() == 0) {
satok4e4569d2010-11-19 18:45:53 +09003534 // If there are no selected shortcut subtypes, the framework will try to find
3535 // the most applicable subtype from all subtypes whose mode is
3536 // SUBTYPE_MODE_VOICE. This is an exceptional case, so we will hardcode the mode.
satokf3db1af2010-11-23 13:34:33 +09003537 Pair<InputMethodInfo, InputMethodSubtype> info =
3538 findLastResortApplicableShortcutInputMethodAndSubtypeLocked(
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003539 InputMethodUtils.SUBTYPE_MODE_VOICE);
satok7599a7f2010-12-22 13:45:23 +09003540 if (info != null) {
satok3da92232011-01-11 22:46:30 +09003541 ret.add(info.first);
3542 ret.add(info.second);
satok7599a7f2010-12-22 13:45:23 +09003543 }
satok3da92232011-01-11 22:46:30 +09003544 return ret;
satokf3db1af2010-11-23 13:34:33 +09003545 }
satokf3db1af2010-11-23 13:34:33 +09003546 for (InputMethodInfo imi: mShortcutInputMethodsAndSubtypes.keySet()) {
3547 ret.add(imi);
3548 for (InputMethodSubtype subtype: mShortcutInputMethodsAndSubtypes.get(imi)) {
3549 ret.add(subtype);
satok4e4569d2010-11-19 18:45:53 +09003550 }
3551 }
satokf3db1af2010-11-23 13:34:33 +09003552 return ret;
satok4e4569d2010-11-19 18:45:53 +09003553 }
3554 }
3555
satok42c5a162011-05-26 16:46:14 +09003556 @Override
satokb66d2872010-11-10 01:04:04 +09003557 public boolean setCurrentInputMethodSubtype(InputMethodSubtype subtype) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003558 // TODO: Make this work even for non-current users?
3559 if (!calledFromValidUser()) {
3560 return false;
3561 }
satokb66d2872010-11-10 01:04:04 +09003562 synchronized (mMethodMap) {
3563 if (subtype != null && mCurMethodId != null) {
3564 InputMethodInfo imi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003565 int subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(imi, subtype.hashCode());
satokb66d2872010-11-10 01:04:04 +09003566 if (subtypeId != NOT_A_SUBTYPE_ID) {
3567 setInputMethodLocked(mCurMethodId, subtypeId);
3568 return true;
3569 }
3570 }
3571 return false;
3572 }
3573 }
3574
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003575 // TODO: Cache the state for each user and reset when the cached user is removed.
satoke7c6998e2011-06-03 17:57:59 +09003576 private static class InputMethodFileManager {
3577 private static final String SYSTEM_PATH = "system";
3578 private static final String INPUT_METHOD_PATH = "inputmethod";
3579 private static final String ADDITIONAL_SUBTYPES_FILE_NAME = "subtypes.xml";
3580 private static final String NODE_SUBTYPES = "subtypes";
3581 private static final String NODE_SUBTYPE = "subtype";
3582 private static final String NODE_IMI = "imi";
3583 private static final String ATTR_ID = "id";
3584 private static final String ATTR_LABEL = "label";
3585 private static final String ATTR_ICON = "icon";
3586 private static final String ATTR_IME_SUBTYPE_LOCALE = "imeSubtypeLocale";
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003587 private static final String ATTR_IME_SUBTYPE_LANGUAGE_TAG = "languageTag";
satoke7c6998e2011-06-03 17:57:59 +09003588 private static final String ATTR_IME_SUBTYPE_MODE = "imeSubtypeMode";
3589 private static final String ATTR_IME_SUBTYPE_EXTRA_VALUE = "imeSubtypeExtraValue";
3590 private static final String ATTR_IS_AUXILIARY = "isAuxiliary";
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003591 private static final String ATTR_IS_ASCII_CAPABLE = "isAsciiCapable";
satoke7c6998e2011-06-03 17:57:59 +09003592 private final AtomicFile mAdditionalInputMethodSubtypeFile;
3593 private final HashMap<String, InputMethodInfo> mMethodMap;
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003594 private final HashMap<String, List<InputMethodSubtype>> mAdditionalSubtypesMap =
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003595 new HashMap<>();
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003596 public InputMethodFileManager(HashMap<String, InputMethodInfo> methodMap, int userId) {
satoke7c6998e2011-06-03 17:57:59 +09003597 if (methodMap == null) {
3598 throw new NullPointerException("methodMap is null");
3599 }
3600 mMethodMap = methodMap;
Xiaohui Chen7c696362015-09-16 09:56:14 -07003601 final File systemDir = userId == UserHandle.USER_SYSTEM
Satoshi Kataoka5ade83b2012-09-26 22:59:41 +09003602 ? new File(Environment.getDataDirectory(), SYSTEM_PATH)
3603 : Environment.getUserSystemDirectory(userId);
satoke7c6998e2011-06-03 17:57:59 +09003604 final File inputMethodDir = new File(systemDir, INPUT_METHOD_PATH);
Yohei Yukawadf5af482015-08-04 22:11:11 -07003605 if (!inputMethodDir.exists() && !inputMethodDir.mkdirs()) {
satoke7c6998e2011-06-03 17:57:59 +09003606 Slog.w(TAG, "Couldn't create dir.: " + inputMethodDir.getAbsolutePath());
3607 }
3608 final File subtypeFile = new File(inputMethodDir, ADDITIONAL_SUBTYPES_FILE_NAME);
3609 mAdditionalInputMethodSubtypeFile = new AtomicFile(subtypeFile);
3610 if (!subtypeFile.exists()) {
3611 // If "subtypes.xml" doesn't exist, create a blank file.
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003612 writeAdditionalInputMethodSubtypes(
3613 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, methodMap);
satoke7c6998e2011-06-03 17:57:59 +09003614 } else {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003615 readAdditionalInputMethodSubtypes(
3616 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile);
satoke7c6998e2011-06-03 17:57:59 +09003617 }
3618 }
3619
3620 private void deleteAllInputMethodSubtypes(String imiId) {
3621 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003622 mAdditionalSubtypesMap.remove(imiId);
3623 writeAdditionalInputMethodSubtypes(
3624 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003625 }
3626 }
3627
3628 public void addInputMethodSubtypes(
satok4a28bde2011-06-29 21:03:40 +09003629 InputMethodInfo imi, InputMethodSubtype[] additionalSubtypes) {
satoke7c6998e2011-06-03 17:57:59 +09003630 synchronized (mMethodMap) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003631 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003632 final int N = additionalSubtypes.length;
3633 for (int i = 0; i < N; ++i) {
3634 final InputMethodSubtype subtype = additionalSubtypes[i];
satoked2b24e2011-08-31 18:03:21 +09003635 if (!subtypes.contains(subtype)) {
satoke7c6998e2011-06-03 17:57:59 +09003636 subtypes.add(subtype);
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003637 } else {
3638 Slog.w(TAG, "Duplicated subtype definition found: "
3639 + subtype.getLocale() + ", " + subtype.getMode());
satoke7c6998e2011-06-03 17:57:59 +09003640 }
3641 }
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003642 mAdditionalSubtypesMap.put(imi.getId(), subtypes);
3643 writeAdditionalInputMethodSubtypes(
3644 mAdditionalSubtypesMap, mAdditionalInputMethodSubtypeFile, mMethodMap);
satoke7c6998e2011-06-03 17:57:59 +09003645 }
3646 }
3647
3648 public HashMap<String, List<InputMethodSubtype>> getAllAdditionalInputMethodSubtypes() {
3649 synchronized (mMethodMap) {
Satoshi Kataokae62e6d82012-07-02 18:45:43 +09003650 return mAdditionalSubtypesMap;
satoke7c6998e2011-06-03 17:57:59 +09003651 }
3652 }
3653
3654 private static void writeAdditionalInputMethodSubtypes(
3655 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile,
3656 HashMap<String, InputMethodInfo> methodMap) {
3657 // Safety net for the case that this function is called before methodMap is set.
3658 final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0;
3659 FileOutputStream fos = null;
3660 try {
3661 fos = subtypesFile.startWrite();
3662 final XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003663 out.setOutput(fos, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003664 out.startDocument(null, true);
3665 out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
3666 out.startTag(null, NODE_SUBTYPES);
3667 for (String imiId : allSubtypes.keySet()) {
3668 if (isSetMethodMap && !methodMap.containsKey(imiId)) {
3669 Slog.w(TAG, "IME uninstalled or not valid.: " + imiId);
3670 continue;
3671 }
3672 out.startTag(null, NODE_IMI);
3673 out.attribute(null, ATTR_ID, imiId);
3674 final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId);
3675 final int N = subtypesList.size();
3676 for (int i = 0; i < N; ++i) {
3677 final InputMethodSubtype subtype = subtypesList.get(i);
3678 out.startTag(null, NODE_SUBTYPE);
3679 out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId()));
3680 out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId()));
3681 out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale());
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003682 out.attribute(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG,
3683 subtype.getLanguageTag());
satoke7c6998e2011-06-03 17:57:59 +09003684 out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode());
3685 out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue());
3686 out.attribute(null, ATTR_IS_AUXILIARY,
3687 String.valueOf(subtype.isAuxiliary() ? 1 : 0));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003688 out.attribute(null, ATTR_IS_ASCII_CAPABLE,
3689 String.valueOf(subtype.isAsciiCapable() ? 1 : 0));
satoke7c6998e2011-06-03 17:57:59 +09003690 out.endTag(null, NODE_SUBTYPE);
3691 }
3692 out.endTag(null, NODE_IMI);
3693 }
3694 out.endTag(null, NODE_SUBTYPES);
3695 out.endDocument();
3696 subtypesFile.finishWrite(fos);
3697 } catch (java.io.IOException e) {
3698 Slog.w(TAG, "Error writing subtypes", e);
3699 if (fos != null) {
3700 subtypesFile.failWrite(fos);
3701 }
3702 }
3703 }
3704
3705 private static void readAdditionalInputMethodSubtypes(
3706 HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile) {
3707 if (allSubtypes == null || subtypesFile == null) return;
3708 allSubtypes.clear();
Yohei Yukawa5894b432015-08-11 13:29:24 -07003709 try (final FileInputStream fis = subtypesFile.openRead()) {
satoke7c6998e2011-06-03 17:57:59 +09003710 final XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003711 parser.setInput(fis, StandardCharsets.UTF_8.name());
satoke7c6998e2011-06-03 17:57:59 +09003712 int type = parser.getEventType();
3713 // Skip parsing until START_TAG
3714 while ((type = parser.next()) != XmlPullParser.START_TAG
3715 && type != XmlPullParser.END_DOCUMENT) {}
3716 String firstNodeName = parser.getName();
3717 if (!NODE_SUBTYPES.equals(firstNodeName)) {
3718 throw new XmlPullParserException("Xml doesn't start with subtypes");
3719 }
3720 final int depth =parser.getDepth();
3721 String currentImiId = null;
3722 ArrayList<InputMethodSubtype> tempSubtypesArray = null;
3723 while (((type = parser.next()) != XmlPullParser.END_TAG
3724 || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
3725 if (type != XmlPullParser.START_TAG)
3726 continue;
3727 final String nodeName = parser.getName();
3728 if (NODE_IMI.equals(nodeName)) {
3729 currentImiId = parser.getAttributeValue(null, ATTR_ID);
3730 if (TextUtils.isEmpty(currentImiId)) {
3731 Slog.w(TAG, "Invalid imi id found in subtypes.xml");
3732 continue;
3733 }
Yohei Yukawab0377bb2015-08-10 21:06:30 -07003734 tempSubtypesArray = new ArrayList<>();
satoke7c6998e2011-06-03 17:57:59 +09003735 allSubtypes.put(currentImiId, tempSubtypesArray);
3736 } else if (NODE_SUBTYPE.equals(nodeName)) {
3737 if (TextUtils.isEmpty(currentImiId) || tempSubtypesArray == null) {
3738 Slog.w(TAG, "IME uninstalled or not valid.: " + currentImiId);
3739 continue;
3740 }
3741 final int icon = Integer.valueOf(
3742 parser.getAttributeValue(null, ATTR_ICON));
3743 final int label = Integer.valueOf(
3744 parser.getAttributeValue(null, ATTR_LABEL));
3745 final String imeSubtypeLocale =
3746 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LOCALE);
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003747 final String languageTag =
3748 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_LANGUAGE_TAG);
satoke7c6998e2011-06-03 17:57:59 +09003749 final String imeSubtypeMode =
3750 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_MODE);
3751 final String imeSubtypeExtraValue =
3752 parser.getAttributeValue(null, ATTR_IME_SUBTYPE_EXTRA_VALUE);
satok4a28bde2011-06-29 21:03:40 +09003753 final boolean isAuxiliary = "1".equals(String.valueOf(
3754 parser.getAttributeValue(null, ATTR_IS_AUXILIARY)));
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003755 final boolean isAsciiCapable = "1".equals(String.valueOf(
3756 parser.getAttributeValue(null, ATTR_IS_ASCII_CAPABLE)));
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003757 final InputMethodSubtype subtype = new InputMethodSubtypeBuilder()
3758 .setSubtypeNameResId(label)
3759 .setSubtypeIconResId(icon)
3760 .setSubtypeLocale(imeSubtypeLocale)
Yohei Yukawa868d19b2015-12-07 15:58:57 -08003761 .setLanguageTag(languageTag)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003762 .setSubtypeMode(imeSubtypeMode)
3763 .setSubtypeExtraValue(imeSubtypeExtraValue)
3764 .setIsAuxiliary(isAuxiliary)
Yohei Yukawa1e1a4472016-03-10 23:46:07 -08003765 .setIsAsciiCapable(isAsciiCapable)
Yohei Yukawa443c2ba2014-09-10 14:10:30 +09003766 .build();
satoke7c6998e2011-06-03 17:57:59 +09003767 tempSubtypesArray.add(subtype);
3768 }
3769 }
Yohei Yukawa5894b432015-08-11 13:29:24 -07003770 } catch (XmlPullParserException | IOException | NumberFormatException e) {
3771 Slog.w(TAG, "Error reading subtypes", e);
satoke7c6998e2011-06-03 17:57:59 +09003772 return;
satoke7c6998e2011-06-03 17:57:59 +09003773 }
3774 }
3775 }
3776
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003777 private static final class LocalServiceImpl implements InputMethodManagerInternal {
3778 @NonNull
3779 private final Handler mHandler;
3780
3781 LocalServiceImpl(@NonNull final Handler handler) {
3782 mHandler = handler;
3783 }
3784
3785 @Override
3786 public void setInteractive(boolean interactive) {
3787 // Do everything in handler so as not to block the caller.
3788 mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
3789 interactive ? 1 : 0, 0));
3790 }
Yohei Yukawaae61f712015-12-09 13:00:10 -08003791
3792 @Override
3793 public void switchInputMethod(boolean forwardDirection) {
3794 // Do everything in handler so as not to block the caller.
3795 mHandler.sendMessage(mHandler.obtainMessage(MSG_SWITCH_IME,
3796 forwardDirection ? 1 : 0, 0));
3797 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003798 }
3799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 @Override
3801 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3802 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3803 != PackageManager.PERMISSION_GRANTED) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 pw.println("Permission Denial: can't dump InputMethodManager from from pid="
3806 + Binder.getCallingPid()
3807 + ", uid=" + Binder.getCallingUid());
3808 return;
3809 }
3810
3811 IInputMethod method;
3812 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003813 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 synchronized (mMethodMap) {
3818 p.println("Current Input Method Manager state:");
3819 int N = mMethodList.size();
3820 p.println(" Input Methods:");
3821 for (int i=0; i<N; i++) {
3822 InputMethodInfo info = mMethodList.get(i);
3823 p.println(" InputMethod #" + i + ":");
3824 info.dump(p, " ");
3825 }
3826 p.println(" Clients:");
3827 for (ClientState ci : mClients.values()) {
3828 p.println(" Client " + ci + ":");
3829 p.println(" client=" + ci.client);
3830 p.println(" inputContext=" + ci.inputContext);
3831 p.println(" sessionRequested=" + ci.sessionRequested);
3832 p.println(" curSession=" + ci.curSession);
3833 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003834 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07003836 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
3837 p.println(" mCurFocusedWindow=" + mCurFocusedWindow);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003838 focusedWindowClient = mCurFocusedWindowClient;
3839 p.println(" mCurFocusedWindowClient=" + focusedWindowClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 p.println(" mCurId=" + mCurId + " mHaveConnect=" + mHaveConnection
3841 + " mBoundToMethod=" + mBoundToMethod);
3842 p.println(" mCurToken=" + mCurToken);
3843 p.println(" mCurIntent=" + mCurIntent);
3844 method = mCurMethod;
3845 p.println(" mCurMethod=" + mCurMethod);
3846 p.println(" mEnabledSession=" + mEnabledSession);
3847 p.println(" mShowRequested=" + mShowRequested
3848 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
3849 + " mShowForced=" + mShowForced
3850 + " mInputShown=" + mInputShown);
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003851 p.println(" mCurUserActionNotificationSequenceNumber="
3852 + mCurUserActionNotificationSequenceNumber);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07003853 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07003854 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07003855 p.println(" mSwitchingController:");
3856 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08003857 p.println(" mSettings:");
3858 mSettings.dumpLocked(p, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003860
Jeff Brownb88102f2010-09-08 11:49:43 -07003861 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 pw.flush();
3864 try {
3865 client.client.asBinder().dump(fd, args);
3866 } catch (RemoteException e) {
3867 p.println("Input method client dead: " + e);
3868 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003869 } else {
3870 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003872
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08003873 if (focusedWindowClient != null && client != focusedWindowClient) {
3874 p.println(" ");
3875 p.println("Warning: Current input method client doesn't match the last focused. "
3876 + "window.");
3877 p.println("Dumping input method client in the last focused window just in case.");
3878 p.println(" ");
3879 pw.flush();
3880 try {
3881 focusedWindowClient.client.asBinder().dump(fd, args);
3882 } catch (RemoteException e) {
3883 p.println("Input method client in focused window dead: " + e);
3884 }
3885 }
3886
Jeff Brownb88102f2010-09-08 11:49:43 -07003887 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 pw.flush();
3890 try {
3891 method.asBinder().dump(fd, args);
3892 } catch (RemoteException e) {
3893 p.println("Input method service dead: " + e);
3894 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003895 } else {
3896 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 }
3898 }
3899}