blob: b949d6bcf2e2545b50fb950aef2994f0f5a74774 [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
Yohei Yukawa603f4d02018-09-11 15:04:58 -070016package com.android.server.inputmethod;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017
lumarkef1965b2018-09-12 17:42:53 +080018import static android.view.Display.DEFAULT_DISPLAY;
lumark90120a82018-08-15 00:33:03 +080019import static android.view.Display.INVALID_DISPLAY;
Yohei Yukawa0569a182018-08-28 16:09:28 -070020
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070021import static java.lang.annotation.RetentionPolicy.SOURCE;
22
Yohei Yukawa926488d2017-12-11 17:24:55 -080023import android.Manifest;
Phil Weaver03a65b02018-07-19 16:07:57 -070024import android.accessibilityservice.AccessibilityService;
Tarandeep Singh75a92392018-01-12 14:58:59 -080025import android.annotation.AnyThread;
Yohei Yukawad6475a62017-04-17 10:35:27 -070026import android.annotation.BinderThread;
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +090027import android.annotation.ColorInt;
Yohei Yukawa41b094f2018-09-09 23:58:45 -070028import android.annotation.DrawableRes;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -070029import android.annotation.IntDef;
Yohei Yukawa930328c2017-10-18 20:19:53 -070030import android.annotation.MainThread;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070031import android.annotation.NonNull;
Yohei Yukawae13a20fa2015-09-30 19:11:32 -070032import android.annotation.Nullable;
Yohei Yukawa926488d2017-12-11 17:24:55 -080033import android.annotation.RequiresPermission;
Yohei Yukawa7b18aec2016-03-07 13:04:32 -080034import android.annotation.UserIdInt;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080035import android.app.ActivityManager;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -070036import android.app.ActivityManagerInternal;
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -070037import android.app.ActivityThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.app.AlertDialog;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070039import android.app.AppGlobals;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +090040import android.app.AppOpsManager;
satokf90a33e2011-07-19 11:55:52 +090041import android.app.KeyguardManager;
satok7cfc0ed2011-06-20 21:29:36 +090042import android.app.Notification;
43import android.app.NotificationManager;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070044import android.app.PendingIntent;
satok5b927c432012-05-01 20:09:34 +090045import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.ComponentName;
Yohei Yukawa3933a6e2016-11-10 00:47:48 -080047import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.ContentResolver;
49import android.content.Context;
50import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.DialogInterface.OnCancelListener;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +090052import android.content.DialogInterface.OnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.content.Intent;
satoke7c6998e2011-06-03 17:57:59 +090054import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.ServiceConnection;
Brandon Ballinger6da35a02009-10-21 00:38:13 -070056import android.content.pm.ApplicationInfo;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090057import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.content.pm.PackageManager;
59import android.content.pm.ResolveInfo;
60import android.content.pm.ServiceInfo;
Amith Yamasanie861ec12010-03-24 21:39:27 -070061import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.res.Resources;
63import android.content.res.TypedArray;
64import android.database.ContentObserver;
Yohei Yukawab4f328a2019-05-02 08:41:27 -070065import android.graphics.Matrix;
Alan Viverette505e3ab2014-11-24 15:22:11 -080066import android.graphics.drawable.Drawable;
Yohei Yukawab4f328a2019-05-02 08:41:27 -070067import android.hardware.display.DisplayManagerInternal;
lpeter133fce02020-03-05 20:32:16 +080068import android.hardware.input.InputManagerInternal;
Joe Onorato857fd9b2011-01-27 15:08:35 -080069import android.inputmethodservice.InputMethodService;
Michael Wright7b5a96b2014-08-09 19:28:42 -070070import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Binder;
Chris Wren1ce4b6d2015-06-11 10:19:43 -040072import android.os.Bundle;
Seigo Nonakae27dc2b2015-08-14 18:21:27 -070073import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Handler;
75import android.os.IBinder;
76import android.os.IInterface;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070077import android.os.LocaleList;
Yohei Yukawa0569a182018-08-28 16:09:28 -070078import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Parcel;
Yohei Yukawa6ab1a822015-08-25 14:01:28 -070080import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.RemoteException;
The Android Open Source Project4df24232009-03-05 14:34:35 -080082import android.os.ResultReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.ServiceManager;
Yohei Yukawa926488d2017-12-11 17:24:55 -080084import android.os.ShellCallback;
85import android.os.ShellCommand;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.SystemClock;
Tarandeep Singh75a92392018-01-12 14:58:59 -080087import android.os.SystemProperties;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +090088import android.os.UserHandle;
Amith Yamasani734983f2014-03-04 16:48:05 -080089import android.os.UserManager;
Yohei Yukawa42081402019-01-15 09:57:50 -080090import android.os.UserManagerInternal;
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;
Yohei Yukawaac9311e2018-11-20 19:25:23 -080094import android.util.ArrayMap;
Christopher Tate7b9a28c2015-03-18 13:06:16 -070095import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.util.EventLog;
satokf9f01002011-05-19 21:31:50 +090097import android.util.LruCache;
satokab751aa2010-09-14 19:17:36 +090098import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.PrintWriterPrinter;
100import android.util.Printer;
satoke7c6998e2011-06-03 17:57:59 +0900101import android.util.Slog;
Yohei Yukawab4f328a2019-05-02 08:41:27 -0700102import android.util.SparseArray;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +0900103import android.view.ContextThemeWrapper;
Yohei Yukawab4f328a2019-05-02 08:41:27 -0700104import android.view.DisplayInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.IWindowManager;
Jeff Brownc28867a2013-03-26 15:42:39 -0700106import android.view.InputChannel;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900107import android.view.LayoutInflater;
108import android.view.View;
109import android.view.ViewGroup;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700110import android.view.Window;
Yohei Yukawa6e875592019-01-28 00:49:30 -0800111import android.view.WindowManager.LayoutParams;
Yohei Yukawab0c26452018-10-21 10:42:52 -0700112import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
Feng Cao7c85eb72020-02-28 11:39:56 -0800113import android.view.autofill.AutofillId;
satokab751aa2010-09-14 19:17:36 +0900114import android.view.inputmethod.EditorInfo;
Feng Cao16b2de52020-01-09 17:27:27 -0800115import android.view.inputmethod.InlineSuggestionsRequest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.inputmethod.InputBinding;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800117import android.view.inputmethod.InputConnection;
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700118import android.view.inputmethod.InputConnectionInspector;
Yohei Yukawa432cf602018-10-21 10:41:37 -0700119import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import android.view.inputmethod.InputMethod;
121import android.view.inputmethod.InputMethodInfo;
122import android.view.inputmethod.InputMethodManager;
satokab751aa2010-09-14 19:17:36 +0900123import android.view.inputmethod.InputMethodSubtype;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900124import android.widget.ArrayAdapter;
satok01038492012-04-09 21:08:27 +0900125import android.widget.CompoundButton;
126import android.widget.CompoundButton.OnCheckedChangeListener;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900127import android.widget.RadioButton;
satok01038492012-04-09 21:08:27 +0900128import android.widget.Switch;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900129import android.widget.TextView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Yohei Yukawa0569a182018-08-28 16:09:28 -0700131import com.android.internal.annotations.GuardedBy;
132import com.android.internal.content.PackageMonitor;
133import com.android.internal.inputmethod.IInputContentUriToken;
Yohei Yukawac54c1172018-09-06 11:39:50 -0700134import com.android.internal.inputmethod.IInputMethodPrivilegedOperations;
Yohei Yukawaa468d702018-10-21 11:42:34 -0700135import com.android.internal.inputmethod.InputMethodDebug;
lumarkd85e1582019-12-29 20:20:41 +0800136import com.android.internal.inputmethod.SoftInputShowHideReason;
Yohei Yukawa35fa6d52018-10-31 11:33:32 -0700137import com.android.internal.inputmethod.StartInputFlags;
Yohei Yukawac6632df2018-10-21 11:47:16 -0700138import com.android.internal.inputmethod.StartInputReason;
Yohei Yukawa499e3f72018-10-21 20:15:11 -0700139import com.android.internal.inputmethod.UnbindReason;
Yohei Yukawa0569a182018-08-28 16:09:28 -0700140import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
141import com.android.internal.notification.SystemNotificationChannels;
142import com.android.internal.os.HandlerCaller;
143import com.android.internal.os.SomeArgs;
144import com.android.internal.os.TransferPipe;
145import com.android.internal.util.DumpUtils;
Yohei Yukawa0569a182018-08-28 16:09:28 -0700146import com.android.internal.util.IndentingPrintWriter;
Adam Hebc67f2e2019-11-13 14:34:56 -0800147import com.android.internal.view.IInlineSuggestionsRequestCallback;
Feng Cao16b2de52020-01-09 17:27:27 -0800148import com.android.internal.view.IInlineSuggestionsResponseCallback;
Yohei Yukawa0569a182018-08-28 16:09:28 -0700149import com.android.internal.view.IInputContext;
150import com.android.internal.view.IInputMethod;
151import com.android.internal.view.IInputMethodClient;
152import com.android.internal.view.IInputMethodManager;
153import com.android.internal.view.IInputMethodSession;
154import com.android.internal.view.IInputSessionCallback;
Feng Cao36960ee2020-02-18 18:23:30 -0800155import com.android.internal.view.InlineSuggestionsRequestInfo;
Yohei Yukawa0569a182018-08-28 16:09:28 -0700156import com.android.internal.view.InputBindResult;
Yohei Yukawa603f4d02018-09-11 15:04:58 -0700157import com.android.server.EventLogTags;
158import com.android.server.LocalServices;
159import com.android.server.SystemService;
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +0900160import com.android.server.inputmethod.InputMethodManagerInternal.InputMethodListListener;
Yohei Yukawae6b6e0e2018-09-12 16:42:48 -0700161import com.android.server.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
162import com.android.server.inputmethod.InputMethodUtils.InputMethodSettings;
Yohei Yukawa0569a182018-08-28 16:09:28 -0700163import com.android.server.statusbar.StatusBarManagerService;
Adrian Roose99bc052017-11-20 17:55:31 +0100164import com.android.server.wm.WindowManagerInternal;
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166import java.io.FileDescriptor;
167import java.io.IOException;
168import java.io.PrintWriter;
Yohei Yukawafa0e47e2016-04-05 09:55:56 -0700169import java.lang.annotation.Retention;
Yohei Yukawa25e08132016-06-22 16:31:41 -0700170import java.security.InvalidParameterException;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800171import java.text.SimpleDateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172import java.util.ArrayList;
Yohei Yukawab8d240f2018-12-26 10:03:11 -0800173import java.util.Arrays;
satok688bd472012-02-09 20:09:17 +0900174import java.util.Collections;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800175import java.util.Date;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176import java.util.List;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800177import java.util.Locale;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800178import java.util.WeakHashMap;
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +0900179import java.util.concurrent.CopyOnWriteArrayList;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800180import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182/**
183 * This class provides a system service that manages input methods.
184 */
185public class InputMethodManagerService extends IInputMethodManager.Stub
186 implements ServiceConnection, Handler.Callback {
187 static final boolean DEBUG = false;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700188 static final String TAG = "InputMethodManagerService";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Yohei Yukawa926488d2017-12-11 17:24:55 -0800190 @Retention(SOURCE)
191 @IntDef({ShellCommandResult.SUCCESS, ShellCommandResult.FAILURE})
192 private @interface ShellCommandResult {
193 int SUCCESS = 0;
194 int FAILURE = -1;
195 }
196
Seigo Nonakad4474cb2015-05-04 16:53:24 -0700197 static final int MSG_SHOW_IM_SUBTYPE_PICKER = 1;
198 static final int MSG_SHOW_IM_SUBTYPE_ENABLER = 2;
199 static final int MSG_SHOW_IM_CONFIG = 3;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 static final int MSG_UNBIND_INPUT = 1000;
202 static final int MSG_BIND_INPUT = 1010;
203 static final int MSG_SHOW_SOFT_INPUT = 1020;
204 static final int MSG_HIDE_SOFT_INPUT = 1030;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -0700205 static final int MSG_HIDE_CURRENT_INPUT_METHOD = 1035;
Yohei Yukawac54c1172018-09-06 11:39:50 -0700206 static final int MSG_INITIALIZE_IME = 1040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 static final int MSG_CREATE_SESSION = 1050;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 static final int MSG_START_INPUT = 2000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800210
Yohei Yukawa33e81792015-11-17 21:14:42 -0800211 static final int MSG_UNBIND_CLIENT = 3000;
212 static final int MSG_BIND_CLIENT = 3010;
Dianne Hackborna6e41342012-05-22 16:30:34 -0700213 static final int MSG_SET_ACTIVE = 3020;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700214 static final int MSG_SET_INTERACTIVE = 3030;
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800215 static final int MSG_REPORT_FULLSCREEN_MODE = 3045;
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -0800216 static final int MSG_REPORT_PRE_RENDERED = 3060;
217 static final int MSG_APPLY_IME_VISIBILITY = 3070;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800218
satok01038492012-04-09 21:08:27 +0900219 static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
220
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700221 static final int MSG_SYSTEM_UNLOCK_USER = 5000;
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +0900222 static final int MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED = 5010;
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -0700223
Adam Hebc67f2e2019-11-13 14:34:56 -0800224 static final int MSG_INLINE_SUGGESTIONS_REQUEST = 6000;
225
Satoshi Kataokabcacc322013-10-21 17:57:27 -0700226 static final long TIME_TO_RECONNECT = 3 * 1000;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
satokf9f01002011-05-19 21:31:50 +0900228 static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
229
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900230 private static final int NOT_A_SUBTYPE_ID = InputMethodUtils.NOT_A_SUBTYPE_ID;
satokb6359412011-06-28 17:47:41 +0900231 private static final String TAG_TRY_SUPPRESSING_IME_SWITCHER = "TrySuppressingImeSwitcher";
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +0900232
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700233 /**
234 * Binding flags for establishing connection to the {@link InputMethodService}.
235 */
236 private static final int IME_CONNECTION_BIND_FLAGS =
237 Context.BIND_AUTO_CREATE
238 | Context.BIND_NOT_VISIBLE
239 | Context.BIND_NOT_FOREGROUND
Yohei Yukawaad78a612017-08-04 01:57:27 -0700240 | Context.BIND_IMPORTANT_BACKGROUND;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700241
242 /**
243 * Binding flags used only while the {@link InputMethodService} is showing window.
244 */
245 private static final int IME_VISIBLE_BIND_FLAGS =
246 Context.BIND_AUTO_CREATE
247 | Context.BIND_TREAT_LIKE_ACTIVITY
Yohei Yukawaad78a612017-08-04 01:57:27 -0700248 | Context.BIND_FOREGROUND_SERVICE
Amith Yamasanic45a9902019-04-05 16:29:30 -0700249 | Context.BIND_INCLUDE_CAPABILITIES
Yohei Yukawa59730962019-03-18 10:47:22 -0700250 | Context.BIND_SHOWING_UI
251 | Context.BIND_SCHEDULE_LIKE_TOP_APP;
Yohei Yukawaa67a4592017-03-30 15:57:02 -0700252
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900253 /**
254 * A protected broadcast intent action for internal use for {@link PendingIntent} in
255 * the notification.
256 */
257 private static final String ACTION_SHOW_INPUT_METHOD_PICKER =
Yohei Yukawa603f4d02018-09-11 15:04:58 -0700258 "com.android.server.inputmethod.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER";
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +0900259
Tarandeep Singh75a92392018-01-12 14:58:59 -0800260 /**
261 * Debug flag for overriding runtime {@link SystemProperties}.
262 */
263 @AnyThread
264 private static final class DebugFlag {
265 private static final Object LOCK = new Object();
266 private final String mKey;
Tarandeep Singh9df4ad12018-04-03 13:35:50 -0700267 private final boolean mDefaultValue;
Tarandeep Singh75a92392018-01-12 14:58:59 -0800268 @GuardedBy("LOCK")
269 private boolean mValue;
270
Tarandeep Singh1c042c52018-03-29 17:04:44 -0700271 public DebugFlag(String key, boolean defaultValue) {
Tarandeep Singh75a92392018-01-12 14:58:59 -0800272 mKey = key;
Tarandeep Singh9df4ad12018-04-03 13:35:50 -0700273 mDefaultValue = defaultValue;
Tarandeep Singh1c042c52018-03-29 17:04:44 -0700274 mValue = SystemProperties.getBoolean(key, defaultValue);
Tarandeep Singh75a92392018-01-12 14:58:59 -0800275 }
276
277 void refresh() {
278 synchronized (LOCK) {
Tarandeep Singh9df4ad12018-04-03 13:35:50 -0700279 mValue = SystemProperties.getBoolean(mKey, mDefaultValue);
Tarandeep Singh75a92392018-01-12 14:58:59 -0800280 }
281 }
282
283 boolean value() {
284 synchronized (LOCK) {
285 return mValue;
286 }
287 }
288 }
289
290 /**
291 * Debug flags that can be overridden using "adb shell setprop <key>"
292 * Note: These flags are cached. To refresh, run "adb shell ime refresh_debug_properties".
293 */
294 private static final class DebugFlags {
295 static final DebugFlag FLAG_OPTIMIZE_START_INPUT =
Tarandeep Singh1c042c52018-03-29 17:04:44 -0700296 new DebugFlag("debug.optimize_startinput", false);
Tarandeep Singheadb1392018-11-09 18:15:57 +0100297 static final DebugFlag FLAG_PRE_RENDER_IME_VIEWS =
298 new DebugFlag("persist.pre_render_ime_views", false);
Tarandeep Singh75a92392018-01-12 14:58:59 -0800299 }
300
Yohei Yukawaa7babbb2019-01-08 16:45:34 -0800301 @UserIdInt
302 private int mLastSwitchUserId;
Tarandeep Singh75a92392018-01-12 14:58:59 -0800303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 final Context mContext;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800305 final Resources mRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 final Handler mHandler;
satokd87c2592010-09-29 11:52:06 +0900307 final InputMethodSettings mSettings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 final SettingsObserver mSettingsObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 final IWindowManager mIWindowManager;
Seigo Nonaka7309b122015-08-17 18:34:13 -0700310 final WindowManagerInternal mWindowManagerInternal;
lpeter133fce02020-03-05 20:32:16 +0800311 final InputManagerInternal mInputManagerInternal;
Yohei Yukawab4f328a2019-05-02 08:41:27 -0700312 private final DisplayManagerInternal mDisplayManagerInternal;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 final HandlerCaller mCaller;
Dianne Hackborn119bbc32013-03-22 17:27:25 -0700314 final boolean mHasFeature;
Yohei Yukawab557d572018-12-29 21:26:26 -0800315 private final ArrayMap<String, List<InputMethodSubtype>> mAdditionalSubtypeMap =
316 new ArrayMap<>();
Tarandeep Singheadb1392018-11-09 18:15:57 +0100317 private final boolean mIsLowRam;
satok01038492012-04-09 21:08:27 +0900318 private final HardKeyboardListener mHardKeyboardListener;
Yohei Yukawae63b5fa2014-09-19 13:14:55 +0900319 private final AppOpsManager mAppOpsManager;
Yohei Yukawaed4952a2016-02-17 07:57:25 -0800320 private final UserManager mUserManager;
Yohei Yukawa42081402019-01-15 09:57:50 -0800321 private final UserManagerInternal mUserManagerInternal;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 // All known input methods. mMethodMap also serves as the global
324 // lock for this class.
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700325 final ArrayList<InputMethodInfo> mMethodList = new ArrayList<>();
Yohei Yukawa1dd7de62018-11-20 19:25:29 -0800326 final ArrayMap<String, InputMethodInfo> mMethodMap = new ArrayMap<>();
satokf9f01002011-05-19 21:31:50 +0900327 private final LruCache<SuggestionSpan, InputMethodInfo> mSecureSuggestionSpans =
Yohei Yukawab0377bb2015-08-10 21:06:30 -0700328 new LruCache<>(SECURE_SUGGESTION_SPANS_MAX_SIZE);
Satoshi Kataokad787f692013-10-26 04:44:21 +0900329 private final InputMethodSubtypeSwitchingController mSwitchingController;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800330
Yohei Yukawae0733062017-02-09 22:49:35 -0800331 /**
332 * Tracks how many times {@link #mMethodMap} was updated.
333 */
334 @GuardedBy("mMethodMap")
335 private int mMethodMapUpdateCount = 0;
336
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700337 // Used to bring IME service up to visible adjustment while it is being shown.
338 final ServiceConnection mVisibleConnection = new ServiceConnection() {
Yohei Yukawad3ef8422018-06-07 16:28:07 -0700339 @Override public void onBindingDied(ComponentName name) {
340 synchronized (mMethodMap) {
341 if (mVisibleBound) {
342 mContext.unbindService(mVisibleConnection);
343 mVisibleBound = false;
344 }
345 }
346 }
347
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700348 @Override public void onServiceConnected(ComponentName name, IBinder service) {
349 }
350
351 @Override public void onServiceDisconnected(ComponentName name) {
352 }
353 };
354 boolean mVisibleBound = false;
355
satok7cfc0ed2011-06-20 21:29:36 +0900356 // Ongoing notification
Dianne Hackborn661cd522011-08-22 00:26:20 -0700357 private NotificationManager mNotificationManager;
358 private KeyguardManager mKeyguardManager;
Griff Hazen6090c262016-03-25 08:11:24 -0700359 private @Nullable StatusBarManagerService mStatusBar;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400360 private Notification.Builder mImeSwitcherNotification;
Dianne Hackborn661cd522011-08-22 00:26:20 -0700361 private PendingIntent mImeSwitchPendingIntent;
satokb858c732011-07-22 19:54:34 +0900362 private boolean mShowOngoingImeSwitcherForPhones;
satok7cfc0ed2011-06-20 21:29:36 +0900363 private boolean mNotificationShown;
364
Tadashi G. Takaoka8c6d4772014-08-05 15:29:17 +0900365 static class SessionState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 final ClientState client;
367 final IInputMethod method;
Jeff Brownc28867a2013-03-26 15:42:39 -0700368
369 IInputMethodSession session;
370 InputChannel channel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 @Override
373 public String toString() {
374 return "SessionState{uid " + client.uid + " pid " + client.pid
375 + " method " + Integer.toHexString(
376 System.identityHashCode(method))
377 + " session " + Integer.toHexString(
378 System.identityHashCode(session))
Jeff Brownc28867a2013-03-26 15:42:39 -0700379 + " channel " + channel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 + "}";
381 }
382
383 SessionState(ClientState _client, IInputMethod _method,
Jeff Brownc28867a2013-03-26 15:42:39 -0700384 IInputMethodSession _session, InputChannel _channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 client = _client;
386 method = _method;
387 session = _session;
Jeff Brownc28867a2013-03-26 15:42:39 -0700388 channel = _channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800391
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700392 private static final class ClientDeathRecipient implements IBinder.DeathRecipient {
393 private final InputMethodManagerService mImms;
394 private final IInputMethodClient mClient;
395
396 ClientDeathRecipient(InputMethodManagerService imms, IInputMethodClient client) {
397 mImms = imms;
398 mClient = client;
399 }
400
401 @Override
402 public void binderDied() {
403 mImms.removeClient(mClient);
404 }
405 }
406
Jeff Brownc28867a2013-03-26 15:42:39 -0700407 static final class ClientState {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 final IInputMethodClient client;
409 final IInputContext inputContext;
410 final int uid;
411 final int pid;
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800412 final int selfReportedDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 final InputBinding binding;
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700414 final ClientDeathRecipient clientDeathRecipient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 boolean sessionRequested;
Tarandeep Singheadb1392018-11-09 18:15:57 +0100417 // Determines if IMEs should be pre-rendered.
418 // DebugFlag can be flipped anytime. This flag is kept per-client to maintain behavior
419 // through the life of the current client.
420 boolean shouldPreRenderIme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 SessionState curSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 @Override
424 public String toString() {
425 return "ClientState{" + Integer.toHexString(
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800426 System.identityHashCode(this)) + " uid=" + uid
427 + " pid=" + pid + " displayId=" + selfReportedDisplayId + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 ClientState(IInputMethodClient _client, IInputContext _inputContext,
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800431 int _uid, int _pid, int _selfReportedDisplayId,
432 ClientDeathRecipient _clientDeathRecipient) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 client = _client;
434 inputContext = _inputContext;
435 uid = _uid;
436 pid = _pid;
Yohei Yukawa4052a10f2018-10-15 15:35:55 +0800437 selfReportedDisplayId = _selfReportedDisplayId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 binding = new InputBinding(null, inputContext.asBinder(), uid, pid);
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700439 clientDeathRecipient = _clientDeathRecipient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Yohei Yukawaac9311e2018-11-20 19:25:23 -0800443 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<>();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800444
Yohei Yukawab4f328a2019-05-02 08:41:27 -0700445 private static final class ActivityViewInfo {
446 /**
447 * {@link ClientState} where {@link android.app.ActivityView} is running.
448 */
449 private final ClientState mParentClient;
450 /**
451 * {@link Matrix} to convert screen coordinates in the embedded virtual display to
452 * screen coordinates where {@link #mParentClient} exists.
453 */
454 private final Matrix mMatrix;
455
456 ActivityViewInfo(ClientState parentClient, Matrix matrix) {
457 mParentClient = parentClient;
458 mMatrix = matrix;
459 }
460 }
461
462 /**
463 * A mapping table from virtual display IDs created for {@link android.app.ActivityView}
464 * to its parent IME client where {@link android.app.ActivityView} is running.
465 *
466 * <p>Note: this can be used only for virtual display IDs created by
467 * {@link android.app.ActivityView}.</p>
468 */
469 private SparseArray<ActivityViewInfo> mActivityViewDisplayIdToParentMap = new SparseArray<>();
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 /**
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700472 * Set once the system is ready to run third party code.
473 */
474 boolean mSystemReady;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800475
Dianne Hackborna34f1ad2009-09-02 13:26:28 -0700476 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700477 * Id obtained with {@link InputMethodInfo#getId()} for the currently selected input method.
478 * method. This is to be synchronized with the secure settings keyed with
479 * {@link Settings.Secure#DEFAULT_INPUT_METHOD}.
480 *
481 * <p>This can be transiently {@code null} when the system is re-initializing input method
482 * settings, e.g., the system locale is just changed.</p>
483 *
484 * <p>Note that {@link #mCurId} is used to track which IME is being connected to
485 * {@link InputMethodManagerService}.</p>
486 *
487 * @see #mCurId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700489 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 String mCurMethodId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 /**
493 * The current binding sequence number, incremented every time there is
494 * a new bind performed.
495 */
496 int mCurSeq;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
499 * The client that is currently bound to an input method.
500 */
501 ClientState mCurClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 /**
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800504 * The last window token that we confirmed to be focused. This is always updated upon reports
505 * from the input method client. If the window state is already changed before the report is
506 * handled, this field just keeps the last value.
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700507 */
508 IBinder mCurFocusedWindow;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800509
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700510 /**
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -0700511 * The last window token that we confirmed that IME started talking to. This is always updated
512 * upon reports from the input method. If the window state is already changed before the report
513 * is handled, this field just keeps the last value.
514 */
515 IBinder mLastImeTargetWindow;
516
517 /**
Yohei Yukawa6e875592019-01-28 00:49:30 -0800518 * {@link LayoutParams#softInputMode} of {@link #mCurFocusedWindow}.
Yohei Yukawa22a89232017-02-12 16:38:59 -0800519 *
520 * @see #mCurFocusedWindow
521 */
Yohei Yukawab0c26452018-10-21 10:42:52 -0700522 @SoftInputModeFlags
Yohei Yukawa22a89232017-02-12 16:38:59 -0800523 int mCurFocusedWindowSoftInputMode;
524
525 /**
Tarandeep Singheb570612018-01-29 16:20:32 -0800526 * The client by which {@link #mCurFocusedWindow} was reported.
Yohei Yukawae39d4ed2015-11-19 03:38:49 -0800527 */
528 ClientState mCurFocusedWindowClient;
529
530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * The input context last provided by the current client.
532 */
533 IInputContext mCurInputContext;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 /**
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700536 * The missing method flags for the input context last provided by the current client.
537 *
538 * @see android.view.inputmethod.InputConnectionInspector.MissingMethodFlags
539 */
Yohei Yukawa432cf602018-10-21 10:41:37 -0700540 @MissingMethodFlags
Yohei Yukawa19a80a12016-03-14 22:57:37 -0700541 int mCurInputContextMissingMethods;
542
543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 * The attributes last provided by the current client.
545 */
546 EditorInfo mCurAttribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 /**
Yohei Yukawab4f328a2019-05-02 08:41:27 -0700549 * A special {@link Matrix} to convert virtual screen coordinates to the IME target display
550 * coordinates.
551 *
552 * <p>Used only while the IME client is running in a virtual display inside
553 * {@link android.app.ActivityView}. {@code null} otherwise.</p>
554 */
555 @Nullable
556 private Matrix mCurActivityViewToScreenMatrix = null;
557
558 /**
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700559 * Id obtained with {@link InputMethodInfo#getId()} for the input method that we are currently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 * connected to or in the process of connecting to.
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700561 *
562 * <p>This can be {@code null} when no input method is connected.</p>
563 *
564 * @see #mCurMethodId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 */
Yohei Yukawae13a20fa2015-09-30 19:11:32 -0700566 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 String mCurId;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 /**
satokab751aa2010-09-14 19:17:36 +0900570 * The current subtype of the current input method.
571 */
572 private InputMethodSubtype mCurrentSubtype;
573
John Spurlocke0980502013-10-25 11:59:29 -0400574 // Was the keyguard locked when this client became current?
575 private boolean mCurClientInKeyguard;
576
satokab751aa2010-09-14 19:17:36 +0900577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 * Set to true if our ServiceConnection is currently actively bound to
579 * a service (whether or not we have gotten its IBinder back yet).
580 */
581 boolean mHaveConnection;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 /**
584 * Set if the client has asked for the input method to be shown.
585 */
586 boolean mShowRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 /**
589 * Set if we were explicitly told to show the input method.
590 */
591 boolean mShowExplicitlyRequested;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 /**
594 * Set if we were forced to be shown.
595 */
596 boolean mShowForced;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 /**
599 * Set if we last told the input method to show itself.
600 */
601 boolean mInputShown;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 /**
Yohei Yukawa2bc66172017-02-08 11:13:25 -0800604 * {@code true} if the current input method is in fullscreen mode.
605 */
606 boolean mInFullscreenMode;
607
608 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 * The Intent used to connect to the current input method.
610 */
611 Intent mCurIntent;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 /**
614 * The token we have made for the currently active input method, to
615 * identify it in the future.
616 */
617 IBinder mCurToken;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 /**
lumark90120a82018-08-15 00:33:03 +0800620 * The displayId of current active input method.
621 */
622 int mCurTokenDisplayId = INVALID_DISPLAY;
623
lumark7570cac2019-03-07 22:14:38 +0800624 /**
lpeter133fce02020-03-05 20:32:16 +0800625 * The host input token of the current active input method.
626 */
627 @GuardedBy("mMethodMap")
628 @Nullable
629 private IBinder mCurHostInputToken;
630
631 /**
lumark7570cac2019-03-07 22:14:38 +0800632 * The display ID of the input method indicates the fallback display which returned by
633 * {@link #computeImeDisplayIdForTarget}.
634 */
635 private static final int FALLBACK_DISPLAY_ID = DEFAULT_DISPLAY;
636
lumarkef1965b2018-09-12 17:42:53 +0800637 final ImeDisplayValidator mImeDisplayValidator;
638
lumark90120a82018-08-15 00:33:03 +0800639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * If non-null, this is the input method service we are currently connected
641 * to.
642 */
643 IInputMethod mCurMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 /**
646 * Time that we last initiated a bind to the input method, to determine
647 * if we should try to disconnect and reconnect to it.
648 */
649 long mLastBindTime;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 /**
652 * Have we called mCurMethod.bindInput()?
653 */
654 boolean mBoundToMethod;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 /**
657 * Currently enabled session. Only touched by service thread, not
658 * protected by a lock.
659 */
660 SessionState mEnabledSession;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 /**
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700663 * True if the device is currently interactive with user. The value is true initially.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 */
Yohei Yukawafa6e0a82015-07-23 15:08:59 -0700665 boolean mIsInteractive = true;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
Joe Onorato857fd9b2011-01-27 15:08:35 -0800667 int mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900668
669 /**
670 * A set of status bits regarding the active IME.
671 *
672 * <p>This value is a combination of following two bits:</p>
673 * <dl>
674 * <dt>{@link InputMethodService#IME_ACTIVE}</dt>
675 * <dd>
676 * If this bit is ON, connected IME is ready to accept touch/key events.
677 * </dd>
678 * <dt>{@link InputMethodService#IME_VISIBLE}</dt>
679 * <dd>
680 * If this bit is ON, some of IME view, e.g. software input, candidate view, is visible.
681 * </dd>
Tarandeep Singheadb1392018-11-09 18:15:57 +0100682 * dt>{@link InputMethodService#IME_INVISIBLE}</dt>
683 * <dd> If this bit is ON, IME is ready with views from last EditorInfo but is
684 * currently invisible.
685 * </dd>
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900686 * </dl>
lumark7570cac2019-03-07 22:14:38 +0800687 * <em>Do not update this value outside of {@link #setImeWindowStatus(IBinder, int, int)} and
688 * {@link #unbindCurrentMethodLocked()}.</em>
Seigo Nonakad9eb9112015-05-26 20:54:43 +0900689 */
Joe Onorato857fd9b2011-01-27 15:08:35 -0800690 int mImeWindowVis;
691
Ken Wakasa05dbb652011-08-22 15:22:43 +0900692 private AlertDialog.Builder mDialogBuilder;
693 private AlertDialog mSwitchingDialog;
Wale Ogunwale3a931692016-11-02 16:49:48 -0700694 private IBinder mSwitchingDialogToken = new Binder();
satok01038492012-04-09 21:08:27 +0900695 private View mSwitchingDialogTitleView;
Ken Wakasa05dbb652011-08-22 15:22:43 +0900696 private InputMethodInfo[] mIms;
697 private int[] mSubtypeIds;
Yohei Yukawae985c242016-02-24 18:27:04 -0800698 private LocaleList mLastSystemLocales;
Michael Wright7b5a96b2014-08-09 19:28:42 -0700699 private boolean mShowImeWithHardKeyboard;
Anna Galusza9b278112016-01-04 11:37:37 -0800700 private boolean mAccessibilityRequestingNoSoftKeyboard;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +0900701 private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor();
702 private final IPackageManager mIPackageManager;
Jason Monk3e189872016-01-12 09:10:34 -0500703 private final String mSlotIme;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800705 /**
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +0900706 * Registered {@link InputMethodListListeners}.
707 * This variable can be accessed from both of MainThread and BinderThread.
708 */
709 private final CopyOnWriteArrayList<InputMethodListListener> mInputMethodListListeners =
710 new CopyOnWriteArrayList<>();
711
712 /**
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800713 * Internal state snapshot when {@link #MSG_START_INPUT} message is about to be posted to the
714 * internal message queue. Any subsequent state change inside {@link InputMethodManagerService}
715 * will not affect those tasks that are already posted.
716 *
717 * <p>Posting {@link #MSG_START_INPUT} message basically means that
718 * {@link InputMethodService#doStartInput(InputConnection, EditorInfo, boolean)} will be called
719 * back in the current IME process shortly, which will also affect what the current IME starts
720 * receiving from {@link InputMethodService#getCurrentInputConnection()}. In other words, this
721 * snapshot will be taken every time when {@link InputMethodManagerService} is initiating a new
722 * logical input session between the client application and the current IME.</p>
723 *
724 * <p>Be careful to not keep strong references to this object forever, which can prevent
725 * {@link StartInputInfo#mImeToken} and {@link StartInputInfo#mTargetWindow} from being GC-ed.
726 * </p>
727 */
728 private static class StartInputInfo {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800729 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
730
731 final int mSequenceNumber;
732 final long mTimestamp;
733 final long mWallTime;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800734 @UserIdInt
735 final int mImeUserId;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800736 @NonNull
737 final IBinder mImeToken;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800738 final int mImeDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800739 @NonNull
740 final String mImeId;
Yohei Yukawadc66e522018-10-21 10:43:14 -0700741 @StartInputReason
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800742 final int mStartInputReason;
743 final boolean mRestarting;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800744 @UserIdInt
745 final int mTargetUserId;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800746 final int mTargetDisplayId;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800747 @Nullable
748 final IBinder mTargetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800749 @NonNull
750 final EditorInfo mEditorInfo;
Yohei Yukawab0c26452018-10-21 10:42:52 -0700751 @SoftInputModeFlags
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800752 final int mTargetWindowSoftInputMode;
753 final int mClientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800754
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800755 StartInputInfo(@UserIdInt int imeUserId, @NonNull IBinder imeToken, int imeDisplayId,
756 @NonNull String imeId, @StartInputReason int startInputReason, boolean restarting,
757 @UserIdInt int targetUserId, int targetDisplayId, @Nullable IBinder targetWindow,
758 @NonNull EditorInfo editorInfo, @SoftInputModeFlags int targetWindowSoftInputMode,
759 int clientBindSequenceNumber) {
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800760 mSequenceNumber = sSequenceNumber.getAndIncrement();
761 mTimestamp = SystemClock.uptimeMillis();
762 mWallTime = System.currentTimeMillis();
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800763 mImeUserId = imeUserId;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800764 mImeToken = imeToken;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800765 mImeDisplayId = imeDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800766 mImeId = imeId;
767 mStartInputReason = startInputReason;
768 mRestarting = restarting;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800769 mTargetUserId = targetUserId;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800770 mTargetDisplayId = targetDisplayId;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800771 mTargetWindow = targetWindow;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800772 mEditorInfo = editorInfo;
773 mTargetWindowSoftInputMode = targetWindowSoftInputMode;
774 mClientBindSequenceNumber = clientBindSequenceNumber;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800775 }
776 }
777
Yohei Yukawab37d8bd2017-02-13 18:29:05 -0800778 @GuardedBy("mMethodMap")
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -0700779 private final WeakHashMap<IBinder, IBinder> mImeTargetWindowMap = new WeakHashMap<>();
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -0800780
lumarkd85e1582019-12-29 20:20:41 +0800781 private static final class SoftInputShowHideHistory {
782 private Entry[] mEntries = new Entry[16];
783 private int mNextIndex = 0;
784 private static final AtomicInteger sSequenceNumber = new AtomicInteger(0);
785
lumarkd85e1582019-12-29 20:20:41 +0800786 private static final class Entry {
Ming-Shin Lua0605a62020-04-15 23:49:40 +0800787 final int mSequenceNumber = sSequenceNumber.getAndIncrement();
Anmol Gupta29f209d2020-03-11 11:34:46 -0700788 final ClientState mClientState;
lumarkd85e1582019-12-29 20:20:41 +0800789 @SoftInputModeFlags
Anmol Gupta29f209d2020-03-11 11:34:46 -0700790 final int mFocusedWindowSoftInputMode;
lumarkd85e1582019-12-29 20:20:41 +0800791 @SoftInputShowHideReason
Anmol Gupta29f209d2020-03-11 11:34:46 -0700792 final int mReason;
lumarkd85e1582019-12-29 20:20:41 +0800793 // The timing of handling MSG_SHOW_SOFT_INPUT or MSG_HIDE_SOFT_INPUT.
Anmol Gupta29f209d2020-03-11 11:34:46 -0700794 final long mTimestamp;
795 final long mWallTime;
796 final boolean mInFullscreenMode;
797 @NonNull
798 final String mFocusedWindowName;
799 @NonNull
800 final EditorInfo mEditorInfo;
801 @NonNull
802 final String mRequestWindowName;
lumarkd85e1582019-12-29 20:20:41 +0800803
Anmol Gupta29f209d2020-03-11 11:34:46 -0700804 Entry(ClientState client, EditorInfo editorInfo, String focusedWindowName,
805 @SoftInputModeFlags int softInputMode, @SoftInputShowHideReason int reason,
806 boolean inFullscreenMode, String requestWindowName) {
lumarkd85e1582019-12-29 20:20:41 +0800807 mClientState = client;
Anmol Gupta29f209d2020-03-11 11:34:46 -0700808 mEditorInfo = editorInfo;
809 mFocusedWindowName = focusedWindowName;
lumarkd85e1582019-12-29 20:20:41 +0800810 mFocusedWindowSoftInputMode = softInputMode;
811 mReason = reason;
lumarkd85e1582019-12-29 20:20:41 +0800812 mTimestamp = SystemClock.uptimeMillis();
813 mWallTime = System.currentTimeMillis();
Anmol Gupta29f209d2020-03-11 11:34:46 -0700814 mInFullscreenMode = inFullscreenMode;
815 mRequestWindowName = requestWindowName;
lumarkd85e1582019-12-29 20:20:41 +0800816 }
817 }
818
819 void addEntry(@NonNull Entry entry) {
820 final int index = mNextIndex;
821 mEntries[index] = entry;
822 mNextIndex = (mNextIndex + 1) % mEntries.length;
823 }
824
825 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
826 final SimpleDateFormat dataFormat =
827 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
828
829 for (int i = 0; i < mEntries.length; ++i) {
830 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
831 if (entry == null) {
832 continue;
833 }
834 pw.print(prefix);
Ming-Shin Lua0605a62020-04-15 23:49:40 +0800835 pw.println("SoftInputShowHideHistory #" + entry.mSequenceNumber + ":");
lumarkd85e1582019-12-29 20:20:41 +0800836
837 pw.print(prefix);
838 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
839 + " (timestamp=" + entry.mTimestamp + ")");
840
841 pw.print(prefix);
Anmol Gupta29f209d2020-03-11 11:34:46 -0700842 pw.print(" reason=" + InputMethodDebug.softInputDisplayReasonToString(
843 entry.mReason));
844 pw.println(" inFullscreenMode=" + entry.mInFullscreenMode);
lumarkd85e1582019-12-29 20:20:41 +0800845
846 pw.print(prefix);
Anmol Gupta29f209d2020-03-11 11:34:46 -0700847 pw.println(" requestClient=" + entry.mClientState);
848
849 pw.print(prefix);
850 pw.println(" focusedWindowName=" + entry.mFocusedWindowName);
851
852 pw.print(prefix);
853 pw.println(" requestWindowName=" + entry.mRequestWindowName);
854
855 pw.print(prefix);
856 pw.print(" editorInfo: ");
857 pw.print(" inputType=" + entry.mEditorInfo.inputType);
858 pw.print(" privateImeOptions=" + entry.mEditorInfo.privateImeOptions);
859 pw.println(" fieldId (viewId)=" + entry.mEditorInfo.fieldId);
lumarkd85e1582019-12-29 20:20:41 +0800860
861 pw.print(prefix);
862 pw.println(" focusedWindowSoftInputMode=" + InputMethodDebug.softInputModeToString(
863 entry.mFocusedWindowSoftInputMode));
864 }
865 }
866 }
867
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800868 /**
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -0800869 * Map of generated token to windowToken that is requesting
870 * {@link InputMethodManager#showSoftInput(View, int)}.
871 * This map tracks origin of showSoftInput requests.
872 */
873 @GuardedBy("mMethodMap")
874 private final WeakHashMap<IBinder, IBinder> mShowRequestWindowMap = new WeakHashMap<>();
875
876 /**
Tarandeep Singh4fe5b652020-02-20 17:20:19 -0800877 * Map of generated token to windowToken that is requesting
878 * {@link InputMethodManager#hideSoftInputFromWindow(IBinder, int)}.
879 * This map tracks origin of hideSoftInput requests.
880 */
881 @GuardedBy("mMethodMap")
882 private final WeakHashMap<IBinder, IBinder> mHideRequestWindowMap = new WeakHashMap<>();
883
884 /**
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800885 * A ring buffer to store the history of {@link StartInputInfo}.
886 */
887 private static final class StartInputHistory {
888 /**
889 * Entry size for non low-RAM devices.
890 *
891 * <p>TODO: Consider to follow what other system services have been doing to manage
892 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
893 */
894 private final static int ENTRY_SIZE_FOR_HIGH_RAM_DEVICE = 16;
895
896 /**
897 * Entry size for non low-RAM devices.
898 *
899 * <p>TODO: Consider to follow what other system services have been doing to manage
900 * constants (e.g. {@link android.provider.Settings.Global#ACTIVITY_MANAGER_CONSTANTS}).</p>
901 */
902 private final static int ENTRY_SIZE_FOR_LOW_RAM_DEVICE = 5;
903
904 private static int getEntrySize() {
905 if (ActivityManager.isLowRamDeviceStatic()) {
906 return ENTRY_SIZE_FOR_LOW_RAM_DEVICE;
907 } else {
908 return ENTRY_SIZE_FOR_HIGH_RAM_DEVICE;
909 }
910 }
911
912 /**
913 * Backing store for the ring bugger.
914 */
915 private final Entry[] mEntries = new Entry[getEntrySize()];
916
917 /**
918 * An index of {@link #mEntries}, to which next {@link #addEntry(StartInputInfo)} should
919 * write.
920 */
921 private int mNextIndex = 0;
922
923 /**
924 * Recyclable entry to store the information in {@link StartInputInfo}.
925 */
926 private static final class Entry {
927 int mSequenceNumber;
928 long mTimestamp;
929 long mWallTime;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800930 @UserIdInt
931 int mImeUserId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800932 @NonNull
933 String mImeTokenString;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800934 int mImeDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800935 @NonNull
936 String mImeId;
Yohei Yukawadc66e522018-10-21 10:43:14 -0700937 @StartInputReason
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800938 int mStartInputReason;
939 boolean mRestarting;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800940 @UserIdInt
941 int mTargetUserId;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800942 int mTargetDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800943 @NonNull
944 String mTargetWindowString;
945 @NonNull
946 EditorInfo mEditorInfo;
Yohei Yukawab0c26452018-10-21 10:42:52 -0700947 @SoftInputModeFlags
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800948 int mTargetWindowSoftInputMode;
949 int mClientBindSequenceNumber;
950
951 Entry(@NonNull StartInputInfo original) {
952 set(original);
953 }
954
955 void set(@NonNull StartInputInfo original) {
956 mSequenceNumber = original.mSequenceNumber;
957 mTimestamp = original.mTimestamp;
958 mWallTime = original.mWallTime;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800959 mImeUserId = original.mImeUserId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800960 // Intentionally convert to String so as not to keep a strong reference to a Binder
961 // object.
962 mImeTokenString = String.valueOf(original.mImeToken);
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800963 mImeDisplayId = original.mImeDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800964 mImeId = original.mImeId;
965 mStartInputReason = original.mStartInputReason;
966 mRestarting = original.mRestarting;
Yohei Yukawa7979e1a2019-02-12 02:01:10 -0800967 mTargetUserId = original.mTargetUserId;
Yohei Yukawa557afbb2019-02-12 02:01:00 -0800968 mTargetDisplayId = original.mTargetDisplayId;
Yohei Yukawa357b2f62017-02-14 09:40:03 -0800969 // Intentionally convert to String so as not to keep a strong reference to a Binder
970 // object.
971 mTargetWindowString = String.valueOf(original.mTargetWindow);
972 mEditorInfo = original.mEditorInfo;
973 mTargetWindowSoftInputMode = original.mTargetWindowSoftInputMode;
974 mClientBindSequenceNumber = original.mClientBindSequenceNumber;
975 }
976 }
977
978 /**
979 * Add a new entry and discard the oldest entry as needed.
980 * @param info {@lin StartInputInfo} to be added.
981 */
982 void addEntry(@NonNull StartInputInfo info) {
983 final int index = mNextIndex;
984 if (mEntries[index] == null) {
985 mEntries[index] = new Entry(info);
986 } else {
987 mEntries[index].set(info);
988 }
989 mNextIndex = (mNextIndex + 1) % mEntries.length;
990 }
991
992 void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
993 final SimpleDateFormat dataFormat =
994 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
995
996 for (int i = 0; i < mEntries.length; ++i) {
997 final Entry entry = mEntries[(i + mNextIndex) % mEntries.length];
998 if (entry == null) {
999 continue;
1000 }
1001 pw.print(prefix);
1002 pw.println("StartInput #" + entry.mSequenceNumber + ":");
1003
1004 pw.print(prefix);
1005 pw.println(" time=" + dataFormat.format(new Date(entry.mWallTime))
1006 + " (timestamp=" + entry.mTimestamp + ")"
1007 + " reason="
Yohei Yukawaa468d702018-10-21 11:42:34 -07001008 + InputMethodDebug.startInputReasonToString(entry.mStartInputReason)
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001009 + " restarting=" + entry.mRestarting);
1010
1011 pw.print(prefix);
Yohei Yukawa557afbb2019-02-12 02:01:00 -08001012 pw.print(" imeToken=" + entry.mImeTokenString + " [" + entry.mImeId + "]");
Yohei Yukawa7979e1a2019-02-12 02:01:10 -08001013 pw.print(" imeUserId=" + entry.mImeUserId);
Yohei Yukawa557afbb2019-02-12 02:01:00 -08001014 pw.println(" imeDisplayId=" + entry.mImeDisplayId);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001015
1016 pw.print(prefix);
1017 pw.println(" targetWin=" + entry.mTargetWindowString
1018 + " [" + entry.mEditorInfo.packageName + "]"
Yohei Yukawa7979e1a2019-02-12 02:01:10 -08001019 + " targetUserId=" + entry.mTargetUserId
Yohei Yukawa557afbb2019-02-12 02:01:00 -08001020 + " targetDisplayId=" + entry.mTargetDisplayId
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001021 + " clientBindSeq=" + entry.mClientBindSequenceNumber);
1022
1023 pw.print(prefix);
Yohei Yukawaa468d702018-10-21 11:42:34 -07001024 pw.println(" softInputMode=" + InputMethodDebug.softInputModeToString(
Yohei Yukawa357b2f62017-02-14 09:40:03 -08001025 entry.mTargetWindowSoftInputMode));
1026
1027 pw.print(prefix);
1028 pw.println(" inputType=0x" + Integer.toHexString(entry.mEditorInfo.inputType)
1029 + " imeOptions=0x" + Integer.toHexString(entry.mEditorInfo.imeOptions)
1030 + " fieldId=0x" + Integer.toHexString(entry.mEditorInfo.fieldId)
1031 + " fieldName=" + entry.mEditorInfo.fieldName
1032 + " actionId=" + entry.mEditorInfo.actionId
1033 + " actionLabel=" + entry.mEditorInfo.actionLabel);
1034 }
1035 }
1036 }
1037
1038 @GuardedBy("mMethodMap")
1039 @NonNull
1040 private final StartInputHistory mStartInputHistory = new StartInputHistory();
1041
lumarkd85e1582019-12-29 20:20:41 +08001042 @GuardedBy("mMethodMap")
1043 @NonNull
1044 private final SoftInputShowHideHistory mSoftInputShowHideHistory =
1045 new SoftInputShowHideHistory();
1046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 class SettingsObserver extends ContentObserver {
Yohei Yukawa81482972015-06-04 00:58:59 -07001048 int mUserId;
1049 boolean mRegistered = false;
Yohei Yukawa7b574cb2016-03-16 17:22:22 -07001050 @NonNull
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001051 String mLastEnabled = "";
1052
Yohei Yukawa81482972015-06-04 00:58:59 -07001053 /**
1054 * <em>This constructor must be called within the lock.</em>
1055 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 SettingsObserver(Handler handler) {
1057 super(handler);
Yohei Yukawa81482972015-06-04 00:58:59 -07001058 }
1059
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001060 public void registerContentObserverLocked(@UserIdInt int userId) {
Yohei Yukawa81482972015-06-04 00:58:59 -07001061 if (mRegistered && mUserId == userId) {
1062 return;
1063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 ContentResolver resolver = mContext.getContentResolver();
Yohei Yukawa81482972015-06-04 00:58:59 -07001065 if (mRegistered) {
1066 mContext.getContentResolver().unregisterContentObserver(this);
1067 mRegistered = false;
1068 }
1069 if (mUserId != userId) {
1070 mLastEnabled = "";
1071 mUserId = userId;
1072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -07001074 Settings.Secure.DEFAULT_INPUT_METHOD), false, this, userId);
satokab751aa2010-09-14 19:17:36 +09001075 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -07001076 Settings.Secure.ENABLED_INPUT_METHODS), false, this, userId);
satokb6109bb2011-02-03 22:24:54 +09001077 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -07001078 Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE), false, this, userId);
Michael Wright7b5a96b2014-08-09 19:28:42 -07001079 resolver.registerContentObserver(Settings.Secure.getUriFor(
Yohei Yukawa81482972015-06-04 00:58:59 -07001080 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD), false, this, userId);
Anna Galusza9b278112016-01-04 11:37:37 -08001081 resolver.registerContentObserver(Settings.Secure.getUriFor(
1082 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE), false, this, userId);
Yohei Yukawa81482972015-06-04 00:58:59 -07001083 mRegistered = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001085
Michael Wright7b5a96b2014-08-09 19:28:42 -07001086 @Override public void onChange(boolean selfChange, Uri uri) {
Anna Galusza9b278112016-01-04 11:37:37 -08001087 final Uri showImeUri = Settings.Secure.getUriFor(
1088 Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD);
1089 final Uri accessibilityRequestingNoImeUri = Settings.Secure.getUriFor(
1090 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 synchronized (mMethodMap) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001092 if (showImeUri.equals(uri)) {
1093 updateKeyboardFromSettingsLocked();
Anna Galusza9b278112016-01-04 11:37:37 -08001094 } else if (accessibilityRequestingNoImeUri.equals(uri)) {
Phil Weaver03a65b02018-07-19 16:07:57 -07001095 final int accessibilitySoftKeyboardSetting = Settings.Secure.getIntForUser(
Anna Galusza9b278112016-01-04 11:37:37 -08001096 mContext.getContentResolver(),
Phil Weaver03a65b02018-07-19 16:07:57 -07001097 Settings.Secure.ACCESSIBILITY_SOFT_KEYBOARD_MODE, 0, mUserId);
1098 mAccessibilityRequestingNoSoftKeyboard =
1099 (accessibilitySoftKeyboardSetting & AccessibilityService.SHOW_MODE_MASK)
1100 == AccessibilityService.SHOW_MODE_HIDDEN;
Anna Galusza9b278112016-01-04 11:37:37 -08001101 if (mAccessibilityRequestingNoSoftKeyboard) {
1102 final boolean showRequested = mShowRequested;
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08001103 hideCurrentInputLocked(mCurFocusedWindow, 0, null,
lumarkd85e1582019-12-29 20:20:41 +08001104 SoftInputShowHideReason.HIDE_SETTINGS_ON_CHANGE);
Anna Galusza9b278112016-01-04 11:37:37 -08001105 mShowRequested = showRequested;
1106 } else if (mShowRequested) {
lumarkd85e1582019-12-29 20:20:41 +08001107 showCurrentInputLocked(mCurFocusedWindow,
1108 InputMethodManager.SHOW_IMPLICIT, null,
1109 SoftInputShowHideReason.SHOW_SETTINGS_ON_CHANGE);
Anna Galusza9b278112016-01-04 11:37:37 -08001110 }
Michael Wright7b5a96b2014-08-09 19:28:42 -07001111 } else {
1112 boolean enabledChanged = false;
1113 String newEnabled = mSettings.getEnabledInputMethodsStr();
1114 if (!mLastEnabled.equals(newEnabled)) {
1115 mLastEnabled = newEnabled;
1116 enabledChanged = true;
1117 }
1118 updateInputMethodsFromSettingsLocked(enabledChanged);
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 }
1121 }
Yohei Yukawa81482972015-06-04 00:58:59 -07001122
1123 @Override
1124 public String toString() {
1125 return "SettingsObserver{mUserId=" + mUserId + " mRegistered=" + mRegistered
1126 + " mLastEnabled=" + mLastEnabled + "}";
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001129
Yohei Yukawa0ec08002019-03-08 10:48:26 -08001130 /**
1131 * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to the system user
1132 * only.
1133 */
1134 private final class ImmsBroadcastReceiverForSystemUser extends BroadcastReceiver {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001135 @Override
1136 public void onReceive(Context context, Intent intent) {
1137 final String action = intent.getAction();
Yohei Yukawa4225c7d2019-03-08 00:06:11 -08001138 if (Intent.ACTION_USER_ADDED.equals(action)
Amith Yamasani734983f2014-03-04 16:48:05 -08001139 || Intent.ACTION_USER_REMOVED.equals(action)) {
Kenny Guy2a764942014-04-02 13:29:20 +01001140 updateCurrentProfileIds();
Amith Yamasani734983f2014-03-04 16:48:05 -08001141 return;
Yohei Yukawa79247822017-01-23 15:26:15 -08001142 } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001143 onActionLocaleChanged();
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001144 } else if (ACTION_SHOW_INPUT_METHOD_PICKER.equals(action)) {
1145 // ACTION_SHOW_INPUT_METHOD_PICKER action is a protected-broadcast and it is
1146 // guaranteed to be send only from the system, so that there is no need for extra
1147 // security check such as
1148 // {@link #canShowInputMethodPickerLocked(IInputMethodClient)}.
1149 mHandler.obtainMessage(
1150 MSG_SHOW_IM_SUBTYPE_PICKER,
lumark0b05f9e2018-11-26 15:09:06 +08001151 // TODO(b/120076400): Design and implement IME switcher for heterogeneous
1152 // navbar configuration.
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001153 InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES,
lumark0b05f9e2018-11-26 15:09:06 +08001154 DEFAULT_DISPLAY).sendToTarget();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001155 } else {
1156 Slog.w(TAG, "Unexpected intent " + intent);
1157 }
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001161 /**
Yohei Yukawa0ec08002019-03-08 10:48:26 -08001162 * {@link BroadcastReceiver} that is intended to listen to broadcasts sent to all the users.
1163 */
1164 private final class ImmsBroadcastReceiverForAllUsers extends BroadcastReceiver {
1165 @Override
1166 public void onReceive(Context context, Intent intent) {
1167 final String action = intent.getAction();
1168 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
1169 final PendingResult pendingResult = getPendingResult();
1170 if (pendingResult == null) {
1171 return;
1172 }
1173 // sender userId can be a real user ID or USER_ALL.
1174 final int senderUserId = pendingResult.getSendingUserId();
1175 if (senderUserId != UserHandle.USER_ALL) {
Tarandeep Singhcf5ff822019-07-08 14:06:27 -07001176 if (senderUserId != mSettings.getCurrentUserId()) {
Yohei Yukawa0ec08002019-03-08 10:48:26 -08001177 // A background user is trying to hide the dialog. Ignore.
1178 return;
1179 }
1180 }
1181 hideInputMethodMenu();
1182 } else {
1183 Slog.w(TAG, "Unexpected intent " + intent);
1184 }
1185 }
1186 }
1187
1188 /**
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001189 * Handles {@link Intent#ACTION_LOCALE_CHANGED}.
1190 *
1191 * <p>Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all
1192 * the users. We should ignore this event if this is about any background user's locale.</p>
1193 *
1194 * <p>Caution: This method must not be called when system is not ready.</p>
1195 */
1196 void onActionLocaleChanged() {
1197 synchronized (mMethodMap) {
1198 final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales();
1199 if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) {
1200 return;
1201 }
1202 buildInputMethodListLocked(true);
1203 // If the locale is changed, needs to reset the default ime
1204 resetDefaultImeLocked(mContext);
1205 updateFromSettingsLocked(true);
1206 mLastSystemLocales = possibleNewLocale;
1207 }
1208 }
1209
Yohei Yukawac4e44912017-02-09 19:30:22 -08001210 final class MyPackageMonitor extends PackageMonitor {
1211 /**
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001212 * Package names that are known to contain {@link InputMethodService}.
Yohei Yukawac4e44912017-02-09 19:30:22 -08001213 *
1214 * <p>No need to include packages because of direct-boot unaware IMEs since we always rescan
1215 * all the packages when the user is unlocked, and direct-boot awareness will not be changed
1216 * dynamically unless the entire package is updated, which also always triggers package
1217 * rescanning.</p>
1218 */
1219 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001220 final private ArraySet<String> mKnownImePackageNames = new ArraySet<>();
1221
1222 /**
1223 * Packages that are appeared, disappeared, or modified for whatever reason.
1224 *
1225 * <p>Note: For now we intentionally use {@link ArrayList} instead of {@link ArraySet}
1226 * because 1) the number of elements is almost always 1 or so, and 2) we do not care
1227 * duplicate elements for our use case.</p>
1228 *
1229 * <p>This object must be accessed only from callback methods in {@link PackageMonitor},
1230 * which should be bound to {@link #getRegisteredHandler()}.</p>
1231 */
1232 private final ArrayList<String> mChangedPackages = new ArrayList<>();
1233
1234 /**
1235 * {@code true} if one or more packages that contain {@link InputMethodService} appeared.
1236 *
1237 * <p>This field must be accessed only from callback methods in {@link PackageMonitor},
1238 * which should be bound to {@link #getRegisteredHandler()}.</p>
1239 */
1240 private boolean mImePackageAppeared = false;
Yohei Yukawac4e44912017-02-09 19:30:22 -08001241
1242 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001243 void clearKnownImePackageNamesLocked() {
1244 mKnownImePackageNames.clear();
Yohei Yukawac4e44912017-02-09 19:30:22 -08001245 }
1246
1247 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001248 final void addKnownImePackageNameLocked(@NonNull String packageName) {
1249 mKnownImePackageNames.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001250 }
1251
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001252 @GuardedBy("mMethodMap")
1253 private boolean isChangingPackagesOfCurrentUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001254 final int userId = getChangingUserId();
1255 final boolean retval = userId == mSettings.getCurrentUserId();
1256 if (DEBUG) {
satok81f8b7c2012-12-04 20:42:56 +09001257 if (!retval) {
1258 Slog.d(TAG, "--- ignore this call back from a background user: " + userId);
1259 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001260 }
1261 return retval;
1262 }
1263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001265 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001267 if (!isChangingPackagesOfCurrentUserLocked()) {
1268 return false;
1269 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001270 String curInputMethodId = mSettings.getSelectedInputMethod();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 final int N = mMethodList.size();
1272 if (curInputMethodId != null) {
1273 for (int i=0; i<N; i++) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001274 InputMethodInfo imi = mMethodList.get(i);
1275 if (imi.getId().equals(curInputMethodId)) {
1276 for (String pkg : packages) {
1277 if (imi.getPackageName().equals(pkg)) {
1278 if (!doit) {
1279 return true;
1280 }
satok723a27e2010-11-11 14:58:11 +09001281 resetSelectedInputMethodAndSubtypeLocked("");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001282 chooseNewDefaultIMELocked();
1283 return true;
1284 }
1285 }
1286 }
1287 }
1288 }
1289 }
1290 return false;
1291 }
1292
1293 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001294 public void onBeginPackageChanges() {
1295 clearPackageChangeState();
1296 }
1297
1298 @Override
1299 public void onPackageAppeared(String packageName, int reason) {
1300 if (!mImePackageAppeared) {
1301 final PackageManager pm = mContext.getPackageManager();
1302 final List<ResolveInfo> services = pm.queryIntentServicesAsUser(
1303 new Intent(InputMethod.SERVICE_INTERFACE).setPackage(packageName),
Yohei Yukawa5ec1fce2018-12-17 07:56:14 -08001304 PackageManager.MATCH_DISABLED_COMPONENTS, getChangingUserId());
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001305 // No need to lock this because we access it only on getRegisteredHandler().
1306 if (!services.isEmpty()) {
1307 mImePackageAppeared = true;
1308 }
1309 }
1310 // No need to lock this because we access it only on getRegisteredHandler().
1311 mChangedPackages.add(packageName);
1312 }
1313
1314 @Override
1315 public void onPackageDisappeared(String packageName, int reason) {
1316 // No need to lock this because we access it only on getRegisteredHandler().
1317 mChangedPackages.add(packageName);
1318 }
1319
1320 @Override
1321 public void onPackageModified(String packageName) {
1322 // No need to lock this because we access it only on getRegisteredHandler().
1323 mChangedPackages.add(packageName);
1324 }
1325
1326 @Override
1327 public void onPackagesSuspended(String[] packages) {
1328 // No need to lock this because we access it only on getRegisteredHandler().
1329 for (String packageName : packages) {
1330 mChangedPackages.add(packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08001331 }
1332 }
1333
1334 @Override
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001335 public void onPackagesUnsuspended(String[] packages) {
1336 // No need to lock this because we access it only on getRegisteredHandler().
1337 for (String packageName : packages) {
1338 mChangedPackages.add(packageName);
1339 }
1340 }
1341
1342 @Override
1343 public void onFinishPackageChanges() {
1344 onFinishPackageChangesInternal();
1345 clearPackageChangeState();
1346 }
1347
1348 private void clearPackageChangeState() {
1349 // No need to lock them because we access these fields only on getRegisteredHandler().
1350 mChangedPackages.clear();
1351 mImePackageAppeared = false;
1352 }
1353
Andreas Gampea36dc622018-02-05 17:19:22 -08001354 @GuardedBy("mMethodMap")
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001355 private boolean shouldRebuildInputMethodListLocked() {
1356 // This method is guaranteed to be called only by getRegisteredHandler().
1357
1358 // If there is any new package that contains at least one IME, then rebuilt the list
1359 // of IMEs.
1360 if (mImePackageAppeared) {
1361 return true;
1362 }
1363
1364 // Otherwise, check if mKnownImePackageNames and mChangedPackages have any intersection.
1365 // TODO: Consider to create a utility method to do the following test. List.retainAll()
1366 // is an option, but it may still do some extra operations that we do not need here.
1367 final int N = mChangedPackages.size();
1368 for (int i = 0; i < N; ++i) {
1369 final String packageName = mChangedPackages.get(i);
1370 if (mKnownImePackageNames.contains(packageName)) {
1371 return true;
1372 }
1373 }
1374 return false;
1375 }
1376
1377 private void onFinishPackageChangesInternal() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001378 synchronized (mMethodMap) {
Yohei Yukawa278f2ab2017-02-14 19:51:33 -08001379 if (!isChangingPackagesOfCurrentUserLocked()) {
1380 return;
1381 }
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08001382 if (!shouldRebuildInputMethodListLocked()) {
1383 return;
1384 }
1385
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001386 InputMethodInfo curIm = null;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001387 String curInputMethodId = mSettings.getSelectedInputMethod();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001388 final int N = mMethodList.size();
1389 if (curInputMethodId != null) {
1390 for (int i=0; i<N; i++) {
1391 InputMethodInfo imi = mMethodList.get(i);
satoke7c6998e2011-06-03 17:57:59 +09001392 final String imiId = imi.getId();
1393 if (imiId.equals(curInputMethodId)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001394 curIm = imi;
1395 }
satoke7c6998e2011-06-03 17:57:59 +09001396
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001397 int change = isPackageDisappearing(imi.getPackageName());
satoke7c6998e2011-06-03 17:57:59 +09001398 if (isPackageModified(imi.getPackageName())) {
Yohei Yukawab557d572018-12-29 21:26:26 -08001399 mAdditionalSubtypeMap.remove(imi.getId());
1400 AdditionalSubtypeUtils.save(mAdditionalSubtypeMap, mMethodMap,
1401 mSettings.getCurrentUserId());
satoke7c6998e2011-06-03 17:57:59 +09001402 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001403 if (change == PACKAGE_TEMPORARY_CHANGE
1404 || change == PACKAGE_PERMANENT_CHANGE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 Slog.i(TAG, "Input method uninstalled, disabling: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001406 + imi.getComponent());
1407 setInputMethodEnabledLocked(imi.getId(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409 }
1410 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001411
Yohei Yukawa94e33302016-02-12 19:37:03 -08001412 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 boolean changed = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001416 if (curIm != null) {
Anna Galusza9b278112016-01-04 11:37:37 -08001417 int change = isPackageDisappearing(curIm.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001418 if (change == PACKAGE_TEMPORARY_CHANGE
1419 || change == PACKAGE_PERMANENT_CHANGE) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001420 ServiceInfo si = null;
1421 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001422 si = mIPackageManager.getServiceInfo(
1423 curIm.getComponent(), 0, mSettings.getCurrentUserId());
1424 } catch (RemoteException ex) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001425 }
1426 if (si == null) {
1427 // Uh oh, current input method is no longer around!
1428 // Pick another one...
Joe Onorato8a9b2202010-02-26 18:56:32 -08001429 Slog.i(TAG, "Current input method removed: " + curInputMethodId);
Yohei Yukawa849443c2019-01-21 09:02:25 -08001430 updateSystemUiLocked(0 /* vis */, mBackDisposition);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001431 if (!chooseNewDefaultIMELocked()) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001432 changed = true;
1433 curIm = null;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001434 Slog.i(TAG, "Unsetting current input method");
satok723a27e2010-11-11 14:58:11 +09001435 resetSelectedInputMethodAndSubtypeLocked("");
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001436 }
1437 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001438 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001439 }
satokab751aa2010-09-14 19:17:36 +09001440
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001441 if (curIm == null) {
1442 // We currently don't have a default input method... is
1443 // one now available?
1444 changed = chooseNewDefaultIMELocked();
Yohei Yukawa54d512c2015-05-19 22:15:02 -07001445 } else if (!changed && isPackageModified(curIm.getPackageName())) {
1446 // Even if the current input method is still available, mCurrentSubtype could
1447 // be obsolete when the package is modified in practice.
1448 changed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001451 if (changed) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08001452 updateFromSettingsLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454 }
1455 }
1456 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001457
Jeff Brownc28867a2013-03-26 15:42:39 -07001458 private static final class MethodCallback extends IInputSessionCallback.Stub {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001459 private final InputMethodManagerService mParentIMMS;
Jeff Brownc28867a2013-03-26 15:42:39 -07001460 private final IInputMethod mMethod;
1461 private final InputChannel mChannel;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001462
Jeff Brownc28867a2013-03-26 15:42:39 -07001463 MethodCallback(InputMethodManagerService imms, IInputMethod method,
1464 InputChannel channel) {
Jean Chalarde0d32a62011-10-20 20:36:07 +09001465 mParentIMMS = imms;
Jeff Brownc28867a2013-03-26 15:42:39 -07001466 mMethod = method;
1467 mChannel = channel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001469
satoke7c6998e2011-06-03 17:57:59 +09001470 @Override
Jeff Brownc28867a2013-03-26 15:42:39 -07001471 public void sessionCreated(IInputMethodSession session) {
Dianne Hackborn6b6b3fd2014-03-24 11:27:18 -07001472 long ident = Binder.clearCallingIdentity();
1473 try {
1474 mParentIMMS.onSessionCreated(mMethod, session, mChannel);
1475 } finally {
1476 Binder.restoreCallingIdentity(ident);
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
1479 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001480
satok01038492012-04-09 21:08:27 +09001481 private class HardKeyboardListener
Seigo Nonaka7309b122015-08-17 18:34:13 -07001482 implements WindowManagerInternal.OnHardKeyboardStatusChangeListener {
satok01038492012-04-09 21:08:27 +09001483 @Override
Michael Wright7b5a96b2014-08-09 19:28:42 -07001484 public void onHardKeyboardStatusChange(boolean available) {
1485 mHandler.sendMessage(mHandler.obtainMessage(MSG_HARD_KEYBOARD_SWITCH_CHANGED,
1486 available ? 1 : 0));
satok01038492012-04-09 21:08:27 +09001487 }
1488
Michael Wright7b5a96b2014-08-09 19:28:42 -07001489 public void handleHardKeyboardStatusChange(boolean available) {
satok01038492012-04-09 21:08:27 +09001490 if (DEBUG) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07001491 Slog.w(TAG, "HardKeyboardStatusChanged: available=" + available);
satok01038492012-04-09 21:08:27 +09001492 }
1493 synchronized(mMethodMap) {
1494 if (mSwitchingDialog != null && mSwitchingDialogTitleView != null
1495 && mSwitchingDialog.isShowing()) {
1496 mSwitchingDialogTitleView.findViewById(
1497 com.android.internal.R.id.hard_keyboard_section).setVisibility(
1498 available ? View.VISIBLE : View.GONE);
1499 }
1500 }
1501 }
1502 }
1503
Yohei Yukawad277d692020-02-19 17:12:17 -08001504 private static final class UserSwitchHandlerTask implements Runnable {
1505 final InputMethodManagerService mService;
1506
1507 @UserIdInt
1508 final int mToUserId;
1509
1510 @Nullable
1511 IInputMethodClient mClientToBeReset;
1512
1513 UserSwitchHandlerTask(InputMethodManagerService service, @UserIdInt int toUserId,
1514 @Nullable IInputMethodClient clientToBeReset) {
1515 mService = service;
1516 mToUserId = toUserId;
1517 mClientToBeReset = clientToBeReset;
1518 }
1519
1520 @Override
1521 public void run() {
1522 synchronized (mService.mMethodMap) {
1523 if (mService.mUserSwitchHandlerTask != this) {
1524 // This task was already canceled before it is handled here. So do nothing.
1525 return;
1526 }
1527 mService.switchUserOnHandlerLocked(mService.mUserSwitchHandlerTask.mToUserId,
1528 mClientToBeReset);
1529 mService.mUserSwitchHandlerTask = null;
1530 }
1531 }
1532 }
1533
1534 /**
1535 * When non-{@code null}, this represents pending user-switch task, which is to be executed as
1536 * a handler callback. This needs to be set and unset only within the lock.
1537 */
1538 @Nullable
1539 @GuardedBy("mMethodMap")
1540 private UserSwitchHandlerTask mUserSwitchHandlerTask;
1541
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001542 public static final class Lifecycle extends SystemService {
1543 private InputMethodManagerService mService;
1544
1545 public Lifecycle(Context context) {
1546 super(context);
1547 mService = new InputMethodManagerService(context);
1548 }
1549
1550 @Override
1551 public void onStart() {
1552 LocalServices.addService(InputMethodManagerInternal.class,
Yohei Yukawafffc0e52018-09-04 13:24:00 -07001553 new LocalServiceImpl(mService));
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001554 publishBinderService(Context.INPUT_METHOD_SERVICE, mService);
1555 }
1556
1557 @Override
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001558 public void onSwitchUser(@UserIdInt int userHandle) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001559 // Called on ActivityManager thread.
Yohei Yukawad277d692020-02-19 17:12:17 -08001560 synchronized (mService.mMethodMap) {
1561 mService.scheduleSwitchUserTaskLocked(userHandle, null /* clientToBeReset */);
1562 }
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001563 }
1564
1565 @Override
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001566 public void onBootPhase(int phase) {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001567 // Called on ActivityManager thread.
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001568 // TODO: Dispatch this to a worker thread as needed.
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001569 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1570 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
1571 .getService(Context.STATUS_BAR_SERVICE);
1572 mService.systemRunning(statusBarService);
1573 }
1574 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001575
1576 @Override
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07001577 public void onUnlockUser(final @UserIdInt int userHandle) {
1578 // Called on ActivityManager thread.
1579 mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
Fyodor Kupolov0f57cce2016-09-09 10:36:30 -07001580 userHandle /* arg1 */, 0 /* arg2 */));
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001581 }
1582 }
1583
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001584 void onUnlockUser(@UserIdInt int userId) {
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001585 synchronized(mMethodMap) {
1586 final int currentUserId = mSettings.getCurrentUserId();
1587 if (DEBUG) {
1588 Slog.d(TAG, "onUnlockUser: userId=" + userId + " curUserId=" + currentUserId);
1589 }
1590 if (userId != currentUserId) {
1591 return;
1592 }
1593 mSettings.switchCurrentUser(currentUserId, !mSystemReady);
Yohei Yukawa79247822017-01-23 15:26:15 -08001594 if (mSystemReady) {
1595 // We need to rebuild IMEs.
1596 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
1597 updateInputMethodsFromSettingsLocked(true /* enabledChanged */);
1598 }
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001599 }
Fyodor Kupolov1e33dc82016-01-21 12:02:58 -08001600 }
1601
Yohei Yukawad277d692020-02-19 17:12:17 -08001602 @GuardedBy("mMethodMap")
1603 void scheduleSwitchUserTaskLocked(@UserIdInt int userId,
1604 @Nullable IInputMethodClient clientToBeReset) {
1605 if (mUserSwitchHandlerTask != null) {
1606 if (mUserSwitchHandlerTask.mToUserId == userId) {
1607 mUserSwitchHandlerTask.mClientToBeReset = clientToBeReset;
1608 return;
1609 }
1610 mHandler.removeCallbacks(mUserSwitchHandlerTask);
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001611 }
Yohei Yukawad277d692020-02-19 17:12:17 -08001612 final UserSwitchHandlerTask task = new UserSwitchHandlerTask(this, userId,
1613 clientToBeReset);
1614 mUserSwitchHandlerTask = task;
1615 mHandler.post(task);
Yohei Yukawa7b18aec2016-03-07 13:04:32 -08001616 }
1617
Seigo Nonaka7309b122015-08-17 18:34:13 -07001618 public InputMethodManagerService(Context context) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001619 mIPackageManager = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 mContext = context;
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -08001621 mRes = context.getResources();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 mHandler = new Handler(this);
Yohei Yukawa81482972015-06-04 00:58:59 -07001623 // Note: SettingsObserver doesn't register observers in its constructor.
1624 mSettingsObserver = new SettingsObserver(mHandler);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 mIWindowManager = IWindowManager.Stub.asInterface(
1626 ServiceManager.getService(Context.WINDOW_SERVICE));
Seigo Nonaka7309b122015-08-17 18:34:13 -07001627 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
lpeter133fce02020-03-05 20:32:16 +08001628 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Yohei Yukawab4f328a2019-05-02 08:41:27 -07001629 mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
lumark9a72d222019-03-30 18:31:45 +08001630 mImeDisplayValidator = displayId -> mWindowManagerInternal.shouldShowIme(displayId);
Mita Yuned218c72012-12-06 17:18:25 -08001631 mCaller = new HandlerCaller(context, null, new HandlerCaller.Callback() {
satoke7c6998e2011-06-03 17:57:59 +09001632 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 public void executeMessage(Message msg) {
1634 handleMessage(msg);
1635 }
Mita Yuned218c72012-12-06 17:18:25 -08001636 }, true /*asyncHandler*/);
Yohei Yukawad34e1482016-02-11 08:03:52 -08001637 mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001638 mUserManager = mContext.getSystemService(UserManager.class);
Yohei Yukawa42081402019-01-15 09:57:50 -08001639 mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
satok01038492012-04-09 21:08:27 +09001640 mHardKeyboardListener = new HardKeyboardListener();
Dianne Hackborn119bbc32013-03-22 17:27:25 -07001641 mHasFeature = context.getPackageManager().hasSystemFeature(
1642 PackageManager.FEATURE_INPUT_METHODS);
Jason Monk3e189872016-01-12 09:10:34 -05001643 mSlotIme = mContext.getString(com.android.internal.R.string.status_bar_ime);
Tarandeep Singheadb1392018-11-09 18:15:57 +01001644 mIsLowRam = ActivityManager.isLowRamDeviceStatic();
satok7cfc0ed2011-06-20 21:29:36 +09001645
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001646 Bundle extras = new Bundle();
1647 extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001648 @ColorInt final int accentColor = mContext.getColor(
1649 com.android.internal.R.color.system_notification_accent_color);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001650 mImeSwitcherNotification =
1651 new Notification.Builder(mContext, SystemNotificationChannels.VIRTUAL_KEYBOARD)
1652 .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
1653 .setWhen(0)
1654 .setOngoing(true)
1655 .addExtras(extras)
1656 .setCategory(Notification.CATEGORY_SYSTEM)
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001657 .setColor(accentColor);
Daniel Sandler590d5152012-06-14 16:10:13 -04001658
Tadashi G. Takaoka01065a52017-07-19 14:10:24 +09001659 Intent intent = new Intent(ACTION_SHOW_INPUT_METHOD_PICKER)
1660 .setPackage(mContext.getPackageName());
satok683e2382011-07-12 08:28:52 +09001661 mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
satokb858c732011-07-22 19:54:34 +09001662
1663 mShowOngoingImeSwitcherForPhones = false;
satok7cfc0ed2011-06-20 21:29:36 +09001664
satok7cfc0ed2011-06-20 21:29:36 +09001665 mNotificationShown = false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001666 int userId = 0;
1667 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001668 userId = ActivityManager.getService().getCurrentUser().id;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001669 } catch (RemoteException e) {
1670 Slog.w(TAG, "Couldn't get current user ID; guessing it's 0", e);
1671 }
satok913a8922010-08-26 21:53:41 +09001672
Yohei Yukawaa7babbb2019-01-08 16:45:34 -08001673 mLastSwitchUserId = userId;
1674
satokd87c2592010-09-29 11:52:06 +09001675 // mSettings should be created before buildInputMethodListLocked
satokdf31ae62011-01-15 06:19:44 +09001676 mSettings = new InputMethodSettings(
Yohei Yukawaf9277532019-01-25 02:47:32 -08001677 mRes, context.getContentResolver(), mMethodMap, userId, !mSystemReady);
Svet Ganovadc1cf42015-06-15 16:36:24 -07001678
Kenny Guy2a764942014-04-02 13:29:20 +01001679 updateCurrentProfileIds();
Yohei Yukawab557d572018-12-29 21:26:26 -08001680 AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, userId);
Yohei Yukawa79247822017-01-23 15:26:15 -08001681 mSwitchingController = InputMethodSubtypeSwitchingController.createInstanceLocked(
1682 mSettings, context);
satok5b927c432012-05-01 20:09:34 +09001683 }
1684
satok5b927c432012-05-01 20:09:34 +09001685 private void resetDefaultImeLocked(Context context) {
1686 // Do not reset the default (current) IME when it is a 3rd-party IME
Yohei Yukawafd70fe82018-04-08 12:19:56 -07001687 if (mCurMethodId != null && !mMethodMap.get(mCurMethodId).isSystem()) {
satok5b927c432012-05-01 20:09:34 +09001688 return;
1689 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001690 final List<InputMethodInfo> suitableImes = InputMethodUtils.getDefaultEnabledImes(
Yohei Yukawaaf5cee82017-01-23 16:17:11 -08001691 context, mSettings.getEnabledInputMethodListLocked());
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001692 if (suitableImes.isEmpty()) {
1693 Slog.i(TAG, "No default found");
1694 return;
satok5b927c432012-05-01 20:09:34 +09001695 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001696 final InputMethodInfo defIm = suitableImes.get(0);
Yohei Yukawad0332832017-02-01 13:59:43 -08001697 if (DEBUG) {
1698 Slog.i(TAG, "Default found, using " + defIm.getId());
1699 }
Yohei Yukawac2393ac2016-02-18 00:30:45 -08001700 setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false);
satok5b927c432012-05-01 20:09:34 +09001701 }
1702
Andreas Gampea36dc622018-02-05 17:19:22 -08001703 @GuardedBy("mMethodMap")
Yohei Yukawad277d692020-02-19 17:12:17 -08001704 private void switchUserOnHandlerLocked(@UserIdInt int newUserId,
1705 IInputMethodClient clientToBeReset) {
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001706 if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId
1707 + " currentUserId=" + mSettings.getCurrentUserId());
1708
Yohei Yukawa81482972015-06-04 00:58:59 -07001709 // ContentObserver should be registered again when the user is changed
1710 mSettingsObserver.registerContentObserverLocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001711
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001712 // If the system is not ready or the device is not yed unlocked by the user, then we use
1713 // copy-on-write settings.
1714 final boolean useCopyOnWriteSettings =
Yohei Yukawa42081402019-01-15 09:57:50 -08001715 !mSystemReady || !mUserManagerInternal.isUserUnlockingOrUnlocked(newUserId);
Yohei Yukawa68645a62016-02-17 07:54:20 -08001716 mSettings.switchCurrentUser(newUserId, useCopyOnWriteSettings);
Kenny Guy2a764942014-04-02 13:29:20 +01001717 updateCurrentProfileIds();
Yohei Yukawab557d572018-12-29 21:26:26 -08001718 // Additional subtypes should be reset when the user is changed
1719 AdditionalSubtypeUtils.load(mAdditionalSubtypeMap, newUserId);
Satoshi Kataoka7f7535f2013-02-18 12:54:16 +09001720 final String defaultImiId = mSettings.getSelectedInputMethod();
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001721
1722 if (DEBUG) Slog.d(TAG, "Switching user stage 2/3. newUserId=" + newUserId
1723 + " defaultImiId=" + defaultImiId);
1724
Satoshi Kataoka7c4a2a12013-02-25 15:25:43 +09001725 // For secondary users, the list of enabled IMEs may not have been updated since the
1726 // callbacks to PackageMonitor are ignored for the secondary user. Here, defaultImiId may
1727 // not be empty even if the IME has been uninstalled by the primary user.
1728 // Even in such cases, IMMS works fine because it will find the most applicable
1729 // IME for that user.
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001730 final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001731 mLastSystemLocales = mRes.getConfiguration().getLocales();
1732
1733 // TODO: Is it really possible that switchUserLocked() happens before system ready?
1734 if (mSystemReady) {
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08001735 hideCurrentInputLocked(
1736 mCurFocusedWindow, 0, null, SoftInputShowHideReason.HIDE_SWITCH_USER);
1737
Yohei Yukawab7526452018-10-21 20:15:17 -07001738 resetCurrentMethodAndClient(UnbindReason.SWITCH_USER);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001739 buildInputMethodListLocked(initialUserSwitch);
1740 if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) {
1741 // This is the first time of the user switch and
1742 // set the current ime to the proper one.
1743 resetDefaultImeLocked(mContext);
1744 }
1745 updateFromSettingsLocked(true);
Yohei Yukawa0d7aff82017-02-10 00:40:51 -08001746 }
1747
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001748 if (initialUserSwitch) {
Yohei Yukawa094c71f2015-06-20 00:41:31 -07001749 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1750 mSettings.getEnabledInputMethodListLocked(), newUserId,
1751 mContext.getBasePackageName());
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09001752 }
Yohei Yukawa90bf7082015-06-03 23:50:27 -07001753
1754 if (DEBUG) Slog.d(TAG, "Switching user stage 3/3. newUserId=" + newUserId
1755 + " selectedIme=" + mSettings.getSelectedInputMethod());
Yohei Yukawaa7babbb2019-01-08 16:45:34 -08001756
1757 mLastSwitchUserId = newUserId;
Yohei Yukawad277d692020-02-19 17:12:17 -08001758
1759 if (mIsInteractive && clientToBeReset != null) {
1760 final ClientState cs = mClients.get(clientToBeReset.asBinder());
1761 if (cs == null) {
1762 // The client is already gone.
1763 return;
1764 }
1765 try {
1766 cs.client.scheduleStartInputIfNecessary(mInFullscreenMode);
1767 } catch (RemoteException e) {
1768 }
1769 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001770 }
1771
Kenny Guy2a764942014-04-02 13:29:20 +01001772 void updateCurrentProfileIds() {
Fyodor Kupolov7f98aa42016-04-07 14:56:25 -07001773 mSettings.setCurrentProfileIds(
1774 mUserManager.getProfileIdsWithDisabled(mSettings.getCurrentUserId()));
Amith Yamasani734983f2014-03-04 16:48:05 -08001775 }
1776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 @Override
1778 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1779 throws RemoteException {
1780 try {
1781 return super.onTransact(code, data, reply, flags);
1782 } catch (RuntimeException e) {
1783 // The input method manager only throws security exceptions, so let's
1784 // log all others.
1785 if (!(e instanceof SecurityException)) {
Dianne Hackborn164371f2013-10-01 19:10:13 -07001786 Slog.wtf(TAG, "Input Method Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 throw e;
1789 }
1790 }
1791
Svetoslav Ganova0027152013-06-25 14:59:53 -07001792 public void systemRunning(StatusBarManagerService statusBar) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001793 synchronized (mMethodMap) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001794 if (DEBUG) {
1795 Slog.d(TAG, "--- systemReady");
1796 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001797 if (!mSystemReady) {
1798 mSystemReady = true;
Yohei Yukawa79247822017-01-23 15:26:15 -08001799 mLastSystemLocales = mRes.getConfiguration().getLocales();
Yohei Yukawa68645a62016-02-17 07:54:20 -08001800 final int currentUserId = mSettings.getCurrentUserId();
Yohei Yukawaed4952a2016-02-17 07:57:25 -08001801 mSettings.switchCurrentUser(currentUserId,
Yohei Yukawa42081402019-01-15 09:57:50 -08001802 !mUserManagerInternal.isUserUnlockingOrUnlocked(currentUserId));
Yohei Yukawad34e1482016-02-11 08:03:52 -08001803 mKeyguardManager = mContext.getSystemService(KeyguardManager.class);
1804 mNotificationManager = mContext.getSystemService(NotificationManager.class);
Dianne Hackborn661cd522011-08-22 00:26:20 -07001805 mStatusBar = statusBar;
Griff Hazen6090c262016-03-25 08:11:24 -07001806 if (mStatusBar != null) {
1807 mStatusBar.setIconVisibility(mSlotIme, false);
1808 }
Yohei Yukawa849443c2019-01-21 09:02:25 -08001809 updateSystemUiLocked(mImeWindowVis, mBackDisposition);
satokb858c732011-07-22 19:54:34 +09001810 mShowOngoingImeSwitcherForPhones = mRes.getBoolean(
1811 com.android.internal.R.bool.show_ongoing_ime_switcher);
satok01038492012-04-09 21:08:27 +09001812 if (mShowOngoingImeSwitcherForPhones) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07001813 mWindowManagerInternal.setOnHardKeyboardStatusChangeListener(
satok01038492012-04-09 21:08:27 +09001814 mHardKeyboardListener);
1815 }
Yohei Yukawa79247822017-01-23 15:26:15 -08001816
1817 mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
1818 mSettingsObserver.registerContentObserverLocked(currentUserId);
1819
Yohei Yukawa0ec08002019-03-08 10:48:26 -08001820 final IntentFilter broadcastFilterForSystemUser = new IntentFilter();
1821 broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_ADDED);
1822 broadcastFilterForSystemUser.addAction(Intent.ACTION_USER_REMOVED);
1823 broadcastFilterForSystemUser.addAction(Intent.ACTION_LOCALE_CHANGED);
1824 broadcastFilterForSystemUser.addAction(ACTION_SHOW_INPUT_METHOD_PICKER);
1825 mContext.registerReceiver(new ImmsBroadcastReceiverForSystemUser(),
1826 broadcastFilterForSystemUser);
1827
1828 final IntentFilter broadcastFilterForAllUsers = new IntentFilter();
1829 broadcastFilterForAllUsers.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
1830 mContext.registerReceiverAsUser(new ImmsBroadcastReceiverForAllUsers(),
1831 UserHandle.ALL, broadcastFilterForAllUsers, null, null);
Yohei Yukawa79247822017-01-23 15:26:15 -08001832
Yohei Yukawa1f9a3cb2017-10-26 15:00:59 -07001833 final String defaultImiId = mSettings.getSelectedInputMethod();
1834 final boolean imeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId);
1835 buildInputMethodListLocked(!imeSelectedOnBoot /* resetDefaultEnabledIme */);
Yohei Yukawa79247822017-01-23 15:26:15 -08001836 updateFromSettingsLocked(true);
1837 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
1838 mSettings.getEnabledInputMethodListLocked(), currentUserId,
1839 mContext.getBasePackageName());
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07001840 }
1841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001843
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001844 // ---------------------------------------------------------------------------------------
1845 // Check whether or not this is a valid IPC. Assumes an IPC is valid when either
1846 // 1) it comes from the system process
1847 // 2) the calling process' user id is identical to the current user id IMMS thinks.
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08001848 @GuardedBy("mMethodMap")
Yohei Yukawa46d74762019-01-22 10:17:22 -08001849 private boolean calledFromValidUserLocked() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001850 final int uid = Binder.getCallingUid();
1851 final int userId = UserHandle.getUserId(uid);
1852 if (DEBUG) {
1853 Slog.d(TAG, "--- calledFromForegroundUserOrSystemProcess ? "
1854 + "calling uid = " + uid + " system uid = " + Process.SYSTEM_UID
1855 + " calling userId = " + userId + ", foreground user id = "
Satoshi Kataoka87c29142013-07-31 23:11:54 +09001856 + mSettings.getCurrentUserId() + ", calling pid = " + Binder.getCallingPid()
1857 + InputMethodUtils.getApiCallStack());
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001858 }
Yohei Yukawaa878b952019-01-10 19:36:24 -08001859 if (uid == Process.SYSTEM_UID) {
1860 return true;
1861 }
1862 if (userId == mSettings.getCurrentUserId()) {
1863 return true;
1864 }
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001865
1866 // Caveat: A process which has INTERACT_ACROSS_USERS_FULL gets results for the
1867 // foreground user, not for the user of that process. Accordingly InputMethodManagerService
1868 // must not manage background users' states in any functions.
1869 // Note that privacy-sensitive IPCs, such as setInputMethod, are still securely guarded
1870 // by a token.
1871 if (mContext.checkCallingOrSelfPermission(
1872 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1873 == PackageManager.PERMISSION_GRANTED) {
1874 if (DEBUG) {
1875 Slog.d(TAG, "--- Access granted because the calling process has "
1876 + "the INTERACT_ACROSS_USERS_FULL permission");
1877 }
1878 return true;
1879 }
Yohei Yukawad0332832017-02-01 13:59:43 -08001880 // TODO(b/34886274): The semantics of this verification is actually not well-defined.
Seigo Nonakae27dc2b2015-08-14 18:21:27 -07001881 Slog.w(TAG, "--- IPC called from background users. Ignore. callers="
1882 + Debug.getCallers(10));
Satoshi Kataoka135e5fb2012-09-28 18:25:06 +09001883 return false;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001884 }
1885
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001886
1887 /**
1888 * Returns true iff the caller is identified to be the current input method with the token.
1889 * @param token The window token given to the input method when it was started.
1890 * @return true if and only if non-null valid token is specified.
1891 */
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08001892 @GuardedBy("mMethodMap")
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08001893 private boolean calledWithValidTokenLocked(@NonNull IBinder token) {
1894 if (token == null) {
1895 throw new InvalidParameterException("token must not be null.");
Yohei Yukawad0332832017-02-01 13:59:43 -08001896 }
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08001897 if (token != mCurToken) {
Yohei Yukawad0332832017-02-01 13:59:43 -08001898 Slog.e(TAG, "Ignoring " + Debug.getCaller() + " due to an invalid token."
1899 + " uid:" + Binder.getCallingUid() + " token:" + token);
Yohei Yukawa22c97be2014-06-04 19:43:36 +09001900 return false;
1901 }
1902 return true;
1903 }
1904
Yohei Yukawaf80087c2018-05-21 09:47:53 -07001905 @GuardedBy("mMethodMap")
1906 private boolean bindCurrentInputMethodServiceLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001907 Intent service, ServiceConnection conn, int flags) {
1908 if (service == null || conn == null) {
1909 Slog.e(TAG, "--- bind failed: service = " + service + ", conn = " + conn);
1910 return false;
1911 }
Amith Yamasani27b89e62013-01-16 12:30:11 -08001912 return mContext.bindServiceAsUser(service, conn, flags,
1913 new UserHandle(mSettings.getCurrentUserId()));
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09001914 }
1915
satoke7c6998e2011-06-03 17:57:59 +09001916 @Override
Yohei Yukawad20eef82019-02-05 10:45:32 -08001917 public List<InputMethodInfo> getInputMethodList(@UserIdInt int userId) {
1918 if (UserHandle.getCallingUserId() != userId) {
1919 mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, null);
1920 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001921 synchronized (mMethodMap) {
Yohei Yukawad20eef82019-02-05 10:45:32 -08001922 final int[] resolvedUserIds = InputMethodUtils.resolveUserId(userId,
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001923 mSettings.getCurrentUserId(), null);
1924 if (resolvedUserIds.length != 1) {
1925 return Collections.emptyList();
1926 }
1927 final long ident = Binder.clearCallingIdentity();
1928 try {
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08001929 return getInputMethodListLocked(resolvedUserIds[0]);
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001930 } finally {
1931 Binder.restoreCallingIdentity(ident);
Tarandeep Singh89a6c482017-11-21 14:26:11 -08001932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 }
1935
satoke7c6998e2011-06-03 17:57:59 +09001936 @Override
Yohei Yukawa1fb13c52019-02-05 07:55:28 -08001937 public List<InputMethodInfo> getEnabledInputMethodList(@UserIdInt int userId) {
1938 if (UserHandle.getCallingUserId() != userId) {
1939 mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL, null);
1940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 synchronized (mMethodMap) {
Yohei Yukawa1fb13c52019-02-05 07:55:28 -08001942 final int[] resolvedUserIds = InputMethodUtils.resolveUserId(userId,
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001943 mSettings.getCurrentUserId(), null);
1944 if (resolvedUserIds.length != 1) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08001945 return Collections.emptyList();
1946 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001947 final long ident = Binder.clearCallingIdentity();
1948 try {
1949 return getEnabledInputMethodListLocked(resolvedUserIds[0]);
1950 } finally {
1951 Binder.restoreCallingIdentity(ident);
1952 }
1953 }
1954 }
1955
1956 @GuardedBy("mMethodMap")
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08001957 private List<InputMethodInfo> getInputMethodListLocked(@UserIdInt int userId) {
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001958 final ArrayList<InputMethodInfo> methodList;
1959 if (userId == mSettings.getCurrentUserId()) {
1960 // Create a copy.
1961 methodList = new ArrayList<>(mMethodList);
1962 } else {
1963 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
1964 methodList = new ArrayList<>();
1965 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
1966 new ArrayMap<>();
1967 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
1968 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap,
1969 methodList);
1970 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001971 return methodList;
1972 }
1973
1974 @GuardedBy("mMethodMap")
1975 private List<InputMethodInfo> getEnabledInputMethodListLocked(@UserIdInt int userId) {
1976 if (userId == mSettings.getCurrentUserId()) {
satokd87c2592010-09-29 11:52:06 +09001977 return mSettings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001979 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
1980 final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
1981 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
1982 new ArrayMap<>();
1983 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
1984 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap,
1985 methodList);
1986 final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(),
Yohei Yukawaf9277532019-01-25 02:47:32 -08001987 mContext.getContentResolver(), methodMap, userId, true);
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08001988 return settings.getEnabledInputMethodListLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 }
1990
Adam Hebc67f2e2019-11-13 14:34:56 -08001991 @GuardedBy("mMethodMap")
Feng Cao16b2de52020-01-09 17:27:27 -08001992 private void onCreateInlineSuggestionsRequestLocked(@UserIdInt int userId,
Feng Cao36960ee2020-02-18 18:23:30 -08001993 InlineSuggestionsRequestInfo requestInfo, IInlineSuggestionsRequestCallback callback) {
Adam He7bc8f602019-12-12 17:00:34 -08001994 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
1995 try {
Feng Cao16b2de52020-01-09 17:27:27 -08001996 if (userId == mSettings.getCurrentUserId() && imi != null
1997 && imi.isInlineSuggestionsEnabled() && mCurMethod != null) {
Adam He7bc8f602019-12-12 17:00:34 -08001998 executeOrSendMessage(mCurMethod,
Feng Cao36960ee2020-02-18 18:23:30 -08001999 mCaller.obtainMessageOOO(MSG_INLINE_SUGGESTIONS_REQUEST, mCurMethod,
2000 requestInfo, new InlineSuggestionsRequestCallbackDecorator(callback,
lpeter133fce02020-03-05 20:32:16 +08002001 imi.getPackageName(), mCurTokenDisplayId, mCurToken,
2002 this)));
Adam He7bc8f602019-12-12 17:00:34 -08002003 } else {
2004 callback.onInlineSuggestionsUnsupported();
2005 }
2006 } catch (RemoteException e) {
2007 Slog.w(TAG, "RemoteException calling onCreateInlineSuggestionsRequest(): " + e);
Adam Hebc67f2e2019-11-13 14:34:56 -08002008 }
2009 }
2010
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002011 /**
Feng Cao16b2de52020-01-09 17:27:27 -08002012 * The decorator which validates the host package name in the
2013 * {@link InlineSuggestionsRequest} argument to make sure it matches the IME package name.
2014 */
2015 private static final class InlineSuggestionsRequestCallbackDecorator
2016 extends IInlineSuggestionsRequestCallback.Stub {
2017 @NonNull
2018 private final IInlineSuggestionsRequestCallback mCallback;
2019 @NonNull
2020 private final String mImePackageName;
2021
Svet Ganov02fdd342020-02-20 20:48:34 -08002022 private final int mImeDisplayId;
2023
lpeter133fce02020-03-05 20:32:16 +08002024 @NonNull
2025 private final IBinder mImeToken;
2026 @NonNull
2027 private final InputMethodManagerService mImms;
2028
Feng Cao16b2de52020-01-09 17:27:27 -08002029 InlineSuggestionsRequestCallbackDecorator(
lpeter133fce02020-03-05 20:32:16 +08002030 @NonNull IInlineSuggestionsRequestCallback callback, @NonNull String imePackageName,
2031 int displayId, @NonNull IBinder imeToken, @NonNull InputMethodManagerService imms) {
Feng Cao16b2de52020-01-09 17:27:27 -08002032 mCallback = callback;
2033 mImePackageName = imePackageName;
Svet Ganov02fdd342020-02-20 20:48:34 -08002034 mImeDisplayId = displayId;
lpeter133fce02020-03-05 20:32:16 +08002035 mImeToken = imeToken;
2036 mImms = imms;
Feng Cao16b2de52020-01-09 17:27:27 -08002037 }
2038
2039 @Override
2040 public void onInlineSuggestionsUnsupported() throws RemoteException {
2041 mCallback.onInlineSuggestionsUnsupported();
2042 }
2043
2044 @Override
2045 public void onInlineSuggestionsRequest(InlineSuggestionsRequest request,
Feng Cao97ec1c42020-03-25 12:20:42 -07002046 IInlineSuggestionsResponseCallback callback)
Feng Cao7c85eb72020-02-28 11:39:56 -08002047 throws RemoteException {
Feng Cao16b2de52020-01-09 17:27:27 -08002048 if (!mImePackageName.equals(request.getHostPackageName())) {
2049 throw new SecurityException(
2050 "Host package name in the provide request=[" + request.getHostPackageName()
2051 + "] doesn't match the IME package name=[" + mImePackageName
2052 + "].");
2053 }
Svet Ganov02fdd342020-02-20 20:48:34 -08002054 request.setHostDisplayId(mImeDisplayId);
lpeter133fce02020-03-05 20:32:16 +08002055 mImms.setCurHostInputToken(mImeToken, request.getHostInputToken());
Feng Cao97ec1c42020-03-25 12:20:42 -07002056 mCallback.onInlineSuggestionsRequest(request, callback);
Feng Cao7c85eb72020-02-28 11:39:56 -08002057 }
2058
2059 @Override
Feng Cao97ec1c42020-03-25 12:20:42 -07002060 public void onInputMethodStartInput(AutofillId imeFieldId) throws RemoteException {
2061 mCallback.onInputMethodStartInput(imeFieldId);
Feng Cao7c85eb72020-02-28 11:39:56 -08002062 }
2063
2064 @Override
Feng Cao97ec1c42020-03-25 12:20:42 -07002065 public void onInputMethodShowInputRequested(boolean requestResult) throws RemoteException {
2066 mCallback.onInputMethodShowInputRequested(requestResult);
2067 }
2068
2069 @Override
2070 public void onInputMethodStartInputView() throws RemoteException {
2071 mCallback.onInputMethodStartInputView();
2072 }
2073
2074 @Override
2075 public void onInputMethodFinishInputView() throws RemoteException {
2076 mCallback.onInputMethodFinishInputView();
2077 }
2078
2079 @Override
2080 public void onInputMethodFinishInput() throws RemoteException {
2081 mCallback.onInputMethodFinishInput();
Feng Cao16b2de52020-01-09 17:27:27 -08002082 }
2083 }
2084
2085 /**
lpeter133fce02020-03-05 20:32:16 +08002086 * Sets current host input token.
2087 *
2088 * @param callerImeToken the token has been made for the current active input method
2089 * @param hostInputToken the host input token of the current active input method
2090 */
2091 void setCurHostInputToken(@NonNull IBinder callerImeToken, @Nullable IBinder hostInputToken) {
2092 synchronized (mMethodMap) {
2093 if (!calledWithValidTokenLocked(callerImeToken)) {
2094 return;
2095 }
2096 mCurHostInputToken = hostInputToken;
2097 }
2098 }
2099
2100 /**
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09002101 * @param imiId if null, returns enabled subtypes for the current imi
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002102 * @return enabled subtypes of the specified imi
2103 */
satoke7c6998e2011-06-03 17:57:59 +09002104 @Override
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09002105 public List<InputMethodSubtype> getEnabledInputMethodSubtypeList(String imiId,
satok16331c82010-12-20 23:48:46 +09002106 boolean allowsImplicitlySelectedSubtypes) {
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08002107 final int callingUserId = UserHandle.getCallingUserId();
satok67ddf9c2010-11-17 09:45:54 +09002108 synchronized (mMethodMap) {
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08002109 final int[] resolvedUserIds = InputMethodUtils.resolveUserId(callingUserId,
2110 mSettings.getCurrentUserId(), null);
2111 if (resolvedUserIds.length != 1) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08002112 return Collections.emptyList();
2113 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08002114 final long ident = Binder.clearCallingIdentity();
2115 try {
2116 return getEnabledInputMethodSubtypeListLocked(imiId,
2117 allowsImplicitlySelectedSubtypes, resolvedUserIds[0]);
2118 } finally {
2119 Binder.restoreCallingIdentity(ident);
2120 }
2121 }
2122 }
2123
2124 @GuardedBy("mMethodMap")
2125 private List<InputMethodSubtype> getEnabledInputMethodSubtypeListLocked(String imiId,
2126 boolean allowsImplicitlySelectedSubtypes, @UserIdInt int userId) {
2127 if (userId == mSettings.getCurrentUserId()) {
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09002128 final InputMethodInfo imi;
2129 if (imiId == null && mCurMethodId != null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002130 imi = mMethodMap.get(mCurMethodId);
Satoshi Kataokab3c21ac2013-08-07 15:43:29 +09002131 } else {
2132 imi = mMethodMap.get(imiId);
2133 }
2134 if (imi == null) {
Yohei Yukawab0377bb2015-08-10 21:06:30 -07002135 return Collections.emptyList();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002136 }
2137 return mSettings.getEnabledInputMethodSubtypeListLocked(
2138 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09002139 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08002140 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
2141 final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
2142 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
2143 new ArrayMap<>();
2144 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
2145 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap, methodMap,
2146 methodList);
2147 final InputMethodInfo imi = methodMap.get(imiId);
2148 if (imi == null) {
2149 return Collections.emptyList();
2150 }
2151 final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(),
Yohei Yukawaf9277532019-01-25 02:47:32 -08002152 mContext.getContentResolver(), methodMap, userId, true);
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08002153 return settings.getEnabledInputMethodSubtypeListLocked(
2154 mContext, imi, allowsImplicitlySelectedSubtypes);
satok67ddf9c2010-11-17 09:45:54 +09002155 }
2156
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002157 /**
2158 * Called by each application process as a preparation to start interacting with
2159 * {@link InputMethodManagerService}.
2160 *
2161 * <p>As a general principle, IPCs from the application process that take
Yohei Yukawa499e3f72018-10-21 20:15:11 -07002162 * {@link IInputMethodClient} will be rejected without this step.</p>
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002163 *
2164 * @param client {@link android.os.Binder} proxy that is associated with the singleton instance
2165 * of {@link android.view.inputmethod.InputMethodManager} that runs on the client
2166 * process
2167 * @param inputContext communication channel for the dummy
2168 * {@link android.view.inputmethod.InputConnection}
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002169 * @param selfReportedDisplayId self-reported display ID to which the client is associated.
2170 * Whether the client is still allowed to access to this display
2171 * or not needs to be evaluated every time the client interacts
2172 * with the display
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002173 */
2174 @Override
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002175 public void addClient(IInputMethodClient client, IInputContext inputContext,
2176 int selfReportedDisplayId) {
Yohei Yukawacb768bc2018-10-24 16:05:09 -07002177 // Here there are two scenarios where this method is called:
2178 // A. IMM is being instantiated in a different process and this is an IPC from that process
2179 // B. IMM is being instantiated in the same process but Binder.clearCallingIdentity() is
2180 // called in the caller side if necessary.
2181 // In either case the following UID/PID should be the ones where InputMethodManager is
2182 // actually running.
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002183 final int callerUid = Binder.getCallingUid();
2184 final int callerPid = Binder.getCallingPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 synchronized (mMethodMap) {
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002186 // TODO: Optimize this linear search.
Yohei Yukawaac9311e2018-11-20 19:25:23 -08002187 final int numClients = mClients.size();
2188 for (int i = 0; i < numClients; ++i) {
2189 final ClientState state = mClients.valueAt(i);
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002190 if (state.uid == callerUid && state.pid == callerPid
2191 && state.selfReportedDisplayId == selfReportedDisplayId) {
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002192 throw new SecurityException("uid=" + callerUid + "/pid=" + callerPid
Yohei Yukawacb768bc2018-10-24 16:05:09 -07002193 + "/displayId=" + selfReportedDisplayId + " is already registered.");
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002194 }
2195 }
2196 final ClientDeathRecipient deathRecipient = new ClientDeathRecipient(this, client);
2197 try {
2198 client.asBinder().linkToDeath(deathRecipient, 0);
2199 } catch (RemoteException e) {
2200 throw new IllegalStateException(e);
2201 }
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002202 // We cannot fully avoid race conditions where the client UID already lost the access to
2203 // the given self-reported display ID, even if the client is not maliciously reporting
2204 // a fake display ID. Unconditionally returning SecurityException just because the
2205 // client doesn't pass display ID verification can cause many test failures hence not an
2206 // option right now. At the same time
2207 // context.getSystemService(InputMethodManager.class)
2208 // is expected to return a valid non-null instance at any time if we do not choose to
2209 // have the client crash. Thus we do not verify the display ID at all here. Instead we
2210 // later check the display ID every time the client needs to interact with the specified
2211 // display.
2212 mClients.put(client.asBinder(), new ClientState(client, inputContext, callerUid,
2213 callerPid, selfReportedDisplayId, deathRecipient));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 }
2215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002216
Yohei Yukawae24ed792018-08-28 19:10:32 -07002217 void removeClient(IInputMethodClient client) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 synchronized (mMethodMap) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002219 ClientState cs = mClients.remove(client.asBinder());
2220 if (cs != null) {
Yohei Yukawaa71bb252018-09-19 19:21:24 -07002221 client.asBinder().unlinkToDeath(cs.clientDeathRecipient, 0);
Jeff Brownc28867a2013-03-26 15:42:39 -07002222 clearClientSessionLocked(cs);
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002223
2224 final int numItems = mActivityViewDisplayIdToParentMap.size();
2225 for (int i = numItems - 1; i >= 0; --i) {
2226 final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.valueAt(i);
2227 if (info.mParentClient == cs) {
2228 mActivityViewDisplayIdToParentMap.removeAt(i);
2229 }
2230 }
2231
Yohei Yukawa072b1b52015-11-18 15:54:34 -08002232 if (mCurClient == cs) {
Tarandeep Singh93c00cea2018-02-16 14:31:17 -08002233 if (mBoundToMethod) {
2234 mBoundToMethod = false;
2235 if (mCurMethod != null) {
2236 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2237 MSG_UNBIND_INPUT, mCurMethod));
2238 }
2239 }
Yohei Yukawa072b1b52015-11-18 15:54:34 -08002240 mCurClient = null;
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002241 mCurActivityViewToScreenMatrix = null;
Yohei Yukawa072b1b52015-11-18 15:54:34 -08002242 }
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08002243 if (mCurFocusedWindowClient == cs) {
2244 mCurFocusedWindowClient = null;
2245 }
Jeff Brownc28867a2013-03-26 15:42:39 -07002246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 }
2248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 void executeOrSendMessage(IInterface target, Message msg) {
2251 if (target.asBinder() instanceof Binder) {
2252 mCaller.sendMessage(msg);
2253 } else {
2254 handleMessage(msg);
2255 msg.recycle();
2256 }
2257 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002258
Yohei Yukawa4afd9332018-10-21 10:43:53 -07002259 void unbindCurrentClientLocked(@UnbindReason int unbindClientReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 if (mCurClient != null) {
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002261 if (DEBUG) Slog.v(TAG, "unbindCurrentInputLocked: client="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 + mCurClient.client.asBinder());
2263 if (mBoundToMethod) {
2264 mBoundToMethod = false;
2265 if (mCurMethod != null) {
2266 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
2267 MSG_UNBIND_INPUT, mCurMethod));
2268 }
2269 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07002270
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002271 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
2272 MSG_SET_ACTIVE, 0, 0, mCurClient));
Yohei Yukawa33e81792015-11-17 21:14:42 -08002273 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIIO(
2274 MSG_UNBIND_CLIENT, mCurSeq, unbindClientReason, mCurClient.client));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 mCurClient.sessionRequested = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 mCurClient = null;
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002277 mCurActivityViewToScreenMatrix = null;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002278
The Android Open Source Project10592532009-03-18 17:39:46 -07002279 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 }
2281 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 private int getImeShowFlags() {
2284 int flags = 0;
2285 if (mShowForced) {
2286 flags |= InputMethod.SHOW_FORCED
2287 | InputMethod.SHOW_EXPLICIT;
2288 } else if (mShowExplicitlyRequested) {
2289 flags |= InputMethod.SHOW_EXPLICIT;
2290 }
2291 return flags;
2292 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 private int getAppShowFlags() {
2295 int flags = 0;
2296 if (mShowForced) {
2297 flags |= InputMethodManager.SHOW_FORCED;
2298 } else if (!mShowExplicitlyRequested) {
2299 flags |= InputMethodManager.SHOW_IMPLICIT;
2300 }
2301 return flags;
2302 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002303
Andreas Gampea36dc622018-02-05 17:19:22 -08002304 @GuardedBy("mMethodMap")
Yohei Yukawa2553e482017-12-15 15:47:33 -08002305 @NonNull
Yohei Yukawadc66e522018-10-21 10:43:14 -07002306 InputBindResult attachNewInputLocked(@StartInputReason int startInputReason, boolean initial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 if (!mBoundToMethod) {
2308 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOO(
2309 MSG_BIND_INPUT, mCurMethod, mCurClient.binding));
2310 mBoundToMethod = true;
2311 }
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002312
2313 final Binder startInputToken = new Binder();
Yohei Yukawa7979e1a2019-02-12 02:01:10 -08002314 final StartInputInfo info = new StartInputInfo(mSettings.getCurrentUserId(), mCurToken,
2315 mCurTokenDisplayId, mCurId, startInputReason, !initial,
2316 UserHandle.getUserId(mCurClient.uid), mCurClient.selfReportedDisplayId,
2317 mCurFocusedWindow, mCurAttribute, mCurFocusedWindowSoftInputMode, mCurSeq);
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -07002318 mImeTargetWindowMap.put(startInputToken, mCurFocusedWindow);
Yohei Yukawa357b2f62017-02-14 09:40:03 -08002319 mStartInputHistory.addEntry(info);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002321 final SessionState session = mCurClient.curSession;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002322 executeOrSendMessage(session.method, mCaller.obtainMessageIIOOOO(
Yohei Yukawaf7526b52017-02-11 20:57:10 -08002323 MSG_START_INPUT, mCurInputContextMissingMethods, initial ? 0 : 1 /* restarting */,
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08002324 startInputToken, session, mCurInputContext, mCurAttribute));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 if (mShowRequested) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002326 if (DEBUG) Slog.v(TAG, "Attach new input asks to show input");
lumarkd85e1582019-12-29 20:20:41 +08002327 showCurrentInputLocked(mCurFocusedWindow, getAppShowFlags(), null,
2328 SoftInputShowHideReason.ATTACH_NEW_INPUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 }
Yohei Yukawa2553e482017-12-15 15:47:33 -08002330 return new InputBindResult(InputBindResult.ResultCode.SUCCESS_WITH_IME_SESSION,
2331 session.session, (session.channel != null ? session.channel.dup() : null),
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002332 mCurId, mCurSeq, mCurActivityViewToScreenMatrix);
2333 }
2334
2335 @Nullable
2336 private Matrix getActivityViewToScreenMatrixLocked(int clientDisplayId, int imeDisplayId) {
2337 if (clientDisplayId == imeDisplayId) {
2338 return null;
2339 }
2340 int displayId = clientDisplayId;
2341 Matrix matrix = null;
2342 while (true) {
2343 final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(displayId);
2344 if (info == null) {
2345 return null;
2346 }
2347 if (matrix == null) {
2348 matrix = new Matrix(info.mMatrix);
2349 } else {
2350 matrix.postConcat(info.mMatrix);
2351 }
2352 if (info.mParentClient.selfReportedDisplayId == imeDisplayId) {
2353 return matrix;
2354 }
2355 displayId = info.mParentClient.selfReportedDisplayId;
2356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002358
Andreas Gampea36dc622018-02-05 17:19:22 -08002359 @GuardedBy("mMethodMap")
Yohei Yukawa2553e482017-12-15 15:47:33 -08002360 @NonNull
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002361 InputBindResult startInputUncheckedLocked(@NonNull ClientState cs, IInputContext inputContext,
Yohei Yukawa35fa6d52018-10-31 11:33:32 -07002362 @MissingMethodFlags int missingMethods, @NonNull EditorInfo attribute,
2363 @StartInputFlags int startInputFlags, @StartInputReason int startInputReason) {
Dianne Hackborn7663d802012-02-24 13:08:49 -08002364 // If no method is currently selected, do nothing.
2365 if (mCurMethodId == null) {
Yohei Yukawa2553e482017-12-15 15:47:33 -08002366 return InputBindResult.NO_IME;
Dianne Hackborn7663d802012-02-24 13:08:49 -08002367 }
2368
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002369 if (!mSystemReady) {
2370 // If the system is not yet ready, we shouldn't be running third
2371 // party code.
2372 return new InputBindResult(
2373 InputBindResult.ResultCode.ERROR_SYSTEM_NOT_READY,
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002374 null, null, mCurMethodId, mCurSeq, null);
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002375 }
2376
Yohei Yukawad57ba672015-06-08 16:39:46 -07002377 if (!InputMethodUtils.checkIfPackageBelongsToUid(mAppOpsManager, cs.uid,
2378 attribute.packageName)) {
2379 Slog.e(TAG, "Rejecting this client as it reported an invalid package name."
2380 + " uid=" + cs.uid + " package=" + attribute.packageName);
Yohei Yukawa2553e482017-12-15 15:47:33 -08002381 return InputBindResult.INVALID_PACKAGE_NAME;
Yohei Yukawa0f3ad12015-04-06 16:48:24 -07002382 }
2383
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002384 if (!mWindowManagerInternal.isUidAllowedOnDisplay(cs.selfReportedDisplayId, cs.uid)) {
2385 // Wait, the client no longer has access to the display.
2386 return InputBindResult.INVALID_DISPLAY_ID;
2387 }
lumarkef1965b2018-09-12 17:42:53 +08002388 // Compute the final shown display ID with validated cs.selfReportedDisplayId for this
2389 // session & other conditions.
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08002390 final int displayIdToShowIme = computeImeDisplayIdForTarget(cs.selfReportedDisplayId,
2391 mImeDisplayValidator);
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 if (mCurClient != cs) {
John Spurlocke0980502013-10-25 11:59:29 -04002394 // Was the keyguard locked when switching over to the new client?
2395 mCurClientInKeyguard = isKeyguardLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 // If the client is changing, we need to switch over to the new
2397 // one.
Yohei Yukawab7526452018-10-21 20:15:17 -07002398 unbindCurrentClientLocked(UnbindReason.SWITCH_CLIENT);
Yohei Yukawa35d3f372015-11-25 11:07:19 -08002399 if (DEBUG) Slog.v(TAG, "switching to client: client="
John Spurlocke0980502013-10-25 11:59:29 -04002400 + cs.client.asBinder() + " keyguard=" + mCurClientInKeyguard);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401
2402 // If the screen is on, inform the new client it is active
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07002403 if (mIsInteractive) {
tiansiming [田思明]e102c972018-04-17 18:15:33 +08002404 executeOrSendMessage(cs.client, mCaller.obtainMessageIO(MSG_SET_ACTIVE, 1, cs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 }
2406 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 // Bump up the sequence for this client and attach it.
2409 mCurSeq++;
2410 if (mCurSeq <= 0) mCurSeq = 1;
2411 mCurClient = cs;
2412 mCurInputContext = inputContext;
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002413 mCurActivityViewToScreenMatrix =
2414 getActivityViewToScreenMatrixLocked(cs.selfReportedDisplayId, displayIdToShowIme);
2415 if (cs.selfReportedDisplayId != displayIdToShowIme
2416 && mCurActivityViewToScreenMatrix == null) {
Yohei Yukawa3d2cc0f2019-04-25 18:32:15 -07002417 // CursorAnchorInfo API does not work as-is for cross-display scenario. Pretend that
2418 // InputConnection#requestCursorUpdates() is not implemented in the application so that
2419 // IMEs will always receive false from this API.
2420 missingMethods |= MissingMethodFlags.REQUEST_CURSOR_UPDATES;
2421 }
Yohei Yukawa19a80a12016-03-14 22:57:37 -07002422 mCurInputContextMissingMethods = missingMethods;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 mCurAttribute = attribute;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 // Check if the input method is changing.
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002426 // We expect the caller has already verified that the client is allowed to access this
2427 // display ID.
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002428 if (mCurId != null && mCurId.equals(mCurMethodId)
2429 && displayIdToShowIme == mCurTokenDisplayId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 if (cs.curSession != null) {
2431 // Fast case: if we are already connected to the input method,
2432 // then just return it.
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002433 return attachNewInputLocked(startInputReason,
Yohei Yukawa35fa6d52018-10-31 11:33:32 -07002434 (startInputFlags & StartInputFlags.INITIAL_CONNECTION) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 }
2436 if (mHaveConnection) {
2437 if (mCurMethod != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 // Return to client, and we will get back with it when
2439 // we have had a session made for it.
Jeff Brownc28867a2013-03-26 15:42:39 -07002440 requestClientSessionLocked(cs);
Yohei Yukawa2553e482017-12-15 15:47:33 -08002441 return new InputBindResult(
2442 InputBindResult.ResultCode.SUCCESS_WAITING_IME_SESSION,
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002443 null, null, mCurId, mCurSeq, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 } else if (SystemClock.uptimeMillis()
2445 < (mLastBindTime+TIME_TO_RECONNECT)) {
2446 // In this case we have connected to the service, but
2447 // don't yet have its interface. If it hasn't been too
2448 // long since we did the connection, we'll return to
2449 // the client and wait to get the service interface so
2450 // we can report back. If it has been too long, we want
2451 // to fall through so we can try a disconnect/reconnect
2452 // to see if we can get back in touch with the service.
Yohei Yukawa2553e482017-12-15 15:47:33 -08002453 return new InputBindResult(
2454 InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING,
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002455 null, null, mCurId, mCurSeq, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 } else {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002457 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME,
2458 mCurMethodId, SystemClock.uptimeMillis()-mLastBindTime, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
2460 }
2461 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 InputMethodInfo info = mMethodMap.get(mCurMethodId);
2464 if (info == null) {
2465 throw new IllegalArgumentException("Unknown id: " + mCurMethodId);
2466 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002467
Yohei Yukawad4d895e2018-09-24 16:01:20 -07002468 unbindCurrentMethodLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 mCurIntent = new Intent(InputMethod.SERVICE_INTERFACE);
2471 mCurIntent.setComponent(info.getComponent());
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002472 mCurIntent.putExtra(Intent.EXTRA_CLIENT_LABEL,
2473 com.android.internal.R.string.input_method_binding_label);
2474 mCurIntent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
2475 mContext, 0, new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 0));
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08002476
Yohei Yukawaf80087c2018-05-21 09:47:53 -07002477 if (bindCurrentInputMethodServiceLocked(mCurIntent, this, IME_CONNECTION_BIND_FLAGS)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 mLastBindTime = SystemClock.uptimeMillis();
2479 mHaveConnection = true;
2480 mCurId = info.getId();
2481 mCurToken = new Binder();
Yohei Yukawa0deaef02018-10-17 10:33:48 +08002482 mCurTokenDisplayId = displayIdToShowIme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 try {
lumark90120a82018-08-15 00:33:03 +08002484 if (DEBUG) {
2485 Slog.v(TAG, "Adding window token: " + mCurToken + " for display: "
2486 + mCurTokenDisplayId);
2487 }
Yohei Yukawa6e875592019-01-28 00:49:30 -08002488 mIWindowManager.addWindowToken(mCurToken, LayoutParams.TYPE_INPUT_METHOD,
2489 mCurTokenDisplayId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 } catch (RemoteException e) {
2491 }
Yohei Yukawa2553e482017-12-15 15:47:33 -08002492 return new InputBindResult(
2493 InputBindResult.ResultCode.SUCCESS_WAITING_IME_BINDING,
Yohei Yukawab4f328a2019-05-02 08:41:27 -07002494 null, null, mCurId, mCurSeq, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 }
Yohei Yukawa2553e482017-12-15 15:47:33 -08002496 mCurIntent = null;
2497 Slog.w(TAG, "Failure connecting to input method service: " + mCurIntent);
2498 return InputBindResult.IME_NOT_CONNECTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002500
lumarkef1965b2018-09-12 17:42:53 +08002501 @FunctionalInterface
2502 interface ImeDisplayValidator {
2503 boolean displayCanShowIme(int displayId);
2504 }
2505
2506 /**
2507 * Find the display where the IME should be shown.
2508 *
2509 * @param displayId the ID of the display where the IME client target is.
lumarkef1965b2018-09-12 17:42:53 +08002510 * @param checker instance of {@link ImeDisplayValidator} which is used for
2511 * checking display config to adjust the final target display.
2512 * @return The ID of the display where the IME should be shown.
2513 */
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08002514 static int computeImeDisplayIdForTarget(int displayId, @NonNull ImeDisplayValidator checker) {
lumarkef1965b2018-09-12 17:42:53 +08002515 if (displayId == DEFAULT_DISPLAY || displayId == INVALID_DISPLAY) {
lumark7570cac2019-03-07 22:14:38 +08002516 return FALLBACK_DISPLAY_ID;
lumarkef1965b2018-09-12 17:42:53 +08002517 }
lumark9a72d222019-03-30 18:31:45 +08002518
2519 // Show IME window on fallback display when the display doesn't support system decorations
2520 // or the display is virtual and isn't owned by system for security concern.
lumark7570cac2019-03-07 22:14:38 +08002521 return checker.displayCanShowIme(displayId) ? displayId : FALLBACK_DISPLAY_ID;
lumarkef1965b2018-09-12 17:42:53 +08002522 }
2523
satoke7c6998e2011-06-03 17:57:59 +09002524 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 public void onServiceConnected(ComponentName name, IBinder service) {
2526 synchronized (mMethodMap) {
2527 if (mCurIntent != null && name.equals(mCurIntent.getComponent())) {
2528 mCurMethod = IInputMethod.Stub.asInterface(service);
Dianne Hackborncc278702009-09-02 23:07:23 -07002529 if (mCurToken == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002530 Slog.w(TAG, "Service connected without a token!");
Yohei Yukawad4d895e2018-09-24 16:01:20 -07002531 unbindCurrentMethodLocked();
Dianne Hackborncc278702009-09-02 23:07:23 -07002532 return;
2533 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07002534 if (DEBUG) Slog.v(TAG, "Initiating attach with token: " + mCurToken);
lumark90120a82018-08-15 00:33:03 +08002535 // Dispatch display id for InputMethodService to update context display.
2536 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOO(
2537 MSG_INITIALIZE_IME, mCurTokenDisplayId, mCurMethod, mCurToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002539 clearClientSessionLocked(mCurClient);
2540 requestClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
2542 }
2543 }
2544 }
2545
Jeff Brownc28867a2013-03-26 15:42:39 -07002546 void onSessionCreated(IInputMethod method, IInputMethodSession session,
2547 InputChannel channel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 synchronized (mMethodMap) {
Yohei Yukawa32807ad2020-03-04 12:27:29 -08002549 if (mUserSwitchHandlerTask != null) {
2550 // We have a pending user-switching task so it's better to just ignore this session.
2551 channel.dispose();
2552 return;
2553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 if (mCurMethod != null && method != null
2555 && mCurMethod.asBinder() == method.asBinder()) {
2556 if (mCurClient != null) {
Jeff Brownc28867a2013-03-26 15:42:39 -07002557 clearClientSessionLocked(mCurClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 mCurClient.curSession = new SessionState(mCurClient,
Jeff Brownc28867a2013-03-26 15:42:39 -07002559 method, session, channel);
Yohei Yukawa87ca8402017-02-07 00:13:14 -08002560 InputBindResult res = attachNewInputLocked(
Yohei Yukawa42194222018-10-21 20:14:40 -07002561 StartInputReason.SESSION_CREATED_BY_IME, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 if (res.method != null) {
2563 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
Yohei Yukawa33e81792015-11-17 21:14:42 -08002564 MSG_BIND_CLIENT, mCurClient.client, res));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
Jeff Brownc28867a2013-03-26 15:42:39 -07002566 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
2568 }
2569 }
Jeff Brownc28867a2013-03-26 15:42:39 -07002570
2571 // Session abandoned. Close its associated input channel.
2572 channel.dispose();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002574
Yohei Yukawad4d895e2018-09-24 16:01:20 -07002575 void unbindCurrentMethodLocked() {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07002576 if (mVisibleBound) {
2577 mContext.unbindService(mVisibleConnection);
2578 mVisibleBound = false;
2579 }
2580
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002581 if (mHaveConnection) {
2582 mContext.unbindService(this);
2583 mHaveConnection = false;
2584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002585
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002586 if (mCurToken != null) {
2587 try {
lumark90120a82018-08-15 00:33:03 +08002588 if (DEBUG) {
2589 Slog.v(TAG, "Removing window token: " + mCurToken + " for display: "
2590 + mCurTokenDisplayId);
2591 }
lumark90120a82018-08-15 00:33:03 +08002592 mIWindowManager.removeWindowToken(mCurToken, mCurTokenDisplayId);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002593 } catch (RemoteException e) {
2594 }
lumark7570cac2019-03-07 22:14:38 +08002595 // Set IME window status as invisible when unbind current method.
2596 mImeWindowVis = 0;
2597 mBackDisposition = InputMethodService.BACK_DISPOSITION_DEFAULT;
2598 updateSystemUiLocked(mImeWindowVis, mBackDisposition);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002599 mCurToken = null;
lumark90120a82018-08-15 00:33:03 +08002600 mCurTokenDisplayId = INVALID_DISPLAY;
lpeter133fce02020-03-05 20:32:16 +08002601 mCurHostInputToken = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002602 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002603
The Android Open Source Project10592532009-03-18 17:39:46 -07002604 mCurId = null;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002605 clearCurMethodLocked();
Yohei Yukawabc7b5262015-11-17 17:38:41 -08002606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002607
Yohei Yukawa4afd9332018-10-21 10:43:53 -07002608 void resetCurrentMethodAndClient(@UnbindReason int unbindClientReason) {
Yohei Yukawabc7b5262015-11-17 17:38:41 -08002609 mCurMethodId = null;
Yohei Yukawad4d895e2018-09-24 16:01:20 -07002610 unbindCurrentMethodLocked();
Yohei Yukawa33e81792015-11-17 21:14:42 -08002611 unbindCurrentClientLocked(unbindClientReason);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002612 }
Jeff Brownc28867a2013-03-26 15:42:39 -07002613
2614 void requestClientSessionLocked(ClientState cs) {
2615 if (!cs.sessionRequested) {
2616 if (DEBUG) Slog.v(TAG, "Creating new session for client " + cs);
2617 InputChannel[] channels = InputChannel.openInputChannelPair(cs.toString());
2618 cs.sessionRequested = true;
2619 executeOrSendMessage(mCurMethod, mCaller.obtainMessageOOO(
2620 MSG_CREATE_SESSION, mCurMethod, channels[1],
2621 new MethodCallback(this, mCurMethod, channels[0])));
2622 }
2623 }
2624
2625 void clearClientSessionLocked(ClientState cs) {
2626 finishSessionLocked(cs.curSession);
2627 cs.curSession = null;
2628 cs.sessionRequested = false;
2629 }
2630
2631 private void finishSessionLocked(SessionState sessionState) {
2632 if (sessionState != null) {
2633 if (sessionState.session != null) {
2634 try {
2635 sessionState.session.finishSession();
2636 } catch (RemoteException e) {
2637 Slog.w(TAG, "Session failed to close due to remote exception", e);
Yohei Yukawa849443c2019-01-21 09:02:25 -08002638 updateSystemUiLocked(0 /* vis */, mBackDisposition);
Jeff Brownc28867a2013-03-26 15:42:39 -07002639 }
2640 sessionState.session = null;
2641 }
2642 if (sessionState.channel != null) {
2643 sessionState.channel.dispose();
2644 sessionState.channel = null;
Devin Taylor0c33ed22010-02-23 13:26:46 -06002645 }
2646 }
2647 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002648
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002649 void clearCurMethodLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 if (mCurMethod != null) {
Yohei Yukawaac9311e2018-11-20 19:25:23 -08002651 final int numClients = mClients.size();
2652 for (int i = 0; i < numClients; ++i) {
2653 clearClientSessionLocked(mClients.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
Devin Taylor0c33ed22010-02-23 13:26:46 -06002655
Jeff Brownc28867a2013-03-26 15:42:39 -07002656 finishSessionLocked(mEnabledSession);
Devin Taylor0c33ed22010-02-23 13:26:46 -06002657 mEnabledSession = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 mCurMethod = null;
2659 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002660 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002661 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002662 }
Yohei Yukawa2bc66172017-02-08 11:13:25 -08002663 mInFullscreenMode = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002665
satoke7c6998e2011-06-03 17:57:59 +09002666 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 public void onServiceDisconnected(ComponentName name) {
Yohei Yukawa817d5f72017-01-04 20:15:02 -08002668 // Note that mContext.unbindService(this) does not trigger this. Hence if we are here the
2669 // disconnection is not intended by IMMS (e.g. triggered because the current IMS crashed),
2670 // which is irregular but can eventually happen for everyone just by continuing using the
2671 // device. Thus it is important to make sure that all the internal states are properly
2672 // refreshed when this method is called back. Running
2673 // adb install -r <APK that implements the current IME>
2674 // would be a good way to trigger such a situation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 synchronized (mMethodMap) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002676 if (DEBUG) Slog.v(TAG, "Service disconnected: " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 + " mCurIntent=" + mCurIntent);
2678 if (mCurMethod != null && mCurIntent != null
2679 && name.equals(mCurIntent.getComponent())) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002680 clearCurMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 // We consider this to be a new bind attempt, since the system
2682 // should now try to restart the service for us.
2683 mLastBindTime = SystemClock.uptimeMillis();
2684 mShowRequested = mInputShown;
2685 mInputShown = false;
Yohei Yukawab7526452018-10-21 20:15:17 -07002686 unbindCurrentClientLocked(UnbindReason.DISCONNECT_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
2688 }
2689 }
2690
Yohei Yukawaeec552e2018-09-09 20:48:41 -07002691 @BinderThread
Yohei Yukawa41b094f2018-09-09 23:58:45 -07002692 private void updateStatusIcon(@NonNull IBinder token, String packageName,
2693 @DrawableRes int iconId) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002694 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08002695 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002696 return;
2697 }
2698 final long ident = Binder.clearCallingIdentity();
2699 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 if (iconId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002701 if (DEBUG) Slog.d(TAG, "hide the small icon for the input method");
Dianne Hackborn661cd522011-08-22 00:26:20 -07002702 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002703 mStatusBar.setIconVisibility(mSlotIme, false);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 } else if (packageName != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002706 if (DEBUG) Slog.d(TAG, "show a small icon for the input method");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002707 CharSequence contentDescription = null;
2708 try {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002709 // Use PackageManager to load label
2710 final PackageManager packageManager = mContext.getPackageManager();
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002711 contentDescription = packageManager.getApplicationLabel(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002712 mIPackageManager.getApplicationInfo(packageName, 0,
2713 mSettings.getCurrentUserId()));
2714 } catch (RemoteException e) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -07002715 /* ignore */
2716 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07002717 if (mStatusBar != null) {
Jason Monk3e189872016-01-12 09:10:34 -05002718 mStatusBar.setIcon(mSlotIme, packageName, iconId, 0,
Dianne Hackborn661cd522011-08-22 00:26:20 -07002719 contentDescription != null
2720 ? contentDescription.toString() : null);
Jason Monk3e189872016-01-12 09:10:34 -05002721 mStatusBar.setIconVisibility(mSlotIme, true);
Dianne Hackborn661cd522011-08-22 00:26:20 -07002722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
Yohei Yukawa59377ca2017-01-31 21:32:26 -08002724 } finally {
2725 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
2728 }
2729
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002730 private boolean shouldShowImeSwitcherLocked(int visibility) {
satok7cfc0ed2011-06-20 21:29:36 +09002731 if (!mShowOngoingImeSwitcherForPhones) return false;
Jason Monk807ef762014-05-08 15:47:46 -04002732 if (mSwitchingDialog != null) return false;
Yohei Yukawad2bc3092017-07-31 15:37:14 -07002733 if (mWindowManagerInternal.isKeyguardShowingAndNotOccluded()
2734 && mKeyguardManager != null && mKeyguardManager.isKeyguardSecure()) return false;
Tarandeep Singheadb1392018-11-09 18:15:57 +01002735 if ((visibility & InputMethodService.IME_ACTIVE) == 0
2736 || (visibility & InputMethodService.IME_INVISIBLE) != 0) {
2737 return false;
2738 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002739 if (mWindowManagerInternal.isHardKeyboardAvailable()) {
Yohei Yukawaaf023f22019-06-21 16:38:22 -07002740 // When physical keyboard is attached, we show the ime switcher (or notification if
2741 // NavBar is not available) because SHOW_IME_WITH_HARD_KEYBOARD settings currently
2742 // exists in the IME switcher dialog. Might be OK to remove this condition once
2743 // SHOW_IME_WITH_HARD_KEYBOARD settings finds a good place to live.
2744 return true;
Yohei Yukawa89398382016-03-29 11:37:04 -07002745 } else if ((visibility & InputMethodService.IME_VISIBLE) == 0) {
2746 return false;
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002747 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002748
2749 List<InputMethodInfo> imis = mSettings.getEnabledInputMethodListLocked();
2750 final int N = imis.size();
2751 if (N > 2) return true;
2752 if (N < 1) return false;
2753 int nonAuxCount = 0;
2754 int auxCount = 0;
2755 InputMethodSubtype nonAuxSubtype = null;
2756 InputMethodSubtype auxSubtype = null;
2757 for(int i = 0; i < N; ++i) {
2758 final InputMethodInfo imi = imis.get(i);
2759 final List<InputMethodSubtype> subtypes =
2760 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
2761 final int subtypeCount = subtypes.size();
2762 if (subtypeCount == 0) {
2763 ++nonAuxCount;
2764 } else {
2765 for (int j = 0; j < subtypeCount; ++j) {
2766 final InputMethodSubtype subtype = subtypes.get(j);
2767 if (!subtype.isAuxiliary()) {
2768 ++nonAuxCount;
2769 nonAuxSubtype = subtype;
2770 } else {
2771 ++auxCount;
2772 auxSubtype = subtype;
satok7cfc0ed2011-06-20 21:29:36 +09002773 }
2774 }
satok7cfc0ed2011-06-20 21:29:36 +09002775 }
2776 }
Yohei Yukawac6c7cd22015-05-12 15:00:33 -07002777 if (nonAuxCount > 1 || auxCount > 1) {
2778 return true;
2779 } else if (nonAuxCount == 1 && auxCount == 1) {
2780 if (nonAuxSubtype != null && auxSubtype != null
2781 && (nonAuxSubtype.getLocale().equals(auxSubtype.getLocale())
2782 || auxSubtype.overridesImplicitlyEnabledSubtype()
2783 || nonAuxSubtype.overridesImplicitlyEnabledSubtype())
2784 && nonAuxSubtype.containsExtraValueKey(TAG_TRY_SUPPRESSING_IME_SWITCHER)) {
2785 return false;
2786 }
2787 return true;
2788 }
2789 return false;
satok7cfc0ed2011-06-20 21:29:36 +09002790 }
2791
John Spurlocke0980502013-10-25 11:59:29 -04002792 private boolean isKeyguardLocked() {
2793 return mKeyguardManager != null && mKeyguardManager.isKeyguardLocked();
2794 }
2795
Yohei Yukawad6475a62017-04-17 10:35:27 -07002796 @BinderThread
satokdbf29502011-08-25 15:28:23 +09002797 @SuppressWarnings("deprecation")
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08002798 private void setImeWindowStatus(@NonNull IBinder token, int vis, int backDisposition) {
lumark7570cac2019-03-07 22:14:38 +08002799 final int topFocusedDisplayId = mWindowManagerInternal.getTopFocusedDisplayId();
2800
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002801 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08002802 if (!calledWithValidTokenLocked(token)) {
2803 return;
2804 }
lumark7570cac2019-03-07 22:14:38 +08002805 // Skip update IME status when current token display is not same as focused display.
2806 // Note that we still need to update IME status when focusing external display
2807 // that does not support system decoration and fallback to show IME on default
2808 // display since it is intentional behavior.
2809 if (mCurTokenDisplayId != topFocusedDisplayId
2810 && mCurTokenDisplayId != FALLBACK_DISPLAY_ID) {
2811 return;
2812 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002813 mImeWindowVis = vis;
2814 mBackDisposition = backDisposition;
Yohei Yukawa849443c2019-01-21 09:02:25 -08002815 updateSystemUiLocked(vis, backDisposition);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002816 }
Yohei Yukawad6475a62017-04-17 10:35:27 -07002817
2818 final boolean dismissImeOnBackKeyPressed;
2819 switch (backDisposition) {
2820 case InputMethodService.BACK_DISPOSITION_WILL_DISMISS:
2821 dismissImeOnBackKeyPressed = true;
2822 break;
2823 case InputMethodService.BACK_DISPOSITION_WILL_NOT_DISMISS:
2824 dismissImeOnBackKeyPressed = false;
2825 break;
2826 default:
2827 case InputMethodService.BACK_DISPOSITION_DEFAULT:
2828 dismissImeOnBackKeyPressed = ((vis & InputMethodService.IME_VISIBLE) != 0);
2829 break;
2830 }
Yohei Yukawaee2a7ed2017-02-15 21:38:57 -08002831 mWindowManagerInternal.updateInputMethodWindowStatus(token,
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -07002832 (vis & InputMethodService.IME_VISIBLE) != 0, dismissImeOnBackKeyPressed);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002833 }
2834
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -07002835 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08002836 private void reportStartInput(@NonNull IBinder token, IBinder startInputToken) {
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -07002837 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08002838 if (!calledWithValidTokenLocked(token)) {
2839 return;
2840 }
Yohei Yukawa99e1f6e2018-06-22 17:33:19 -07002841 final IBinder targetWindow = mImeTargetWindowMap.get(startInputToken);
2842 if (targetWindow != null && mLastImeTargetWindow != targetWindow) {
2843 mWindowManagerInternal.updateInputMethodTargetWindow(token, targetWindow);
2844 }
2845 mLastImeTargetWindow = targetWindow;
2846 }
2847 }
2848
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002849 // Caution! This method is called in this class. Handle multi-user carefully
Yohei Yukawa849443c2019-01-21 09:02:25 -08002850 private void updateSystemUiLocked(int vis, int backDisposition) {
2851 if (mCurToken == null) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002852 return;
2853 }
Tarandeep Singheadb1392018-11-09 18:15:57 +01002854 if (DEBUG) {
2855 Slog.d(TAG, "IME window vis: " + vis
2856 + " active: " + (vis & InputMethodService.IME_ACTIVE)
lumark7570cac2019-03-07 22:14:38 +08002857 + " inv: " + (vis & InputMethodService.IME_INVISIBLE)
2858 + " displayId: " + mCurTokenDisplayId);
Tarandeep Singheadb1392018-11-09 18:15:57 +01002859 }
2860
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002861 // TODO: Move this clearing calling identity block to setImeWindowStatus after making sure
2862 // all updateSystemUi happens on system previlege.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002863 final long ident = Binder.clearCallingIdentity();
satok06487a52010-10-29 11:37:18 +09002864 try {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002865 // apply policy for binder calls
2866 if (vis != 0 && isKeyguardLocked() && !mCurClientInKeyguard) {
2867 vis = 0;
satok06487a52010-10-29 11:37:18 +09002868 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002869 // mImeWindowVis should be updated before calling shouldShowImeSwitcherLocked().
2870 final boolean needsToShowImeSwitcher = shouldShowImeSwitcherLocked(vis);
2871 if (mStatusBar != null) {
lumark7570cac2019-03-07 22:14:38 +08002872 mStatusBar.setImeWindowStatus(mCurTokenDisplayId, mCurToken, vis, backDisposition,
Tarandeep Singh07b318b2019-07-17 11:12:04 -07002873 needsToShowImeSwitcher, false /*isMultiClientImeEnabled*/);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002874 }
2875 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
2876 if (imi != null && needsToShowImeSwitcher) {
2877 // Used to load label
2878 final CharSequence title = mRes.getText(
2879 com.android.internal.R.string.select_input_method);
2880 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
2881 mContext, imi, mCurrentSubtype);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04002882 mImeSwitcherNotification.setContentTitle(title)
2883 .setContentText(summary)
2884 .setContentIntent(mImeSwitchPendingIntent);
Seigo Nonaka7309b122015-08-17 18:34:13 -07002885 try {
Charles Chenea6e7f02018-11-19 21:37:45 +08002886 // TODO(b/120076400): Figure out what is the best behavior
Seigo Nonaka7309b122015-08-17 18:34:13 -07002887 if ((mNotificationManager != null)
Charles Chenea6e7f02018-11-19 21:37:45 +08002888 && !mIWindowManager.hasNavigationBar(DEFAULT_DISPLAY)) {
Seigo Nonaka7309b122015-08-17 18:34:13 -07002889 if (DEBUG) {
2890 Slog.d(TAG, "--- show notification: label = " + summary);
2891 }
2892 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002893 SystemMessage.NOTE_SELECT_INPUT_METHOD,
Seigo Nonaka7309b122015-08-17 18:34:13 -07002894 mImeSwitcherNotification.build(), UserHandle.ALL);
2895 mNotificationShown = true;
Dianne Hackborn661cd522011-08-22 00:26:20 -07002896 }
Seigo Nonaka7309b122015-08-17 18:34:13 -07002897 } catch (RemoteException e) {
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002898 }
2899 } else {
2900 if (mNotificationShown && mNotificationManager != null) {
2901 if (DEBUG) {
2902 Slog.d(TAG, "--- hide notification");
satok7cfc0ed2011-06-20 21:29:36 +09002903 }
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002904 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04002905 SystemMessage.NOTE_SELECT_INPUT_METHOD, UserHandle.ALL);
Seigo Nonakad9eb9112015-05-26 20:54:43 +09002906 mNotificationShown = false;
satok7cfc0ed2011-06-20 21:29:36 +09002907 }
satok06487a52010-10-29 11:37:18 +09002908 }
2909 } finally {
2910 Binder.restoreCallingIdentity(ident);
2911 }
2912 }
2913
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002914 void updateFromSettingsLocked(boolean enabledMayChange) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07002915 updateInputMethodsFromSettingsLocked(enabledMayChange);
2916 updateKeyboardFromSettingsLocked();
2917 }
2918
2919 void updateInputMethodsFromSettingsLocked(boolean enabledMayChange) {
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002920 if (enabledMayChange) {
2921 List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
2922 for (int i=0; i<enabled.size(); i++) {
2923 // We allow the user to select "disabled until used" apps, so if they
2924 // are enabling one of those here we now need to make it enabled.
2925 InputMethodInfo imm = enabled.get(i);
2926 try {
2927 ApplicationInfo ai = mIPackageManager.getApplicationInfo(imm.getPackageName(),
2928 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
2929 mSettings.getCurrentUserId());
Satoshi Kataoka7987a312013-04-17 18:59:33 +09002930 if (ai != null && ai.enabledSetting
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002931 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
Satoshi Kataokaed1cdb22013-04-17 16:41:58 +09002932 if (DEBUG) {
2933 Slog.d(TAG, "Update state(" + imm.getId()
2934 + "): DISABLED_UNTIL_USED -> DEFAULT");
2935 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002936 mIPackageManager.setApplicationEnabledSetting(imm.getPackageName(),
2937 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002938 PackageManager.DONT_KILL_APP, mSettings.getCurrentUserId(),
2939 mContext.getBasePackageName());
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08002940 }
2941 } catch (RemoteException e) {
2942 }
2943 }
2944 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002945 // We are assuming that whoever is changing DEFAULT_INPUT_METHOD and
2946 // ENABLED_INPUT_METHODS is taking care of keeping them correctly in
2947 // sync, so we will never have a DEFAULT_INPUT_METHOD that is not
2948 // enabled.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002949 String id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002950 // There is no input method selected, try to choose new applicable input method.
2951 if (TextUtils.isEmpty(id) && chooseNewDefaultIMELocked()) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09002952 id = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09002953 }
2954 if (!TextUtils.isEmpty(id)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09002956 setInputMethodLocked(id, mSettings.getSelectedInputMethodSubtypeId(id));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 } catch (IllegalArgumentException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002958 Slog.w(TAG, "Unknown input method from prefs: " + id, e);
Yohei Yukawab7526452018-10-21 20:15:17 -07002959 resetCurrentMethodAndClient(UnbindReason.SWITCH_IME_FAILED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07002961 } else {
2962 // There is no longer an input method set, so stop any current one.
Yohei Yukawab7526452018-10-21 20:15:17 -07002963 resetCurrentMethodAndClient(UnbindReason.NO_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09002965 // Here is not the perfect place to reset the switching controller. Ideally
2966 // mSwitchingController and mSettings should be able to share the same state.
2967 // TODO: Make sure that mSwitchingController and mSettings are sharing the
2968 // the same enabled IMEs list.
2969 mSwitchingController.resetCircularListLocked(mContext);
Michael Wright7b5a96b2014-08-09 19:28:42 -07002970
2971 }
2972
2973 public void updateKeyboardFromSettingsLocked() {
2974 mShowImeWithHardKeyboard = mSettings.isShowImeWithHardKeyboardEnabled();
2975 if (mSwitchingDialog != null
2976 && mSwitchingDialogTitleView != null
2977 && mSwitchingDialog.isShowing()) {
2978 final Switch hardKeySwitch = (Switch)mSwitchingDialogTitleView.findViewById(
2979 com.android.internal.R.id.hard_keyboard_switch);
2980 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
2981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002983
satokab751aa2010-09-14 19:17:36 +09002984 /* package */ void setInputMethodLocked(String id, int subtypeId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 InputMethodInfo info = mMethodMap.get(id);
2986 if (info == null) {
satok913a8922010-08-26 21:53:41 +09002987 throw new IllegalArgumentException("Unknown id: " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002989
satokd81e9502012-05-21 12:58:45 +09002990 // See if we need to notify a subtype change within the same IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 if (id.equals(mCurMethodId)) {
satokd81e9502012-05-21 12:58:45 +09002992 final int subtypeCount = info.getSubtypeCount();
2993 if (subtypeCount <= 0) {
2994 return;
satokcd7cd292010-11-20 15:46:23 +09002995 }
satokd81e9502012-05-21 12:58:45 +09002996 final InputMethodSubtype oldSubtype = mCurrentSubtype;
2997 final InputMethodSubtype newSubtype;
2998 if (subtypeId >= 0 && subtypeId < subtypeCount) {
2999 newSubtype = info.getSubtypeAt(subtypeId);
3000 } else {
3001 // If subtype is null, try to find the most applicable one from
3002 // getCurrentInputMethodSubtype.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003003 newSubtype = getCurrentInputMethodSubtypeLocked();
satokd81e9502012-05-21 12:58:45 +09003004 }
3005 if (newSubtype == null || oldSubtype == null) {
3006 Slog.w(TAG, "Illegal subtype state: old subtype = " + oldSubtype
3007 + ", new subtype = " + newSubtype);
3008 return;
3009 }
3010 if (newSubtype != oldSubtype) {
3011 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, true);
3012 if (mCurMethod != null) {
3013 try {
Yohei Yukawa849443c2019-01-21 09:02:25 -08003014 updateSystemUiLocked(mImeWindowVis, mBackDisposition);
satokd81e9502012-05-21 12:58:45 +09003015 mCurMethod.changeInputMethodSubtype(newSubtype);
3016 } catch (RemoteException e) {
3017 Slog.w(TAG, "Failed to call changeInputMethodSubtype");
satokab751aa2010-09-14 19:17:36 +09003018 }
3019 }
3020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 return;
3022 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003023
satokd81e9502012-05-21 12:58:45 +09003024 // Changing to a different IME.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 final long ident = Binder.clearCallingIdentity();
3026 try {
satokab751aa2010-09-14 19:17:36 +09003027 // Set a subtype to this input method.
3028 // subtypeId the name of a subtype which will be set.
satok723a27e2010-11-11 14:58:11 +09003029 setSelectedInputMethodAndSubtypeLocked(info, subtypeId, false);
3030 // mCurMethodId should be updated after setSelectedInputMethodAndSubtypeLocked()
3031 // because mCurMethodId is stored as a history in
3032 // setSelectedInputMethodAndSubtypeLocked().
3033 mCurMethodId = id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07003035 if (LocalServices.getService(ActivityManagerInternal.class).isSystemReady()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 Intent intent = new Intent(Intent.ACTION_INPUT_METHOD_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08003037 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 intent.putExtra("input_method_id", id);
Amith Yamasanicd757062012-10-19 18:23:52 -07003039 mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 }
Yohei Yukawab7526452018-10-21 20:15:17 -07003041 unbindCurrentClientLocked(UnbindReason.SWITCH_IME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 } finally {
3043 Binder.restoreCallingIdentity(ident);
3044 }
3045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003046
satok42c5a162011-05-26 16:46:14 +09003047 @Override
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08003048 public boolean showSoftInput(IInputMethodClient client, IBinder windowToken, int flags,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003049 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003050 int uid = Binder.getCallingUid();
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003051 synchronized (mMethodMap) {
Yohei Yukawa46d74762019-01-22 10:17:22 -08003052 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003053 return false;
3054 }
3055 final long ident = Binder.clearCallingIdentity();
3056 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (mCurClient == null || client == null
3058 || mCurClient.client.asBinder() != client.asBinder()) {
Yohei Yukawacf93f9a2018-08-30 15:58:47 -07003059 // We need to check if this is the current client with
3060 // focus in the window manager, to allow this call to
3061 // be made before input is started in it.
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003062 final ClientState cs = mClients.get(client.asBinder());
3063 if (cs == null) {
3064 throw new IllegalArgumentException("unknown client " + client.asBinder());
3065 }
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08003066 if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid,
3067 cs.selfReportedDisplayId)) {
Yohei Yukawacf93f9a2018-08-30 15:58:47 -07003068 Slog.w(TAG, "Ignoring showSoftInput of uid " + uid + ": " + client);
The Android Open Source Project4df24232009-03-05 14:34:35 -08003069 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 }
3071 }
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003072 if (DEBUG) Slog.v(TAG, "Client requesting input be shown");
lumarkd85e1582019-12-29 20:20:41 +08003073 return showCurrentInputLocked(windowToken, flags, resultReceiver,
3074 SoftInputShowHideReason.SHOW_SOFT_INPUT);
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003075 } finally {
3076 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003080
Andreas Gamped6d42062018-07-20 13:08:21 -07003081 @GuardedBy("mMethodMap")
lumarkd85e1582019-12-29 20:20:41 +08003082 boolean showCurrentInputLocked(IBinder windowToken, int flags, ResultReceiver resultReceiver,
3083 @SoftInputShowHideReason int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 mShowRequested = true;
Anna Galusza9b278112016-01-04 11:37:37 -08003085 if (mAccessibilityRequestingNoSoftKeyboard) {
3086 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
Anna Galusza9b278112016-01-04 11:37:37 -08003088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 if ((flags&InputMethodManager.SHOW_FORCED) != 0) {
3090 mShowExplicitlyRequested = true;
3091 mShowForced = true;
Anna Galusza9b278112016-01-04 11:37:37 -08003092 } else if ((flags&InputMethodManager.SHOW_IMPLICIT) == 0) {
3093 mShowExplicitlyRequested = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003095
Dianne Hackborncc278702009-09-02 23:07:23 -07003096 if (!mSystemReady) {
3097 return false;
3098 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003099
The Android Open Source Project4df24232009-03-05 14:34:35 -08003100 boolean res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 if (mCurMethod != null) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003102 if (DEBUG) Slog.d(TAG, "showCurrentInputLocked: mCurToken=" + mCurToken);
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08003103 // create a dummy token for IMS so that IMS cannot inject windows into client app.
3104 Binder showInputToken = new Binder();
3105 mShowRequestWindowMap.put(showInputToken, windowToken);
lumarkd85e1582019-12-29 20:20:41 +08003106 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIIOOO(
3107 MSG_SHOW_SOFT_INPUT, getImeShowFlags(), reason, mCurMethod, resultReceiver,
3108 showInputToken));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 mInputShown = true;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07003110 if (mHaveConnection && !mVisibleBound) {
Yohei Yukawaf80087c2018-05-21 09:47:53 -07003111 bindCurrentInputMethodServiceLocked(
Yohei Yukawaa67a4592017-03-30 15:57:02 -07003112 mCurIntent, mVisibleConnection, IME_VISIBLE_BIND_FLAGS);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07003113 mVisibleBound = true;
3114 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003115 res = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 } else if (mHaveConnection && SystemClock.uptimeMillis()
satok59b424c2011-09-30 17:21:46 +09003117 >= (mLastBindTime+TIME_TO_RECONNECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 // The client has asked to have the input method shown, but
3119 // we have been sitting here too long with a connection to the
3120 // service and no interface received, so let's disconnect/connect
3121 // to try to prod things along.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003122 EventLog.writeEvent(EventLogTags.IMF_FORCE_RECONNECT_IME, mCurMethodId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 SystemClock.uptimeMillis()-mLastBindTime,1);
satok59b424c2011-09-30 17:21:46 +09003124 Slog.w(TAG, "Force disconnect/connect to the IME in showCurrentInputLocked()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 mContext.unbindService(this);
Yohei Yukawaf80087c2018-05-21 09:47:53 -07003126 bindCurrentInputMethodServiceLocked(mCurIntent, this, IME_CONNECTION_BIND_FLAGS);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003127 } else {
3128 if (DEBUG) {
3129 Slog.d(TAG, "Can't show input: connection = " + mHaveConnection + ", time = "
3130 + ((mLastBindTime+TIME_TO_RECONNECT) - SystemClock.uptimeMillis()));
3131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003133
The Android Open Source Project4df24232009-03-05 14:34:35 -08003134 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003136
satok42c5a162011-05-26 16:46:14 +09003137 @Override
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003138 public boolean hideSoftInput(IInputMethodClient client, IBinder windowToken, int flags,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003139 ResultReceiver resultReceiver) {
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003140 int uid = Binder.getCallingUid();
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003141 synchronized (mMethodMap) {
Yohei Yukawa46d74762019-01-22 10:17:22 -08003142 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003143 return false;
3144 }
3145 final long ident = Binder.clearCallingIdentity();
3146 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 if (mCurClient == null || client == null
3148 || mCurClient.client.asBinder() != client.asBinder()) {
Yohei Yukawacf93f9a2018-08-30 15:58:47 -07003149 // We need to check if this is the current client with
3150 // focus in the window manager, to allow this call to
3151 // be made before input is started in it.
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003152 final ClientState cs = mClients.get(client.asBinder());
3153 if (cs == null) {
3154 throw new IllegalArgumentException("unknown client " + client.asBinder());
3155 }
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08003156 if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid,
3157 cs.selfReportedDisplayId)) {
Yohei Yukawacf93f9a2018-08-30 15:58:47 -07003158 if (DEBUG) {
3159 Slog.w(TAG, "Ignoring hideSoftInput of uid " + uid + ": " + client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08003161 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 }
3163 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003164
Joe Onorato8a9b2202010-02-26 18:56:32 -08003165 if (DEBUG) Slog.v(TAG, "Client requesting input be hidden");
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003166 return hideCurrentInputLocked(windowToken, flags, resultReceiver,
lumarkd85e1582019-12-29 20:20:41 +08003167 SoftInputShowHideReason.HIDE_SOFT_INPUT);
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003168 } finally {
3169 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 }
3172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003173
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003174 boolean hideCurrentInputLocked(IBinder windowToken, int flags, ResultReceiver resultReceiver,
lumarkd85e1582019-12-29 20:20:41 +08003175 @SoftInputShowHideReason int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 if ((flags&InputMethodManager.HIDE_IMPLICIT_ONLY) != 0
3177 && (mShowExplicitlyRequested || mShowForced)) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003178 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 -08003179 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
3181 if (mShowForced && (flags&InputMethodManager.HIDE_NOT_ALWAYS) != 0) {
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07003182 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 -08003183 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
Seigo Nonakaec928652015-06-10 15:31:20 +09003185
3186 // There is a chance that IMM#hideSoftInput() is called in a transient state where
3187 // IMMS#InputShown is already updated to be true whereas IMMS#mImeWindowVis is still waiting
3188 // to be updated with the new value sent from IME process. Even in such a transient state
3189 // historically we have accepted an incoming call of IMM#hideSoftInput() from the
3190 // application process as a valid request, and have even promised such a behavior with CTS
3191 // since Android Eclair. That's why we need to accept IMM#hideSoftInput() even when only
3192 // IMMS#InputShown indicates that the software keyboard is shown.
3193 // TODO: Clean up, IMMS#mInputShown, IMMS#mImeWindowVis and mShowRequested.
3194 final boolean shouldHideSoftInput = (mCurMethod != null) && (mInputShown ||
3195 (mImeWindowVis & InputMethodService.IME_ACTIVE) != 0);
The Android Open Source Project4df24232009-03-05 14:34:35 -08003196 boolean res;
Seigo Nonakaec928652015-06-10 15:31:20 +09003197 if (shouldHideSoftInput) {
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003198 final Binder hideInputToken = new Binder();
3199 mHideRequestWindowMap.put(hideInputToken, windowToken);
Seigo Nonakaec928652015-06-10 15:31:20 +09003200 // The IME will report its visible state again after the following message finally
3201 // delivered to the IME process as an IPC. Hence the inconsistency between
3202 // IMMS#mInputShown and IMMS#mImeWindowVis should be resolved spontaneously in
3203 // the final state.
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003204 executeOrSendMessage(mCurMethod, mCaller.obtainMessageIOOO(MSG_HIDE_SOFT_INPUT,
3205 reason, mCurMethod, resultReceiver, hideInputToken));
The Android Open Source Project4df24232009-03-05 14:34:35 -08003206 res = true;
3207 } else {
3208 res = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07003210 if (mHaveConnection && mVisibleBound) {
3211 mContext.unbindService(mVisibleConnection);
3212 mVisibleBound = false;
3213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 mInputShown = false;
3215 mShowRequested = false;
3216 mShowExplicitlyRequested = false;
3217 mShowForced = false;
The Android Open Source Project4df24232009-03-05 14:34:35 -08003218 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003220
Yohei Yukawa2553e482017-12-15 15:47:33 -08003221 @NonNull
satok42c5a162011-05-26 16:46:14 +09003222 @Override
Yohei Yukawa05c25f82016-02-22 12:41:17 -08003223 public InputBindResult startInputOrWindowGainedFocus(
Yohei Yukawadc66e522018-10-21 10:43:14 -07003224 @StartInputReason int startInputReason, IInputMethodClient client, IBinder windowToken,
Yohei Yukawa35fa6d52018-10-31 11:33:32 -07003225 @StartInputFlags int startInputFlags, @SoftInputModeFlags int softInputMode,
3226 int windowFlags, @Nullable EditorInfo attribute, IInputContext inputContext,
Yohei Yukawadc66e522018-10-21 10:43:14 -07003227 @MissingMethodFlags int missingMethods, int unverifiedTargetSdkVersion) {
Yohei Yukawa80498d52018-06-21 16:24:36 -07003228 if (windowToken == null) {
3229 Slog.e(TAG, "windowToken cannot be null.");
3230 return InputBindResult.NULL;
Yohei Yukawa05c25f82016-02-22 12:41:17 -08003231 }
Yohei Yukawa0f5eade2019-01-18 08:48:07 -08003232 final int callingUserId = UserHandle.getCallingUserId();
3233 final int userId;
Yohei Yukawa716897c2019-01-22 00:00:53 -08003234 if (attribute != null && attribute.targetInputMethodUser != null
Yohei Yukawa0f5eade2019-01-18 08:48:07 -08003235 && attribute.targetInputMethodUser.getIdentifier() != callingUserId) {
3236 mContext.enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS_FULL,
Yohei Yukawa670abea2019-01-28 00:00:50 -08003237 "Using EditorInfo.targetInputMethodUser requires INTERACT_ACROSS_USERS_FULL.");
Yohei Yukawa0f5eade2019-01-18 08:48:07 -08003238 userId = attribute.targetInputMethodUser.getIdentifier();
3239 if (!mUserManagerInternal.isUserRunning(userId)) {
3240 // There is a chance that we hit here because of race condition. Let's just return
3241 // an error code instead of crashing the caller process, which at least has
3242 // INTERACT_ACROSS_USERS_FULL permission thus is likely to be an important process.
3243 Slog.e(TAG, "User #" + userId + " is not running.");
3244 return InputBindResult.INVALID_USER;
3245 }
3246 } else {
3247 userId = callingUserId;
3248 }
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003249 final InputBindResult result;
3250 synchronized (mMethodMap) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003251 final long ident = Binder.clearCallingIdentity();
3252 try {
3253 result = startInputOrWindowGainedFocusInternalLocked(startInputReason, client,
3254 windowToken, startInputFlags, softInputMode, windowFlags, attribute,
3255 inputContext, missingMethods, unverifiedTargetSdkVersion, userId);
3256 } finally {
3257 Binder.restoreCallingIdentity(ident);
3258 }
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003259 }
Yohei Yukawa2553e482017-12-15 15:47:33 -08003260 if (result == null) {
3261 // This must never happen, but just in case.
3262 Slog.wtf(TAG, "InputBindResult is @NonNull. startInputReason="
Yohei Yukawaa468d702018-10-21 11:42:34 -07003263 + InputMethodDebug.startInputReasonToString(startInputReason)
Yohei Yukawa2553e482017-12-15 15:47:33 -08003264 + " windowFlags=#" + Integer.toHexString(windowFlags)
3265 + " editorInfo=" + attribute);
3266 return InputBindResult.NULL;
3267 }
3268 return result;
Yohei Yukawa05c25f82016-02-22 12:41:17 -08003269 }
3270
Yohei Yukawa2553e482017-12-15 15:47:33 -08003271 @NonNull
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003272 private InputBindResult startInputOrWindowGainedFocusInternalLocked(
Yohei Yukawadc66e522018-10-21 10:43:14 -07003273 @StartInputReason int startInputReason, IInputMethodClient client,
Yohei Yukawa35fa6d52018-10-31 11:33:32 -07003274 @NonNull IBinder windowToken, @StartInputFlags int startInputFlags,
3275 @SoftInputModeFlags int softInputMode, int windowFlags, EditorInfo attribute,
3276 IInputContext inputContext, @MissingMethodFlags int missingMethods,
Yohei Yukawa4391c202019-01-28 00:49:10 -08003277 int unverifiedTargetSdkVersion, @UserIdInt int userId) {
Yohei Yukawa67464522019-01-28 00:50:09 -08003278 if (DEBUG) {
3279 Slog.v(TAG, "startInputOrWindowGainedFocusInternalLocked: reason="
3280 + InputMethodDebug.startInputReasonToString(startInputReason)
3281 + " client=" + client.asBinder()
3282 + " inputContext=" + inputContext
3283 + " missingMethods="
3284 + InputConnectionInspector.getMissingMethodFlagsAsString(missingMethods)
3285 + " attribute=" + attribute
3286 + " startInputFlags="
3287 + InputMethodDebug.startInputFlagsToString(startInputFlags)
3288 + " softInputMode=" + InputMethodDebug.softInputModeToString(softInputMode)
3289 + " windowFlags=#" + Integer.toHexString(windowFlags)
3290 + " unverifiedTargetSdkVersion=" + unverifiedTargetSdkVersion);
3291 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003292
Yohei Yukawa67464522019-01-28 00:50:09 -08003293 final int windowDisplayId = mWindowManagerInternal.getDisplayIdForWindow(windowToken);
3294
3295 final ClientState cs = mClients.get(client.asBinder());
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003296 if (cs == null) {
Yohei Yukawa67464522019-01-28 00:50:09 -08003297 throw new IllegalArgumentException("unknown client " + client.asBinder());
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003298 }
3299 if (cs.selfReportedDisplayId != windowDisplayId) {
3300 Slog.e(TAG, "startInputOrWindowGainedFocusInternal: display ID mismatch."
3301 + " from client:" + cs.selfReportedDisplayId
3302 + " from window:" + windowDisplayId);
3303 return InputBindResult.DISPLAY_ID_MISMATCH;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08003305
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003306 if (!mWindowManagerInternal.isInputMethodClientFocus(cs.uid, cs.pid,
3307 cs.selfReportedDisplayId)) {
3308 // Check with the window manager to make sure this client actually
3309 // has a window with focus. If not, reject. This is thread safe
3310 // because if the focus changes some time before or after, the
3311 // next client receiving focus that has any interest in input will
3312 // be calling through here after that change happens.
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003313 if (DEBUG) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003314 Slog.w(TAG, "Focus gain on non-focused client " + cs.client
3315 + " (uid=" + cs.uid + " pid=" + cs.pid + ")");
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003316 }
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003317 return InputBindResult.NOT_IME_TARGET_WINDOW;
3318 }
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003319
Yohei Yukawad277d692020-02-19 17:12:17 -08003320 if (mUserSwitchHandlerTask != null) {
3321 // There is already an on-going pending user switch task.
3322 final int nextUserId = mUserSwitchHandlerTask.mToUserId;
3323 if (userId == nextUserId) {
3324 scheduleSwitchUserTaskLocked(userId, cs.client);
3325 return InputBindResult.USER_SWITCHING;
3326 }
3327 for (int profileId : mUserManager.getProfileIdsWithDisabled(nextUserId)) {
3328 if (profileId == userId) {
3329 scheduleSwitchUserTaskLocked(userId, cs.client);
3330 return InputBindResult.USER_SWITCHING;
3331 }
3332 }
3333 return InputBindResult.INVALID_USER;
3334 }
3335
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003336 // cross-profile access is always allowed here to allow profile-switching.
3337 if (!mSettings.isCurrentProfile(userId)) {
3338 Slog.w(TAG, "A background user is requesting window. Hiding IME.");
3339 Slog.w(TAG, "If you need to impersonate a foreground user/profile from"
3340 + " a background user, use EditorInfo.targetInputMethodUser with"
3341 + " INTERACT_ACROSS_USERS_FULL permission.");
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003342 hideCurrentInputLocked(
3343 mCurFocusedWindow, 0, null, SoftInputShowHideReason.HIDE_INVALID_USER);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003344 return InputBindResult.INVALID_USER;
3345 }
3346
Tarandeep Singhcf5ff822019-07-08 14:06:27 -07003347 if (userId != mSettings.getCurrentUserId()) {
Yohei Yukawad277d692020-02-19 17:12:17 -08003348 scheduleSwitchUserTaskLocked(userId, cs.client);
3349 return InputBindResult.USER_SWITCHING;
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003350 }
Yohei Yukawad277d692020-02-19 17:12:17 -08003351
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003352 // Master feature flag that overrides other conditions and forces IME preRendering.
3353 if (DEBUG) {
3354 Slog.v(TAG, "IME PreRendering MASTER flag: "
Yohei Yukawa67464522019-01-28 00:50:09 -08003355 + DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.value() + ", LowRam: " + mIsLowRam);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003356 }
3357 // pre-rendering not supported on low-ram devices.
3358 cs.shouldPreRenderIme = DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.value() && !mIsLowRam;
3359
3360 if (mCurFocusedWindow == windowToken) {
3361 if (DEBUG) {
3362 Slog.w(TAG, "Window already focused, ignoring focus gain of: " + client
3363 + " attribute=" + attribute + ", token = " + windowToken);
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003364 }
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003365 if (attribute != null) {
3366 return startInputUncheckedLocked(cs, inputContext, missingMethods,
3367 attribute, startInputFlags, startInputReason);
3368 }
3369 return new InputBindResult(
3370 InputBindResult.ResultCode.SUCCESS_REPORT_WINDOW_FOCUS_ONLY,
Yohei Yukawab4f328a2019-05-02 08:41:27 -07003371 null, null, null, -1, null);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003372 }
3373 mCurFocusedWindow = windowToken;
3374 mCurFocusedWindowSoftInputMode = softInputMode;
3375 mCurFocusedWindowClient = cs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003377 // Should we auto-show the IME even if the caller has not
3378 // specified what should be done with it?
3379 // We only do this automatically if the window can resize
3380 // to accommodate the IME (so what the user sees will give
3381 // them good context without input information being obscured
3382 // by the IME) or if running on a large screen where there
3383 // is more room for the target window + IME.
3384 final boolean doAutoShow =
Yohei Yukawa6e875592019-01-28 00:49:30 -08003385 (softInputMode & LayoutParams.SOFT_INPUT_MASK_ADJUST)
3386 == LayoutParams.SOFT_INPUT_ADJUST_RESIZE
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003387 || mRes.getConfiguration().isLayoutSizeAtLeast(
3388 Configuration.SCREENLAYOUT_SIZE_LARGE);
Yohei Yukawa67464522019-01-28 00:50:09 -08003389 final boolean isTextEditor = (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003391 // We want to start input before showing the IME, but after closing
3392 // it. We want to do this after closing it to help the IME disappear
3393 // more quickly (not get stuck behind it initializing itself for the
3394 // new focused input, even if its window wants to hide the IME).
3395 boolean didStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396
Yohei Yukawa67464522019-01-28 00:50:09 -08003397 InputBindResult res = null;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003398 switch (softInputMode & LayoutParams.SOFT_INPUT_MASK_STATE) {
3399 case LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED:
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003400 if (!isTextEditor || !doAutoShow) {
Yohei Yukawa6e875592019-01-28 00:49:30 -08003401 if (LayoutParams.mayUseInputMethod(windowFlags)) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003402 // There is no focus view, and this window will
3403 // be behind any soft input window, so hide the
3404 // soft input window if it is shown.
3405 if (DEBUG) Slog.v(TAG, "Unspecified window will hide input");
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003406 hideCurrentInputLocked(
3407 mCurFocusedWindow, InputMethodManager.HIDE_NOT_ALWAYS, null,
lumarkd85e1582019-12-29 20:20:41 +08003408 SoftInputShowHideReason.HIDE_UNSPECIFIED_WINDOW);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003410 // If focused display changed, we should unbind current method
3411 // to make app window in previous display relayout after Ime
3412 // window token removed.
3413 // Note that we can trust client's display ID as long as it matches
3414 // to the display ID obtained from the window.
3415 if (cs.selfReportedDisplayId != mCurTokenDisplayId) {
3416 unbindCurrentMethodLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 }
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003418 }
Yohei Yukawa6e875592019-01-28 00:49:30 -08003419 } else if (isTextEditor && doAutoShow
3420 && (softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003421 // There is a focus view, and we are navigating forward
3422 // into the window, so show the input window for the user.
3423 // We only do this automatically if the window can resize
3424 // to accommodate the IME (so what the user sees will give
3425 // them good context without input information being obscured
3426 // by the IME) or if running on a large screen where there
3427 // is more room for the target window + IME.
3428 if (DEBUG) Slog.v(TAG, "Unspecified window will show input");
3429 if (attribute != null) {
3430 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
3431 attribute, startInputFlags, startInputReason);
3432 didStart = true;
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003433 }
lumarkd85e1582019-12-29 20:20:41 +08003434 showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null,
3435 SoftInputShowHideReason.SHOW_AUTO_EDITOR_FORWARD_NAV);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003436 }
3437 break;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003438 case LayoutParams.SOFT_INPUT_STATE_UNCHANGED:
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003439 // Do nothing.
3440 break;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003441 case LayoutParams.SOFT_INPUT_STATE_HIDDEN:
3442 if ((softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003443 if (DEBUG) Slog.v(TAG, "Window asks to hide input going forward");
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003444 hideCurrentInputLocked(mCurFocusedWindow, 0, null,
lumarkd85e1582019-12-29 20:20:41 +08003445 SoftInputShowHideReason.HIDE_STATE_HIDDEN_FORWARD_NAV);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003446 }
3447 break;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003448 case LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN:
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003449 if (DEBUG) Slog.v(TAG, "Window asks to hide input");
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003450 hideCurrentInputLocked(mCurFocusedWindow, 0, null,
lumarkd85e1582019-12-29 20:20:41 +08003451 SoftInputShowHideReason.HIDE_ALWAYS_HIDDEN_STATE);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003452 break;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003453 case LayoutParams.SOFT_INPUT_STATE_VISIBLE:
3454 if ((softInputMode & LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) != 0) {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003455 if (DEBUG) Slog.v(TAG, "Window asks to show input going forward");
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003456 if (InputMethodUtils.isSoftInputModeStateVisibleAllowed(
3457 unverifiedTargetSdkVersion, startInputFlags)) {
3458 if (attribute != null) {
Yohei Yukawa67464522019-01-28 00:50:09 -08003459 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
3460 attribute, startInputFlags, startInputReason);
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003461 didStart = true;
3462 }
lumarkd85e1582019-12-29 20:20:41 +08003463 showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null,
3464 SoftInputShowHideReason.SHOW_STATE_VISIBLE_FORWARD_NAV);
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003465 } else {
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003466 Slog.e(TAG, "SOFT_INPUT_STATE_VISIBLE is ignored because"
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003467 + " there is no focused view that also returns true from"
3468 + " View#onCheckIsTextEditor()");
3469 }
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003470 }
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003471 break;
Yohei Yukawa6e875592019-01-28 00:49:30 -08003472 case LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE:
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003473 if (DEBUG) Slog.v(TAG, "Window asks to always show input");
3474 if (InputMethodUtils.isSoftInputModeStateVisibleAllowed(
3475 unverifiedTargetSdkVersion, startInputFlags)) {
3476 if (attribute != null) {
3477 res = startInputUncheckedLocked(cs, inputContext, missingMethods,
3478 attribute, startInputFlags, startInputReason);
3479 didStart = true;
3480 }
lumarkd85e1582019-12-29 20:20:41 +08003481 showCurrentInputLocked(windowToken, InputMethodManager.SHOW_IMPLICIT, null,
3482 SoftInputShowHideReason.SHOW_STATE_ALWAYS_VISIBLE);
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003483 } else {
3484 Slog.e(TAG, "SOFT_INPUT_STATE_ALWAYS_VISIBLE is ignored because"
3485 + " there is no focused view that also returns true from"
3486 + " View#onCheckIsTextEditor()");
3487 }
3488 break;
3489 }
3490
3491 if (!didStart) {
3492 if (attribute != null) {
3493 if (!DebugFlags.FLAG_OPTIMIZE_START_INPUT.value()
3494 || (startInputFlags & StartInputFlags.IS_TEXT_EDITOR) != 0) {
Yohei Yukawa67464522019-01-28 00:50:09 -08003495 res = startInputUncheckedLocked(cs, inputContext, missingMethods, attribute,
Yohei Yukawa13dc12f2019-01-28 00:49:24 -08003496 startInputFlags, startInputReason);
3497 } else {
3498 res = InputBindResult.NO_EDITOR;
3499 }
3500 } else {
3501 res = InputBindResult.NULL_EDITOR_INFO;
Yohei Yukawa7cc33562019-01-28 00:49:18 -08003502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
Dianne Hackborn7663d802012-02-24 13:08:49 -08003504 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003506
Guliz Tuncay6908c152017-06-02 16:06:10 -07003507 private boolean canShowInputMethodPickerLocked(IInputMethodClient client) {
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08003508 // TODO(yukawa): multi-display support.
Guliz Tuncay6908c152017-06-02 16:06:10 -07003509 final int uid = Binder.getCallingUid();
lumark0b05f9e2018-11-26 15:09:06 +08003510 if (mCurFocusedWindowClient != null && client != null
Tarandeep Singheb570612018-01-29 16:20:32 -08003511 && mCurFocusedWindowClient.client.asBinder() == client.asBinder()) {
Guliz Tuncay6908c152017-06-02 16:06:10 -07003512 return true;
3513 } else if (mCurIntent != null && InputMethodUtils.checkIfPackageBelongsToUid(
3514 mAppOpsManager,
3515 uid,
3516 mCurIntent.getComponent().getPackageName())) {
3517 return true;
Guliz Tuncay6908c152017-06-02 16:06:10 -07003518 }
Guliz Tuncay6908c152017-06-02 16:06:10 -07003519 return false;
3520 }
3521
satok42c5a162011-05-26 16:46:14 +09003522 @Override
Seigo Nonaka14e13912015-05-06 21:04:13 -07003523 public void showInputMethodPickerFromClient(
3524 IInputMethodClient client, int auxiliarySubtypeMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 synchronized (mMethodMap) {
Yohei Yukawa46d74762019-01-22 10:17:22 -08003526 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003527 return;
3528 }
Guliz Tuncay6908c152017-06-02 16:06:10 -07003529 if(!canShowInputMethodPickerLocked(client)) {
satok47a44912010-10-06 16:03:58 +09003530 Slog.w(TAG, "Ignoring showInputMethodPickerFromClient of uid "
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003531 + Binder.getCallingUid() + ": " + client);
Guliz Tuncay6908c152017-06-02 16:06:10 -07003532 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 }
3534
satok440aab52010-11-25 09:43:11 +09003535 // Always call subtype picker, because subtype picker is a superset of input method
3536 // picker.
lumark0b05f9e2018-11-26 15:09:06 +08003537 mHandler.sendMessage(mCaller.obtainMessageII(
3538 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode,
3539 (mCurClient != null) ? mCurClient.selfReportedDisplayId : DEFAULT_DISPLAY));
satokab751aa2010-09-14 19:17:36 +09003540 }
3541 }
3542
lumark0b05f9e2018-11-26 15:09:06 +08003543 @Override
3544 public void showInputMethodPickerFromSystem(IInputMethodClient client, int auxiliarySubtypeMode,
3545 int displayId) {
3546 if (mContext.checkCallingPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
3547 != PackageManager.PERMISSION_GRANTED) {
3548 throw new SecurityException(
3549 "showInputMethodPickerFromSystem requires WRITE_SECURE_SETTINGS permission");
3550 }
3551 // Always call subtype picker, because subtype picker is a superset of input method
3552 // picker.
3553 mHandler.sendMessage(mCaller.obtainMessageII(
3554 MSG_SHOW_IM_SUBTYPE_PICKER, auxiliarySubtypeMode, displayId));
3555 }
3556
Tarandeep Singheb570612018-01-29 16:20:32 -08003557 public boolean isInputMethodPickerShownForTest() {
3558 synchronized(mMethodMap) {
3559 if (mSwitchingDialog == null) {
3560 return false;
3561 }
3562 return mSwitchingDialog.isShowing();
3563 }
3564 }
3565
Yohei Yukawa0c1ebff2018-12-27 14:06:28 -08003566 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08003567 private void setInputMethod(@NonNull IBinder token, String id) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003568 synchronized (mMethodMap) {
Yohei Yukawa0c1ebff2018-12-27 14:06:28 -08003569 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003570 return;
3571 }
3572 setInputMethodWithSubtypeIdLocked(token, id, NOT_A_SUBTYPE_ID);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003573 }
satok28203512010-11-24 11:06:49 +09003574 }
3575
Yohei Yukawa4773ee12018-12-24 21:13:53 -08003576 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08003577 private void setInputMethodAndSubtype(@NonNull IBinder token, String id,
3578 InputMethodSubtype subtype) {
satok28203512010-11-24 11:06:49 +09003579 synchronized (mMethodMap) {
Yohei Yukawa4773ee12018-12-24 21:13:53 -08003580 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003581 return;
3582 }
satok28203512010-11-24 11:06:49 +09003583 if (subtype != null) {
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003584 setInputMethodWithSubtypeIdLocked(token, id,
3585 InputMethodUtils.getSubtypeIdFromHashCode(mMethodMap.get(id),
3586 subtype.hashCode()));
satok28203512010-11-24 11:06:49 +09003587 } else {
3588 setInputMethod(token, id);
3589 }
3590 }
satokab751aa2010-09-14 19:17:36 +09003591 }
3592
satok42c5a162011-05-26 16:46:14 +09003593 @Override
satokb416a712010-11-25 20:42:14 +09003594 public void showInputMethodAndSubtypeEnablerFromClient(
satok217f5482010-12-15 05:19:19 +09003595 IInputMethodClient client, String inputMethodId) {
satokb416a712010-11-25 20:42:14 +09003596 synchronized (mMethodMap) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003597 // TODO(yukawa): Should we verify the display ID?
Yohei Yukawa46d74762019-01-22 10:17:22 -08003598 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003599 return;
3600 }
satok7fee71f2010-12-17 18:54:26 +09003601 executeOrSendMessage(mCurMethod, mCaller.obtainMessageO(
3602 MSG_SHOW_IM_SUBTYPE_ENABLER, inputMethodId));
satokb416a712010-11-25 20:42:14 +09003603 }
3604 }
3605
Yohei Yukawa0c499082018-12-09 18:52:02 -08003606 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08003607 private boolean switchToPreviousInputMethod(@NonNull IBinder token) {
satok735cf382010-11-11 20:40:09 +09003608 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08003609 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawa0c499082018-12-09 18:52:02 -08003610 return false;
3611 }
satokc445bcd2011-01-25 18:57:24 +09003612 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
satok4fc87d62011-05-20 16:13:43 +09003613 final InputMethodInfo lastImi;
satok208d5632011-05-20 22:13:38 +09003614 if (lastIme != null) {
satok4fc87d62011-05-20 16:13:43 +09003615 lastImi = mMethodMap.get(lastIme.first);
3616 } else {
3617 lastImi = null;
satok735cf382010-11-11 20:40:09 +09003618 }
satok4fc87d62011-05-20 16:13:43 +09003619 String targetLastImiId = null;
3620 int subtypeId = NOT_A_SUBTYPE_ID;
3621 if (lastIme != null && lastImi != null) {
3622 final boolean imiIdIsSame = lastImi.getId().equals(mCurMethodId);
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003623 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
satok4fc87d62011-05-20 16:13:43 +09003624 final int currentSubtypeHash = mCurrentSubtype == null ? NOT_A_SUBTYPE_ID
3625 : mCurrentSubtype.hashCode();
3626 // If the last IME is the same as the current IME and the last subtype is not
3627 // defined, there is no need to switch to the last IME.
3628 if (!imiIdIsSame || lastSubtypeHash != currentSubtypeHash) {
3629 targetLastImiId = lastIme.first;
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003630 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok4fc87d62011-05-20 16:13:43 +09003631 }
3632 }
3633
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003634 if (TextUtils.isEmpty(targetLastImiId)
3635 && !InputMethodUtils.canAddToLastInputMethod(mCurrentSubtype)) {
satok4fc87d62011-05-20 16:13:43 +09003636 // This is a safety net. If the currentSubtype can't be added to the history
3637 // and the framework couldn't find the last ime, we will make the last ime be
3638 // the most applicable enabled keyboard subtype of the system imes.
3639 final List<InputMethodInfo> enabled = mSettings.getEnabledInputMethodListLocked();
3640 if (enabled != null) {
3641 final int N = enabled.size();
3642 final String locale = mCurrentSubtype == null
3643 ? mRes.getConfiguration().locale.toString()
3644 : mCurrentSubtype.getLocale();
3645 for (int i = 0; i < N; ++i) {
3646 final InputMethodInfo imi = enabled.get(i);
Yohei Yukawafd70fe82018-04-08 12:19:56 -07003647 if (imi.getSubtypeCount() > 0 && imi.isSystem()) {
satok4fc87d62011-05-20 16:13:43 +09003648 InputMethodSubtype keyboardSubtype =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003649 InputMethodUtils.findLastResortApplicableSubtypeLocked(mRes,
3650 InputMethodUtils.getSubtypes(imi),
3651 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, locale, true);
satok4fc87d62011-05-20 16:13:43 +09003652 if (keyboardSubtype != null) {
3653 targetLastImiId = imi.getId();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003654 subtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
satok4fc87d62011-05-20 16:13:43 +09003655 imi, keyboardSubtype.hashCode());
3656 if(keyboardSubtype.getLocale().equals(locale)) {
3657 break;
3658 }
3659 }
3660 }
3661 }
3662 }
3663 }
3664
3665 if (!TextUtils.isEmpty(targetLastImiId)) {
3666 if (DEBUG) {
3667 Slog.d(TAG, "Switch to: " + lastImi.getId() + ", " + lastIme.second
3668 + ", from: " + mCurMethodId + ", " + subtypeId);
3669 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003670 setInputMethodWithSubtypeIdLocked(token, targetLastImiId, subtypeId);
satok4fc87d62011-05-20 16:13:43 +09003671 return true;
3672 } else {
3673 return false;
3674 }
satok735cf382010-11-11 20:40:09 +09003675 }
3676 }
3677
Yohei Yukawa70f17e72018-12-09 18:51:38 -08003678 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08003679 private boolean switchToNextInputMethod(@NonNull IBinder token, boolean onlyCurrentIme) {
satok688bd472012-02-09 20:09:17 +09003680 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08003681 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09003682 return false;
3683 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003684 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawa136b6ce2018-05-02 10:55:35 -07003685 onlyCurrentIme, mMethodMap.get(mCurMethodId), mCurrentSubtype);
satok688bd472012-02-09 20:09:17 +09003686 if (nextSubtype == null) {
3687 return false;
3688 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003689 setInputMethodWithSubtypeIdLocked(token, nextSubtype.mImi.getId(),
3690 nextSubtype.mSubtypeId);
satok688bd472012-02-09 20:09:17 +09003691 return true;
3692 }
3693 }
3694
Yohei Yukawaeec552e2018-09-09 20:48:41 -07003695 @BinderThread
3696 private boolean shouldOfferSwitchingToNextInputMethod(@NonNull IBinder token) {
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09003697 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08003698 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawaa0755742014-06-04 20:28:18 +09003699 return false;
3700 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003701 final ImeSubtypeListItem nextSubtype = mSwitchingController.getNextInputMethodLocked(
Yohei Yukawa136b6ce2018-05-02 10:55:35 -07003702 false /* onlyCurrentIme */, mMethodMap.get(mCurMethodId), mCurrentSubtype);
Satoshi Kataoka2b10b522013-08-21 20:39:12 +09003703 if (nextSubtype == null) {
3704 return false;
3705 }
3706 return true;
3707 }
3708 }
3709
3710 @Override
satok68f1b782011-04-11 14:26:04 +09003711 public InputMethodSubtype getLastInputMethodSubtype() {
3712 synchronized (mMethodMap) {
Yohei Yukawa46d74762019-01-22 10:17:22 -08003713 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003714 return null;
3715 }
satok68f1b782011-04-11 14:26:04 +09003716 final Pair<String, String> lastIme = mSettings.getLastInputMethodAndSubtypeLocked();
3717 // TODO: Handle the case of the last IME with no subtypes
3718 if (lastIme == null || TextUtils.isEmpty(lastIme.first)
3719 || TextUtils.isEmpty(lastIme.second)) return null;
3720 final InputMethodInfo lastImi = mMethodMap.get(lastIme.first);
3721 if (lastImi == null) return null;
3722 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +01003723 final int lastSubtypeHash = Integer.parseInt(lastIme.second);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09003724 final int lastSubtypeId =
3725 InputMethodUtils.getSubtypeIdFromHashCode(lastImi, lastSubtypeHash);
satok0e7d7d62011-07-05 13:28:06 +09003726 if (lastSubtypeId < 0 || lastSubtypeId >= lastImi.getSubtypeCount()) {
3727 return null;
3728 }
3729 return lastImi.getSubtypeAt(lastSubtypeId);
satok68f1b782011-04-11 14:26:04 +09003730 } catch (NumberFormatException e) {
3731 return null;
3732 }
3733 }
3734 }
3735
satoke7c6998e2011-06-03 17:57:59 +09003736 @Override
satokee5e77c2011-09-02 18:50:15 +09003737 public void setAdditionalInputMethodSubtypes(String imiId, InputMethodSubtype[] subtypes) {
satok91e88122011-07-18 11:11:42 +09003738 // By this IPC call, only a process which shares the same uid with the IME can add
3739 // additional input method subtypes to the IME.
Yohei Yukawa70f5c482016-01-04 19:42:36 -08003740 if (TextUtils.isEmpty(imiId) || subtypes == null) return;
Yohei Yukawab557d572018-12-29 21:26:26 -08003741 final ArrayList<InputMethodSubtype> toBeAdded = new ArrayList<>();
3742 for (InputMethodSubtype subtype : subtypes) {
3743 if (!toBeAdded.contains(subtype)) {
3744 toBeAdded.add(subtype);
3745 } else {
3746 Slog.w(TAG, "Duplicated subtype definition found: "
3747 + subtype.getLocale() + ", " + subtype.getMode());
3748 }
3749 }
satoke7c6998e2011-06-03 17:57:59 +09003750 synchronized (mMethodMap) {
Yohei Yukawa46d74762019-01-22 10:17:22 -08003751 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08003752 return;
3753 }
Yohei Yukawa79247822017-01-23 15:26:15 -08003754 if (!mSystemReady) {
3755 return;
3756 }
satok91e88122011-07-18 11:11:42 +09003757 final InputMethodInfo imi = mMethodMap.get(imiId);
satokee5e77c2011-09-02 18:50:15 +09003758 if (imi == null) return;
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09003759 final String[] packageInfos;
3760 try {
3761 packageInfos = mIPackageManager.getPackagesForUid(Binder.getCallingUid());
3762 } catch (RemoteException e) {
3763 Slog.e(TAG, "Failed to get package infos");
3764 return;
3765 }
satok91e88122011-07-18 11:11:42 +09003766 if (packageInfos != null) {
3767 final int packageNum = packageInfos.length;
3768 for (int i = 0; i < packageNum; ++i) {
3769 if (packageInfos[i].equals(imi.getPackageName())) {
Yohei Yukawa7b1c8d72018-12-19 11:21:04 -08003770 if (subtypes.length > 0) {
Yohei Yukawab557d572018-12-29 21:26:26 -08003771 mAdditionalSubtypeMap.put(imi.getId(), toBeAdded);
Yohei Yukawa7b1c8d72018-12-19 11:21:04 -08003772 } else {
Yohei Yukawab557d572018-12-29 21:26:26 -08003773 mAdditionalSubtypeMap.remove(imi.getId());
Yohei Yukawa7b1c8d72018-12-19 11:21:04 -08003774 }
Yohei Yukawab557d572018-12-29 21:26:26 -08003775 AdditionalSubtypeUtils.save(mAdditionalSubtypeMap, mMethodMap,
3776 mSettings.getCurrentUserId());
satokc5933802011-08-31 21:26:04 +09003777 final long ident = Binder.clearCallingIdentity();
3778 try {
Yohei Yukawa94e33302016-02-12 19:37:03 -08003779 buildInputMethodListLocked(false /* resetDefaultEnabledIme */);
satokc5933802011-08-31 21:26:04 +09003780 } finally {
3781 Binder.restoreCallingIdentity(ident);
3782 }
satokee5e77c2011-09-02 18:50:15 +09003783 return;
satok91e88122011-07-18 11:11:42 +09003784 }
3785 }
3786 }
satoke7c6998e2011-06-03 17:57:59 +09003787 }
satokee5e77c2011-09-02 18:50:15 +09003788 return;
satoke7c6998e2011-06-03 17:57:59 +09003789 }
3790
Yohei Yukawab985e6e2018-09-05 17:07:52 -07003791 /**
Artur Satayev192ca302020-01-22 21:38:23 +00003792 * This is kept due to {@code @UnsupportedAppUsage} in
Yohei Yukawab985e6e2018-09-05 17:07:52 -07003793 * {@link InputMethodManager#getInputMethodWindowVisibleHeight()} and a dependency in
3794 * {@link InputMethodService#onCreate()}.
3795 *
3796 * <p>TODO(Bug 113914148): Check if we can remove this.</p>
3797 * @return {@link WindowManagerInternal#getInputMethodWindowVisibleHeight()}
3798 */
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003799 @Override
3800 public int getInputMethodWindowVisibleHeight() {
Yohei Yukawa4052a10f2018-10-15 15:35:55 +08003801 // TODO(yukawa): Should we verify the display ID?
lumark90120a82018-08-15 00:33:03 +08003802 return mWindowManagerInternal.getInputMethodWindowVisibleHeight(mCurTokenDisplayId);
Satoshi Kataoka658c7b82013-10-10 17:03:51 +09003803 }
3804
Yohei Yukawab4f328a2019-05-02 08:41:27 -07003805 @Override
3806 public void reportActivityView(IInputMethodClient parentClient, int childDisplayId,
3807 float[] matrixValues) {
3808 final DisplayInfo displayInfo = mDisplayManagerInternal.getDisplayInfo(childDisplayId);
3809 if (displayInfo == null) {
3810 throw new IllegalArgumentException(
3811 "Cannot find display for non-existent displayId: " + childDisplayId);
3812 }
3813 final int callingUid = Binder.getCallingUid();
3814 if (callingUid != displayInfo.ownerUid) {
3815 throw new SecurityException("The caller doesn't own the display.");
3816 }
3817
3818 synchronized (mMethodMap) {
3819 final ClientState cs = mClients.get(parentClient.asBinder());
3820 if (cs == null) {
3821 return;
3822 }
3823
3824 // null matrixValues means that the entry needs to be removed.
3825 if (matrixValues == null) {
3826 final ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(childDisplayId);
3827 if (info == null) {
3828 return;
3829 }
3830 if (info.mParentClient != cs) {
3831 throw new SecurityException("Only the owner client can clear"
3832 + " ActivityViewGeometry for display #" + childDisplayId);
3833 }
3834 mActivityViewDisplayIdToParentMap.remove(childDisplayId);
3835 return;
3836 }
3837
3838 ActivityViewInfo info = mActivityViewDisplayIdToParentMap.get(childDisplayId);
3839 if (info != null && info.mParentClient != cs) {
3840 throw new InvalidParameterException("Display #" + childDisplayId
3841 + " is already registered by " + info.mParentClient);
3842 }
3843 if (info == null) {
3844 if (!mWindowManagerInternal.isUidAllowedOnDisplay(childDisplayId, cs.uid)) {
3845 throw new SecurityException(cs + " cannot access to display #"
3846 + childDisplayId);
3847 }
3848 info = new ActivityViewInfo(cs, new Matrix());
3849 mActivityViewDisplayIdToParentMap.put(childDisplayId, info);
3850 }
3851 info.mMatrix.setValues(matrixValues);
3852
3853 if (mCurClient == null || mCurClient.curSession == null) {
3854 return;
3855 }
3856
3857 Matrix matrix = null;
3858 int displayId = mCurClient.selfReportedDisplayId;
3859 boolean needToNotify = false;
3860 while (true) {
3861 needToNotify |= (displayId == childDisplayId);
3862 final ActivityViewInfo next = mActivityViewDisplayIdToParentMap.get(displayId);
3863 if (next == null) {
3864 break;
3865 }
3866 if (matrix == null) {
3867 matrix = new Matrix(next.mMatrix);
3868 } else {
3869 matrix.postConcat(next.mMatrix);
3870 }
3871 if (next.mParentClient.selfReportedDisplayId == mCurTokenDisplayId) {
3872 if (needToNotify) {
3873 final float[] values = new float[9];
3874 matrix.getValues(values);
3875 try {
3876 mCurClient.client.updateActivityViewToScreenMatrix(mCurSeq, values);
3877 } catch (RemoteException e) {
3878 }
3879 }
3880 break;
3881 }
3882 displayId = info.mParentClient.selfReportedDisplayId;
3883 }
3884 }
3885 }
3886
Yohei Yukawac54c1172018-09-06 11:39:50 -07003887 @BinderThread
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07003888 private void notifyUserAction(@NonNull IBinder token) {
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003889 if (DEBUG) {
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07003890 Slog.d(TAG, "Got the notification of a user action.");
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003891 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003892 synchronized (mMethodMap) {
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07003893 if (mCurToken != token) {
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003894 if (DEBUG) {
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07003895 Slog.d(TAG, "Ignoring the user action notification from IMEs that are no longer"
3896 + " active.");
Yohei Yukawa3d1e8122014-06-06 19:12:47 +09003897 }
3898 return;
3899 }
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003900 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
3901 if (imi != null) {
Yohei Yukawa02970512014-06-05 16:16:18 +09003902 mSwitchingController.onUserActionLocked(imi, mCurrentSubtype);
Yohei Yukawa5a647b692014-05-22 12:49:00 +09003903 }
Satoshi Kataokad7443c82013-10-15 17:45:43 +09003904 }
3905 }
3906
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08003907 @BinderThread
3908 private void reportPreRendered(IBinder token, EditorInfo info) {
3909 synchronized (mMethodMap) {
3910 if (!calledWithValidTokenLocked(token)) {
3911 return;
3912 }
3913 if (mCurClient != null && mCurClient.client != null) {
3914 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageOO(
3915 MSG_REPORT_PRE_RENDERED, info, mCurClient));
3916 }
3917 }
3918 }
3919
3920 @BinderThread
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08003921 private void applyImeVisibility(IBinder token, IBinder windowToken, boolean setVisible) {
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08003922 synchronized (mMethodMap) {
3923 if (!calledWithValidTokenLocked(token)) {
3924 return;
3925 }
Tarandeep Singh500a38f2019-09-26 13:36:40 -07003926 if (!setVisible) {
Taran Singhd7fc5862019-10-10 14:45:17 +02003927 if (mCurClient != null) {
3928 // IMMS only knows of focused window, not the actual IME target.
3929 // e.g. it isn't aware of any window that has both
3930 // NOT_FOCUSABLE, ALT_FOCUSABLE_IM flags set and can the IME target.
3931 // Send it to window manager to hide IME from IME target window.
3932 // TODO(b/139861270): send to mCurClient.client once IMMS is aware of
3933 // actual IME target.
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003934 mWindowManagerInternal.hideIme(mHideRequestWindowMap.get(windowToken));
Tarandeep Singh500a38f2019-09-26 13:36:40 -07003935 }
3936 } else {
3937 // Send to window manager to show IME after IME layout finishes.
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08003938 mWindowManagerInternal.showImePostLayout(mShowRequestWindowMap.get(windowToken));
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08003939 }
3940 }
3941 }
3942
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003943 private void setInputMethodWithSubtypeIdLocked(IBinder token, String id, int subtypeId) {
3944 if (token == null) {
3945 if (mContext.checkCallingOrSelfPermission(
3946 android.Manifest.permission.WRITE_SECURE_SETTINGS)
3947 != PackageManager.PERMISSION_GRANTED) {
3948 throw new SecurityException(
3949 "Using null token requires permission "
3950 + android.Manifest.permission.WRITE_SECURE_SETTINGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 }
Yohei Yukawa4e02bc62014-06-04 18:37:20 +09003952 } else if (mCurToken != token) {
3953 Slog.w(TAG, "Ignoring setInputMethod of uid " + Binder.getCallingUid()
3954 + " token: " + token);
3955 return;
3956 }
3957
3958 final long ident = Binder.clearCallingIdentity();
3959 try {
3960 setInputMethodLocked(id, subtypeId);
3961 } finally {
3962 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 }
3964 }
3965
Yohei Yukawaeec552e2018-09-09 20:48:41 -07003966 @BinderThread
3967 private void hideMySoftInput(@NonNull IBinder token, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08003969 if (!calledWithValidTokenLocked(token)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 return;
3971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 long ident = Binder.clearCallingIdentity();
3973 try {
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08003974 hideCurrentInputLocked(
3975 mLastImeTargetWindow, flags, null,
3976 SoftInputShowHideReason.HIDE_MY_SOFT_INPUT);
3977
The Android Open Source Project4df24232009-03-05 14:34:35 -08003978 } finally {
3979 Binder.restoreCallingIdentity(ident);
3980 }
3981 }
3982 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08003983
Yohei Yukawaeec552e2018-09-09 20:48:41 -07003984 @BinderThread
3985 private void showMySoftInput(@NonNull IBinder token, int flags) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003986 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08003987 if (!calledWithValidTokenLocked(token)) {
The Android Open Source Project4df24232009-03-05 14:34:35 -08003988 return;
3989 }
3990 long ident = Binder.clearCallingIdentity();
3991 try {
lumarkd85e1582019-12-29 20:20:41 +08003992 showCurrentInputLocked(mLastImeTargetWindow, flags, null,
3993 SoftInputShowHideReason.SHOW_MY_SOFT_INPUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 } finally {
3995 Binder.restoreCallingIdentity(ident);
3996 }
3997 }
3998 }
3999
4000 void setEnabledSessionInMainThread(SessionState session) {
4001 if (mEnabledSession != session) {
Yohei Yukawa9d91b432014-05-19 16:03:24 +09004002 if (mEnabledSession != null && mEnabledSession.session != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004004 if (DEBUG) Slog.v(TAG, "Disabling: " + mEnabledSession);
Yohei Yukawa9d91b432014-05-19 16:03:24 +09004005 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 } catch (RemoteException e) {
4007 }
4008 }
4009 mEnabledSession = session;
Yohei Yukawa9d91b432014-05-19 16:03:24 +09004010 if (mEnabledSession != null && mEnabledSession.session != null) {
4011 try {
4012 if (DEBUG) Slog.v(TAG, "Enabling: " + mEnabledSession);
4013 mEnabledSession.method.setSessionEnabled(mEnabledSession.session, true);
4014 } catch (RemoteException e) {
4015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016 }
4017 }
4018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004019
Yohei Yukawa930328c2017-10-18 20:19:53 -07004020 @MainThread
satok42c5a162011-05-26 16:46:14 +09004021 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 public boolean handleMessage(Message msg) {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004023 SomeArgs args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 switch (msg.what) {
satokab751aa2010-09-14 19:17:36 +09004025 case MSG_SHOW_IM_SUBTYPE_PICKER:
Seigo Nonaka14e13912015-05-06 21:04:13 -07004026 final boolean showAuxSubtypes;
lumark0b05f9e2018-11-26 15:09:06 +08004027 final int displayId = msg.arg2;
Seigo Nonaka14e13912015-05-06 21:04:13 -07004028 switch (msg.arg1) {
4029 case InputMethodManager.SHOW_IM_PICKER_MODE_AUTO:
4030 // This is undocumented so far, but IMM#showInputMethodPicker() has been
4031 // implemented so that auxiliary subtypes will be excluded when the soft
4032 // keyboard is invisible.
4033 showAuxSubtypes = mInputShown;
4034 break;
4035 case InputMethodManager.SHOW_IM_PICKER_MODE_INCLUDE_AUXILIARY_SUBTYPES:
4036 showAuxSubtypes = true;
4037 break;
4038 case InputMethodManager.SHOW_IM_PICKER_MODE_EXCLUDE_AUXILIARY_SUBTYPES:
4039 showAuxSubtypes = false;
4040 break;
4041 default:
4042 Slog.e(TAG, "Unknown subtype picker mode = " + msg.arg1);
4043 return false;
4044 }
lumark0b05f9e2018-11-26 15:09:06 +08004045 showInputMethodMenu(showAuxSubtypes, displayId);
satokab751aa2010-09-14 19:17:36 +09004046 return true;
4047
satok47a44912010-10-06 16:03:58 +09004048 case MSG_SHOW_IM_SUBTYPE_ENABLER:
Yohei Yukawa41f34272015-12-14 15:41:52 -08004049 showInputMethodAndSubtypeEnabler((String)msg.obj);
satok217f5482010-12-15 05:19:19 +09004050 return true;
4051
4052 case MSG_SHOW_IM_CONFIG:
4053 showConfigureInputMethods();
satok47a44912010-10-06 16:03:58 +09004054 return true;
4055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 case MSG_UNBIND_INPUT:
4059 try {
4060 ((IInputMethod)msg.obj).unbindInput();
4061 } catch (RemoteException e) {
4062 // There is nothing interesting about the method dying.
4063 }
4064 return true;
4065 case MSG_BIND_INPUT:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004066 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 try {
4068 ((IInputMethod)args.arg1).bindInput((InputBinding)args.arg2);
4069 } catch (RemoteException e) {
4070 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004071 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 return true;
4073 case MSG_SHOW_SOFT_INPUT:
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004074 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 try {
lumarkd85e1582019-12-29 20:20:41 +08004076 final @SoftInputShowHideReason int reason = msg.arg2;
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07004077 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".showSoftInput("
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004078 + args.arg3 + ", " + msg.arg1 + ", " + args.arg2 + ") for reason: "
lumarkd85e1582019-12-29 20:20:41 +08004079 + InputMethodDebug.softInputDisplayReasonToString(reason));
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08004080 ((IInputMethod) args.arg1).showSoftInput(
4081 (IBinder) args.arg3, msg.arg1, (ResultReceiver) args.arg2);
Anmol Gupta29f209d2020-03-11 11:34:46 -07004082 mSoftInputShowHideHistory.addEntry(new SoftInputShowHideHistory.Entry(
4083 mCurClient, mCurAttribute,
4084 mWindowManagerInternal.getWindowName(mCurFocusedWindow),
4085 mCurFocusedWindowSoftInputMode, reason, mInFullscreenMode,
4086 mWindowManagerInternal.getWindowName(
4087 mShowRequestWindowMap.get(args.arg3))));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 } catch (RemoteException e) {
4089 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004090 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 return true;
4092 case MSG_HIDE_SOFT_INPUT:
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004093 args = (SomeArgs) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 try {
lumarkd85e1582019-12-29 20:20:41 +08004095 final @SoftInputShowHideReason int reason = msg.arg1;
Craig Mautnere4bbb1c2013-03-15 11:38:44 -07004096 if (DEBUG) Slog.v(TAG, "Calling " + args.arg1 + ".hideSoftInput(0, "
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004097 + args.arg3 + ", " + args.arg2 + ") for reason: "
lumarkd85e1582019-12-29 20:20:41 +08004098 + InputMethodDebug.softInputDisplayReasonToString(reason));
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004099 ((IInputMethod)args.arg1).hideSoftInput(
4100 (IBinder) args.arg3, 0, (ResultReceiver)args.arg2);
Anmol Gupta29f209d2020-03-11 11:34:46 -07004101 mSoftInputShowHideHistory.addEntry(new SoftInputShowHideHistory.Entry(
4102 mCurClient, mCurAttribute,
4103 mWindowManagerInternal.getWindowName(mCurFocusedWindow),
4104 mCurFocusedWindowSoftInputMode, reason, mInFullscreenMode,
4105 mWindowManagerInternal.getWindowName(
4106 mHideRequestWindowMap.get(args.arg3))));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 } catch (RemoteException e) {
4108 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004109 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 return true;
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004111 case MSG_HIDE_CURRENT_INPUT_METHOD:
4112 synchronized (mMethodMap) {
lumarkd85e1582019-12-29 20:20:41 +08004113 final @SoftInputShowHideReason int reason = (int) msg.obj;
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08004114 hideCurrentInputLocked(mCurFocusedWindow, 0, null, reason);
4115
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004116 }
4117 return true;
Yohei Yukawac54c1172018-09-06 11:39:50 -07004118 case MSG_INITIALIZE_IME:
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004119 args = (SomeArgs)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 try {
lumark90120a82018-08-15 00:33:03 +08004121 if (DEBUG) {
4122 Slog.v(TAG, "Sending attach of token: " + args.arg2 + " for display: "
4123 + msg.arg1);
4124 }
Yohei Yukawac54c1172018-09-06 11:39:50 -07004125 final IBinder token = (IBinder) args.arg2;
lumark90120a82018-08-15 00:33:03 +08004126 ((IInputMethod) args.arg1).initializeInternal(token, msg.arg1,
Yohei Yukawac54c1172018-09-06 11:39:50 -07004127 new InputMethodPrivilegedOperationsImpl(this, token));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 } catch (RemoteException e) {
4129 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004130 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004131 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07004132 case MSG_CREATE_SESSION: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004133 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07004134 IInputMethod method = (IInputMethod)args.arg1;
Jeff Brownc28867a2013-03-26 15:42:39 -07004135 InputChannel channel = (InputChannel)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07004137 method.createSession(channel, (IInputSessionCallback)args.arg3);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 } catch (RemoteException e) {
Jeff Brownc28867a2013-03-26 15:42:39 -07004139 } finally {
Jeff Brown1951ce82013-04-04 22:45:12 -07004140 // Dispose the channel if the input method is not local to this process
4141 // because the remote proxy will get its own copy when unparceled.
4142 if (channel != null && Binder.isProxy(method)) {
Jeff Brownc28867a2013-03-26 15:42:39 -07004143 channel.dispose();
4144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004146 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 return true;
Jeff Brownc28867a2013-03-26 15:42:39 -07004148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004150
Yohei Yukawa19a80a12016-03-14 22:57:37 -07004151 case MSG_START_INPUT: {
Yohei Yukawaf7526b52017-02-11 20:57:10 -08004152 final int missingMethods = msg.arg1;
4153 final boolean restarting = msg.arg2 != 0;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07004154 args = (SomeArgs) msg.obj;
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08004155 final IBinder startInputToken = (IBinder) args.arg1;
4156 final SessionState session = (SessionState) args.arg2;
4157 final IInputContext inputContext = (IInputContext) args.arg3;
4158 final EditorInfo editorInfo = (EditorInfo) args.arg4;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 setEnabledSessionInMainThread(session);
Yohei Yukawa6db3bfe2017-02-13 12:04:41 -08004161 session.method.startInput(startInputToken, inputContext, missingMethods,
Tarandeep Singheadb1392018-11-09 18:15:57 +01004162 editorInfo, restarting, session.client.shouldPreRenderIme);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 } catch (RemoteException e) {
4164 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004165 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 return true;
Yohei Yukawa19a80a12016-03-14 22:57:37 -07004167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 // ---------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004170
Yohei Yukawa33e81792015-11-17 21:14:42 -08004171 case MSG_UNBIND_CLIENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 try {
Yohei Yukawa33e81792015-11-17 21:14:42 -08004173 ((IInputMethodClient)msg.obj).onUnbindMethod(msg.arg1, msg.arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 } catch (RemoteException e) {
4175 // There is nothing interesting about the last client dying.
4176 }
4177 return true;
Yohei Yukawa33e81792015-11-17 21:14:42 -08004178 case MSG_BIND_CLIENT: {
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004179 args = (SomeArgs)msg.obj;
Jeff Brown1951ce82013-04-04 22:45:12 -07004180 IInputMethodClient client = (IInputMethodClient)args.arg1;
4181 InputBindResult res = (InputBindResult)args.arg2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 try {
Jeff Brown1951ce82013-04-04 22:45:12 -07004183 client.onBindMethod(res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004185 Slog.w(TAG, "Client died receiving input method " + args.arg2);
Jeff Brown1951ce82013-04-04 22:45:12 -07004186 } finally {
4187 // Dispose the channel if the input method is not local to this process
4188 // because the remote proxy will get its own copy when unparceled.
4189 if (res.channel != null && Binder.isProxy(client)) {
4190 res.channel.dispose();
4191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 }
Svetoslav Ganov758143e2012-08-06 16:40:27 -07004193 args.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 return true;
Jeff Brown1951ce82013-04-04 22:45:12 -07004195 }
Dianne Hackborna6e41342012-05-22 16:30:34 -07004196 case MSG_SET_ACTIVE:
4197 try {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004198 ((ClientState)msg.obj).client.setActive(msg.arg1 != 0, msg.arg2 != 0);
Dianne Hackborna6e41342012-05-22 16:30:34 -07004199 } catch (RemoteException e) {
4200 Slog.w(TAG, "Got RemoteException sending setActive(false) notification to pid "
4201 + ((ClientState)msg.obj).pid + " uid "
4202 + ((ClientState)msg.obj).uid);
4203 }
4204 return true;
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004205 case MSG_REPORT_FULLSCREEN_MODE: {
4206 final boolean fullscreen = msg.arg1 != 0;
4207 final ClientState clientState = (ClientState)msg.obj;
4208 try {
4209 clientState.client.reportFullscreenMode(fullscreen);
4210 } catch (RemoteException e) {
4211 Slog.w(TAG, "Got RemoteException sending "
4212 + "reportFullscreen(" + fullscreen + ") notification to pid="
4213 + clientState.pid + " uid=" + clientState.uid);
4214 }
4215 return true;
4216 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08004217 case MSG_REPORT_PRE_RENDERED: {
4218 args = (SomeArgs) msg.obj;
4219 final EditorInfo info = (EditorInfo) args.arg1;
4220 final ClientState clientState = (ClientState) args.arg2;
4221 try {
4222 clientState.client.reportPreRendered(info);
4223 } catch (RemoteException e) {
4224 Slog.w(TAG, "Got RemoteException sending "
4225 + "reportPreRendered(" + info + ") notification to pid="
4226 + clientState.pid + " uid=" + clientState.uid);
4227 }
4228 args.recycle();
4229 return true;
4230 }
4231 case MSG_APPLY_IME_VISIBILITY: {
4232 final boolean setVisible = msg.arg1 != 0;
4233 final ClientState clientState = (ClientState) msg.obj;
4234 try {
4235 clientState.client.applyImeVisibility(setVisible);
4236 } catch (RemoteException e) {
4237 Slog.w(TAG, "Got RemoteException sending "
4238 + "applyImeVisibility(" + setVisible + ") notification to pid="
4239 + clientState.pid + " uid=" + clientState.uid);
4240 }
4241 return true;
4242 }
satok01038492012-04-09 21:08:27 +09004243
4244 // --------------------------------------------------------------
4245 case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
Michael Wright7b5a96b2014-08-09 19:28:42 -07004246 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
satok01038492012-04-09 21:08:27 +09004247 return true;
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +09004248 case MSG_SYSTEM_UNLOCK_USER: {
Fyodor Kupolov7877b8a2016-06-29 14:39:19 -07004249 final int userId = msg.arg1;
4250 onUnlockUser(userId);
4251 return true;
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +09004252 }
4253 case MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED: {
4254 final int userId = msg.arg1;
4255 final List<InputMethodInfo> imes = (List<InputMethodInfo>) msg.obj;
4256 mInputMethodListListeners.forEach(
4257 listener -> listener.onInputMethodListUpdated(imes, userId));
4258 return true;
4259 }
Adam Hebc67f2e2019-11-13 14:34:56 -08004260
4261 // ---------------------------------------------------------------
4262 case MSG_INLINE_SUGGESTIONS_REQUEST:
4263 args = (SomeArgs) msg.obj;
Feng Cao36960ee2020-02-18 18:23:30 -08004264 final InlineSuggestionsRequestInfo requestInfo =
4265 (InlineSuggestionsRequestInfo) args.arg2;
Adam Hebc67f2e2019-11-13 14:34:56 -08004266 final IInlineSuggestionsRequestCallback callback =
Feng Cao36960ee2020-02-18 18:23:30 -08004267 (IInlineSuggestionsRequestCallback) args.arg3;
Adam Hebc67f2e2019-11-13 14:34:56 -08004268 try {
Feng Cao36960ee2020-02-18 18:23:30 -08004269 ((IInputMethod) args.arg1).onCreateInlineSuggestionsRequest(requestInfo,
4270 callback);
Adam Hebc67f2e2019-11-13 14:34:56 -08004271 } catch (RemoteException e) {
4272 Slog.w(TAG, "RemoteException calling onCreateInlineSuggestionsRequest(): " + e);
4273 }
4274 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004275 }
4276 return false;
4277 }
4278
satokdc9ddae2011-10-06 12:22:36 +09004279 private boolean chooseNewDefaultIMELocked() {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004280 final InputMethodInfo imi = InputMethodUtils.getMostApplicableDefaultIME(
4281 mSettings.getEnabledInputMethodListLocked());
satokdc9ddae2011-10-06 12:22:36 +09004282 if (imi != null) {
satok03eb319a2010-11-11 18:17:42 +09004283 if (DEBUG) {
4284 Slog.d(TAG, "New default IME was selected: " + imi.getId());
4285 }
satok723a27e2010-11-11 14:58:11 +09004286 resetSelectedInputMethodAndSubtypeLocked(imi.getId());
Brandon Ballinger6da35a02009-10-21 00:38:13 -07004287 return true;
4288 }
4289
4290 return false;
4291 }
4292
Yohei Yukawa05139322018-12-25 10:34:14 -08004293 static void queryInputMethodServicesInternal(Context context,
4294 @UserIdInt int userId, ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap,
4295 ArrayMap<String, InputMethodInfo> methodMap, ArrayList<InputMethodInfo> methodList) {
4296 methodList.clear();
4297 methodMap.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298
Yohei Yukawaed4952a2016-02-17 07:57:25 -08004299 // Note: We do not specify PackageManager.MATCH_ENCRYPTION_* flags here because the default
4300 // behavior of PackageManager is exactly what we want. It by default picks up appropriate
4301 // services depending on the unlock state for the specified user.
Yohei Yukawa05139322018-12-25 10:34:14 -08004302 final List<ResolveInfo> services = context.getPackageManager().queryIntentServicesAsUser(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 new Intent(InputMethod.SERVICE_INTERFACE),
Yohei Yukawa5ec1fce2018-12-17 07:56:14 -08004304 PackageManager.GET_META_DATA | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS,
Yohei Yukawa05139322018-12-25 10:34:14 -08004305 userId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004306
Yohei Yukawa05139322018-12-25 10:34:14 -08004307 methodList.ensureCapacity(services.size());
4308 methodMap.ensureCapacity(services.size());
4309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 for (int i = 0; i < services.size(); ++i) {
4311 ResolveInfo ri = services.get(i);
4312 ServiceInfo si = ri.serviceInfo;
Yohei Yukawaddad4b92017-02-02 01:46:13 -08004313 final String imeId = InputMethodInfo.computeId(ri);
4314 if (!android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
4315 Slog.w(TAG, "Skipping input method " + imeId
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 + ": it does not require the permission "
4317 + android.Manifest.permission.BIND_INPUT_METHOD);
4318 continue;
4319 }
4320
Yohei Yukawaddad4b92017-02-02 01:46:13 -08004321 if (DEBUG) Slog.d(TAG, "Checking " + imeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322
4323 try {
Yohei Yukawa05139322018-12-25 10:34:14 -08004324 final InputMethodInfo imi = new InputMethodInfo(context, ri,
4325 additionalSubtypeMap.get(imeId));
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08004326 if (imi.isVrOnly()) {
4327 continue; // Skip VR-only IME, which isn't supported for now.
4328 }
Yohei Yukawa05139322018-12-25 10:34:14 -08004329 methodList.add(imi);
4330 methodMap.put(imi.getId(), imi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 if (DEBUG) {
Yohei Yukawa05139322018-12-25 10:34:14 -08004332 Slog.d(TAG, "Found an input method " + imi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 }
Tadashi G. Takaoka3c23d5b2016-09-16 11:41:07 +09004334 } catch (Exception e) {
Yohei Yukawaddad4b92017-02-02 01:46:13 -08004335 Slog.wtf(TAG, "Unable to load input method " + imeId, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
Yohei Yukawa05139322018-12-25 10:34:14 -08004338 }
4339
4340 @GuardedBy("mMethodMap")
4341 void buildInputMethodListLocked(boolean resetDefaultEnabledIme) {
4342 if (DEBUG) {
4343 Slog.d(TAG, "--- re-buildInputMethodList reset = " + resetDefaultEnabledIme
4344 + " \n ------ caller=" + Debug.getCallers(10));
4345 }
4346 if (!mSystemReady) {
4347 Slog.e(TAG, "buildInputMethodListLocked is not allowed until system is ready");
4348 return;
4349 }
4350 mMethodMapUpdateCount++;
4351 mMyPackageMonitor.clearKnownImePackageNamesLocked();
4352
4353 queryInputMethodServicesInternal(mContext, mSettings.getCurrentUserId(),
Yohei Yukawab557d572018-12-29 21:26:26 -08004354 mAdditionalSubtypeMap, mMethodMap, mMethodList);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07004355
Yohei Yukawac4e44912017-02-09 19:30:22 -08004356 // Construct the set of possible IME packages for onPackageChanged() to avoid false
4357 // negatives when the package state remains to be the same but only the component state is
4358 // changed.
4359 {
4360 // Here we intentionally use PackageManager.MATCH_DISABLED_COMPONENTS since the purpose
4361 // of this query is to avoid false negatives. PackageManager.MATCH_ALL could be more
4362 // conservative, but it seems we cannot use it for now (Issue 35176630).
Yohei Yukawa05139322018-12-25 10:34:14 -08004363 final List<ResolveInfo> allInputMethodServices =
4364 mContext.getPackageManager().queryIntentServicesAsUser(
4365 new Intent(InputMethod.SERVICE_INTERFACE),
4366 PackageManager.MATCH_DISABLED_COMPONENTS, mSettings.getCurrentUserId());
Yohei Yukawac4e44912017-02-09 19:30:22 -08004367 final int N = allInputMethodServices.size();
4368 for (int i = 0; i < N; ++i) {
4369 final ServiceInfo si = allInputMethodServices.get(i).serviceInfo;
Yohei Yukawa5e3e8a52017-02-14 20:05:17 -08004370 if (android.Manifest.permission.BIND_INPUT_METHOD.equals(si.permission)) {
4371 mMyPackageMonitor.addKnownImePackageNameLocked(si.packageName);
Yohei Yukawac4e44912017-02-09 19:30:22 -08004372 }
Yohei Yukawac4e44912017-02-09 19:30:22 -08004373 }
4374 }
4375
Yohei Yukawa9c372192018-03-20 22:54:56 -07004376 boolean reenableMinimumNonAuxSystemImes = false;
Yohei Yukawa859df052016-02-17 07:56:46 -08004377 // TODO: The following code should find better place to live.
4378 if (!resetDefaultEnabledIme) {
4379 boolean enabledImeFound = false;
Yohei Yukawa9c372192018-03-20 22:54:56 -07004380 boolean enabledNonAuxImeFound = false;
Yohei Yukawa859df052016-02-17 07:56:46 -08004381 final List<InputMethodInfo> enabledImes = mSettings.getEnabledInputMethodListLocked();
4382 final int N = enabledImes.size();
4383 for (int i = 0; i < N; ++i) {
4384 final InputMethodInfo imi = enabledImes.get(i);
4385 if (mMethodList.contains(imi)) {
4386 enabledImeFound = true;
Yohei Yukawa9c372192018-03-20 22:54:56 -07004387 if (!imi.isAuxiliaryIme()) {
4388 enabledNonAuxImeFound = true;
4389 break;
4390 }
Yohei Yukawa859df052016-02-17 07:56:46 -08004391 }
4392 }
4393 if (!enabledImeFound) {
Yohei Yukawad0332832017-02-01 13:59:43 -08004394 if (DEBUG) {
4395 Slog.i(TAG, "All the enabled IMEs are gone. Reset default enabled IMEs.");
4396 }
Yohei Yukawa859df052016-02-17 07:56:46 -08004397 resetDefaultEnabledIme = true;
4398 resetSelectedInputMethodAndSubtypeLocked("");
Yohei Yukawa9c372192018-03-20 22:54:56 -07004399 } else if (!enabledNonAuxImeFound) {
4400 if (DEBUG) {
4401 Slog.i(TAG, "All the enabled non-Aux IMEs are gone. Do partial reset.");
4402 }
4403 reenableMinimumNonAuxSystemImes = true;
Yohei Yukawa859df052016-02-17 07:56:46 -08004404 }
4405 }
4406
Yohei Yukawa9c372192018-03-20 22:54:56 -07004407 if (resetDefaultEnabledIme || reenableMinimumNonAuxSystemImes) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09004408 final ArrayList<InputMethodInfo> defaultEnabledIme =
Yohei Yukawa9c372192018-03-20 22:54:56 -07004409 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList,
4410 reenableMinimumNonAuxSystemImes);
Yohei Yukawa68645a62016-02-17 07:54:20 -08004411 final int N = defaultEnabledIme.size();
4412 for (int i = 0; i < N; ++i) {
Satoshi Kataokaf1367b72013-01-25 17:20:12 +09004413 final InputMethodInfo imi = defaultEnabledIme.get(i);
4414 if (DEBUG) {
4415 Slog.d(TAG, "--- enable ime = " + imi);
4416 }
4417 setInputMethodEnabledLocked(imi.getId(), true);
4418 }
4419 }
4420
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004421 final String defaultImiId = mSettings.getSelectedInputMethod();
satok0a1bcf42012-05-16 19:26:31 +09004422 if (!TextUtils.isEmpty(defaultImiId)) {
Yohei Yukawa94e33302016-02-12 19:37:03 -08004423 if (!mMethodMap.containsKey(defaultImiId)) {
satok0a1bcf42012-05-16 19:26:31 +09004424 Slog.w(TAG, "Default IME is uninstalled. Choose new default IME.");
4425 if (chooseNewDefaultIMELocked()) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07004426 updateInputMethodsFromSettingsLocked(true);
satok0a1bcf42012-05-16 19:26:31 +09004427 }
4428 } else {
4429 // Double check that the default IME is certainly enabled.
4430 setInputMethodEnabledLocked(defaultImiId, true);
Brandon Ballinger6da35a02009-10-21 00:38:13 -07004431 }
4432 }
Yohei Yukawa3d46bab2014-05-30 18:10:18 +09004433 // Here is not the perfect place to reset the switching controller. Ideally
4434 // mSwitchingController and mSettings should be able to share the same state.
4435 // TODO: Make sure that mSwitchingController and mSettings are sharing the
4436 // the same enabled IMEs list.
Yohei Yukawac834a252014-05-21 22:42:32 +09004437 mSwitchingController.resetCircularListLocked(mContext);
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +09004438
4439 // Notify InputMethodListListeners of the new installed InputMethods.
4440 final List<InputMethodInfo> inputMethodList = new ArrayList<>(mMethodList);
4441 mHandler.obtainMessage(MSG_DISPATCH_ON_INPUT_METHOD_LIST_UPDATED,
4442 mSettings.getCurrentUserId(), 0 /* unused */, inputMethodList).sendToTarget();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004446
satok217f5482010-12-15 05:19:19 +09004447 private void showInputMethodAndSubtypeEnabler(String inputMethodId) {
Tadashi G. Takaokaf49688f2011-01-20 17:56:13 +09004448 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
satok47a44912010-10-06 16:03:58 +09004449 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
satok86417ea2010-10-27 14:11:03 +09004450 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
4451 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
satok7fee71f2010-12-17 18:54:26 +09004452 if (!TextUtils.isEmpty(inputMethodId)) {
Tadashi G. Takaoka25480202011-01-20 23:13:02 +09004453 intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
satok7fee71f2010-12-17 18:54:26 +09004454 }
Yohei Yukawa41f34272015-12-14 15:41:52 -08004455 final int userId;
4456 synchronized (mMethodMap) {
4457 userId = mSettings.getCurrentUserId();
4458 }
4459 mContext.startActivityAsUser(intent, null, UserHandle.of(userId));
satok217f5482010-12-15 05:19:19 +09004460 }
4461
4462 private void showConfigureInputMethods() {
4463 Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
4464 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
4465 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
4466 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Satoshi Kataoka3ba439d2012-10-05 18:30:13 +09004467 mContext.startActivityAsUser(intent, null, UserHandle.CURRENT);
satok47a44912010-10-06 16:03:58 +09004468 }
4469
satok2c93efc2012-04-02 19:33:47 +09004470 private boolean isScreenLocked() {
4471 return mKeyguardManager != null
4472 && mKeyguardManager.isKeyguardLocked() && mKeyguardManager.isKeyguardSecure();
4473 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004474
lumark0b05f9e2018-11-26 15:09:06 +08004475 private void showInputMethodMenu(boolean showAuxSubtypes, int displayId) {
Seigo Nonaka14e13912015-05-06 21:04:13 -07004476 if (DEBUG) Slog.v(TAG, "Show switching menu. showAuxSubtypes=" + showAuxSubtypes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477
satok2c93efc2012-04-02 19:33:47 +09004478 final boolean isScreenLocked = isScreenLocked();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004479
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004480 final String lastInputMethodId = mSettings.getSelectedInputMethod();
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004481 int lastInputMethodSubtypeId = mSettings.getSelectedInputMethodSubtypeId(lastInputMethodId);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004482 if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004483
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07004484 synchronized (mMethodMap) {
Yohei Yukawacf3bbff2018-11-20 17:24:20 -08004485 final List<ImeSubtypeListItem> imList =
4486 mSwitchingController.getSortedInputMethodAndSubtypeListLocked(
4487 showAuxSubtypes, isScreenLocked);
4488 if (imList.isEmpty()) {
satok7f35c8c2010-10-07 21:13:11 +09004489 return;
4490 }
4491
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07004492 hideInputMethodMenuLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493
satokc3690562012-01-10 20:14:43 +09004494 if (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID) {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004495 final InputMethodSubtype currentSubtype = getCurrentInputMethodSubtypeLocked();
satokc3690562012-01-10 20:14:43 +09004496 if (currentSubtype != null) {
4497 final InputMethodInfo currentImi = mMethodMap.get(mCurMethodId);
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004498 lastInputMethodSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
4499 currentImi, currentSubtype.hashCode());
satokc3690562012-01-10 20:14:43 +09004500 }
4501 }
4502
Ken Wakasa761eb372011-03-04 19:06:18 +09004503 final int N = imList.size();
satokab751aa2010-09-14 19:17:36 +09004504 mIms = new InputMethodInfo[N];
4505 mSubtypeIds = new int[N];
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07004506 int checkedItem = 0;
4507 for (int i = 0; i < N; ++i) {
Ken Wakasa05dbb652011-08-22 15:22:43 +09004508 final ImeSubtypeListItem item = imList.get(i);
4509 mIms[i] = item.mImi;
4510 mSubtypeIds[i] = item.mSubtypeId;
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07004511 if (mIms[i].getId().equals(lastInputMethodId)) {
satokab751aa2010-09-14 19:17:36 +09004512 int subtypeId = mSubtypeIds[i];
4513 if ((subtypeId == NOT_A_SUBTYPE_ID)
4514 || (lastInputMethodSubtypeId == NOT_A_SUBTYPE_ID && subtypeId == 0)
4515 || (subtypeId == lastInputMethodSubtypeId)) {
4516 checkedItem = i;
4517 }
Dianne Hackborn8cf1bcd2010-03-16 13:06:10 -07004518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 }
Alan Viverette505e3ab2014-11-24 15:22:11 -08004520
lumark0b05f9e2018-11-26 15:09:06 +08004521 final ActivityThread currentThread = ActivityThread.currentActivityThread();
Andrew Sapperstein8a3b4cb2017-04-28 14:35:31 -07004522 final Context settingsContext = new ContextThemeWrapper(
lumark0b05f9e2018-11-26 15:09:06 +08004523 displayId == DEFAULT_DISPLAY ? currentThread.getSystemUiContext()
4524 : currentThread.createSystemUiContext(displayId),
Alan Viverette505e3ab2014-11-24 15:22:11 -08004525 com.android.internal.R.style.Theme_DeviceDefault_Settings);
4526
4527 mDialogBuilder = new AlertDialog.Builder(settingsContext);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004528 mDialogBuilder.setOnCancelListener(new OnCancelListener() {
4529 @Override
4530 public void onCancel(DialogInterface dialog) {
4531 hideInputMethodMenu();
4532 }
4533 });
Alan Viverette505e3ab2014-11-24 15:22:11 -08004534
4535 final Context dialogContext = mDialogBuilder.getContext();
4536 final TypedArray a = dialogContext.obtainStyledAttributes(null,
4537 com.android.internal.R.styleable.DialogPreference,
4538 com.android.internal.R.attr.alertDialogStyle, 0);
4539 final Drawable dialogIcon = a.getDrawable(
4540 com.android.internal.R.styleable.DialogPreference_dialogIcon);
4541 a.recycle();
4542
4543 mDialogBuilder.setIcon(dialogIcon);
4544
Yohei Yukawad34e1482016-02-11 08:03:52 -08004545 final LayoutInflater inflater = dialogContext.getSystemService(LayoutInflater.class);
satok01038492012-04-09 21:08:27 +09004546 final View tv = inflater.inflate(
4547 com.android.internal.R.layout.input_method_switch_dialog_title, null);
4548 mDialogBuilder.setCustomTitle(tv);
4549
4550 // Setup layout for a toggle switch of the hardware keyboard
4551 mSwitchingDialogTitleView = tv;
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004552 mSwitchingDialogTitleView
4553 .findViewById(com.android.internal.R.id.hard_keyboard_section)
Seigo Nonaka7309b122015-08-17 18:34:13 -07004554 .setVisibility(mWindowManagerInternal.isHardKeyboardAvailable()
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004555 ? View.VISIBLE : View.GONE);
Alan Viverette505e3ab2014-11-24 15:22:11 -08004556 final Switch hardKeySwitch = (Switch) mSwitchingDialogTitleView.findViewById(
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004557 com.android.internal.R.id.hard_keyboard_switch);
Michael Wright7b5a96b2014-08-09 19:28:42 -07004558 hardKeySwitch.setChecked(mShowImeWithHardKeyboard);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004559 hardKeySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
4560 @Override
4561 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Michael Wright7b5a96b2014-08-09 19:28:42 -07004562 mSettings.setShowImeWithHardKeyboard(isChecked);
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004563 // Ensure that the input method dialog is dismissed when changing
4564 // the hardware keyboard state.
4565 hideInputMethodMenu();
4566 }
4567 });
4568
Alan Viverette505e3ab2014-11-24 15:22:11 -08004569 final ImeSubtypeListAdapter adapter = new ImeSubtypeListAdapter(dialogContext,
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004570 com.android.internal.R.layout.input_method_switch_item, imList, checkedItem);
4571 final OnClickListener choiceListener = new OnClickListener() {
4572 @Override
4573 public void onClick(final DialogInterface dialog, final int which) {
4574 synchronized (mMethodMap) {
4575 if (mIms == null || mIms.length <= which || mSubtypeIds == null
4576 || mSubtypeIds.length <= which) {
4577 return;
satok01038492012-04-09 21:08:27 +09004578 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004579 final InputMethodInfo im = mIms[which];
4580 int subtypeId = mSubtypeIds[which];
4581 adapter.mCheckedItem = which;
4582 adapter.notifyDataSetChanged();
4583 hideInputMethodMenu();
4584 if (im != null) {
4585 if (subtypeId < 0 || subtypeId >= im.getSubtypeCount()) {
4586 subtypeId = NOT_A_SUBTYPE_ID;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08004587 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004588 setInputMethodLocked(im.getId(), subtypeId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 }
Tadashi G. Takaokad130b802014-08-01 14:05:47 +09004590 }
4591 }
4592 };
4593 mDialogBuilder.setSingleChoiceItems(adapter, checkedItem, choiceListener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 mSwitchingDialog = mDialogBuilder.create();
Dianne Hackborne3a7f622011-03-03 21:48:24 -08004596 mSwitchingDialog.setCanceledOnTouchOutside(true);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004597 final Window w = mSwitchingDialog.getWindow();
Yohei Yukawa6e875592019-01-28 00:49:30 -08004598 final LayoutParams attrs = w.getAttributes();
4599 w.setType(LayoutParams.TYPE_INPUT_METHOD_DIALOG);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004600 // Use an alternate token for the dialog for that window manager can group the token
4601 // with other IME windows based on type vs. grouping based on whichever token happens
4602 // to get selected by the system later on.
4603 attrs.token = mSwitchingDialogToken;
Roshan Piusa3f89c62019-10-11 08:50:53 -07004604 attrs.privateFlags |= LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS;
Wale Ogunwale3a931692016-11-02 16:49:48 -07004605 attrs.setTitle("Select input method");
4606 w.setAttributes(attrs);
Yohei Yukawa849443c2019-01-21 09:02:25 -08004607 updateSystemUiLocked(mImeWindowVis, mBackDisposition);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 mSwitchingDialog.show();
4609 }
4610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004611
Ken Wakasa05dbb652011-08-22 15:22:43 +09004612 private static class ImeSubtypeListAdapter extends ArrayAdapter<ImeSubtypeListItem> {
4613 private final LayoutInflater mInflater;
4614 private final int mTextViewResourceId;
4615 private final List<ImeSubtypeListItem> mItemsList;
Satoshi Kataokad2142962012-11-12 18:43:06 +09004616 public int mCheckedItem;
Ken Wakasa05dbb652011-08-22 15:22:43 +09004617 public ImeSubtypeListAdapter(Context context, int textViewResourceId,
4618 List<ImeSubtypeListItem> itemsList, int checkedItem) {
4619 super(context, textViewResourceId, itemsList);
Alan Viverette505e3ab2014-11-24 15:22:11 -08004620
Ken Wakasa05dbb652011-08-22 15:22:43 +09004621 mTextViewResourceId = textViewResourceId;
4622 mItemsList = itemsList;
4623 mCheckedItem = checkedItem;
Yohei Yukawad34e1482016-02-11 08:03:52 -08004624 mInflater = context.getSystemService(LayoutInflater.class);
Ken Wakasa05dbb652011-08-22 15:22:43 +09004625 }
4626
4627 @Override
4628 public View getView(int position, View convertView, ViewGroup parent) {
4629 final View view = convertView != null ? convertView
4630 : mInflater.inflate(mTextViewResourceId, null);
4631 if (position < 0 || position >= mItemsList.size()) return view;
4632 final ImeSubtypeListItem item = mItemsList.get(position);
4633 final CharSequence imeName = item.mImeName;
4634 final CharSequence subtypeName = item.mSubtypeName;
4635 final TextView firstTextView = (TextView)view.findViewById(android.R.id.text1);
4636 final TextView secondTextView = (TextView)view.findViewById(android.R.id.text2);
4637 if (TextUtils.isEmpty(subtypeName)) {
4638 firstTextView.setText(imeName);
4639 secondTextView.setVisibility(View.GONE);
4640 } else {
4641 firstTextView.setText(subtypeName);
4642 secondTextView.setText(imeName);
4643 secondTextView.setVisibility(View.VISIBLE);
4644 }
4645 final RadioButton radioButton =
4646 (RadioButton)view.findViewById(com.android.internal.R.id.radio);
4647 radioButton.setChecked(position == mCheckedItem);
4648 return view;
4649 }
4650 }
4651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 void hideInputMethodMenu() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004653 synchronized (mMethodMap) {
4654 hideInputMethodMenuLocked();
4655 }
4656 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004657
The Android Open Source Project10592532009-03-18 17:39:46 -07004658 void hideInputMethodMenuLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004659 if (DEBUG) Slog.v(TAG, "Hide switching menu");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660
The Android Open Source Project10592532009-03-18 17:39:46 -07004661 if (mSwitchingDialog != null) {
4662 mSwitchingDialog.dismiss();
4663 mSwitchingDialog = null;
Yohei Yukawa1fc7a1d2018-04-18 17:31:27 -07004664 mSwitchingDialogTitleView = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004666
Yohei Yukawa849443c2019-01-21 09:02:25 -08004667 updateSystemUiLocked(mImeWindowVis, mBackDisposition);
The Android Open Source Project10592532009-03-18 17:39:46 -07004668 mDialogBuilder = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004669 mIms = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004670 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004672 // ----------------------------------------------------------------------
Doug Zongkerab5c49c2009-12-04 10:31:43 -08004673
Yohei Yukawa3d3abd22019-04-09 23:20:12 -07004674 /**
4675 * Enable or disable the given IME by updating {@link Settings.Secure#ENABLED_INPUT_METHODS}.
4676 *
4677 * @param id ID of the IME is to be manipulated. It is OK to pass IME ID that is currently not
4678 * recognized by the system.
4679 * @param enabled {@code true} if {@code id} needs to be enabled.
4680 * @return {@code true} if the IME was previously enabled. {@code false} otherwise.
4681 */
4682 private boolean setInputMethodEnabledLocked(String id, boolean enabled) {
satokd87c2592010-09-29 11:52:06 +09004683 List<Pair<String, ArrayList<String>>> enabledInputMethodsList = mSettings
4684 .getEnabledInputMethodsAndSubtypeListLocked();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004685
satokd87c2592010-09-29 11:52:06 +09004686 if (enabled) {
4687 for (Pair<String, ArrayList<String>> pair: enabledInputMethodsList) {
4688 if (pair.first.equals(id)) {
4689 // We are enabling this input method, but it is already enabled.
4690 // Nothing to do. The previous state was enabled.
4691 return true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004692 }
4693 }
satokd87c2592010-09-29 11:52:06 +09004694 mSettings.appendAndPutEnabledInputMethodLocked(id, false);
4695 // Previous state was disabled.
4696 return false;
4697 } else {
4698 StringBuilder builder = new StringBuilder();
4699 if (mSettings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
4700 builder, enabledInputMethodsList, id)) {
4701 // Disabled input method is currently selected, switch to another one.
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004702 final String selId = mSettings.getSelectedInputMethod();
satok03eb319a2010-11-11 18:17:42 +09004703 if (id.equals(selId) && !chooseNewDefaultIMELocked()) {
4704 Slog.i(TAG, "Can't find new IME, unsetting the current input method.");
4705 resetSelectedInputMethodAndSubtypeLocked("");
satokd87c2592010-09-29 11:52:06 +09004706 }
4707 // Previous state was enabled.
4708 return true;
4709 } else {
4710 // We are disabling the input method but it is already disabled.
4711 // Nothing to do. The previous state was disabled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004712 return false;
4713 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004714 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004715 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08004716
satok723a27e2010-11-11 14:58:11 +09004717 private void setSelectedInputMethodAndSubtypeLocked(InputMethodInfo imi, int subtypeId,
4718 boolean setSubtypeOnly) {
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08004719 mSettings.saveCurrentInputMethodAndSubtypeToHistory(mCurMethodId, mCurrentSubtype);
Tarandeep Singh89a6c482017-11-21 14:26:11 -08004720
satok723a27e2010-11-11 14:58:11 +09004721 // Set Subtype here
4722 if (imi == null || subtypeId < 0) {
4723 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
Tadashi G. Takaoka0ba75bb2010-11-09 12:19:32 -08004724 mCurrentSubtype = null;
satok723a27e2010-11-11 14:58:11 +09004725 } else {
Ken Wakasa586f0512011-01-20 22:31:01 +09004726 if (subtypeId < imi.getSubtypeCount()) {
4727 InputMethodSubtype subtype = imi.getSubtypeAt(subtypeId);
4728 mSettings.putSelectedSubtype(subtype.hashCode());
4729 mCurrentSubtype = subtype;
satok723a27e2010-11-11 14:58:11 +09004730 } else {
4731 mSettings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
satokd81e9502012-05-21 12:58:45 +09004732 // If the subtype is not specified, choose the most applicable one
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004733 mCurrentSubtype = getCurrentInputMethodSubtypeLocked();
satok723a27e2010-11-11 14:58:11 +09004734 }
satokab751aa2010-09-14 19:17:36 +09004735 }
satok723a27e2010-11-11 14:58:11 +09004736
Yohei Yukawa68645a62016-02-17 07:54:20 -08004737 if (!setSubtypeOnly) {
satok723a27e2010-11-11 14:58:11 +09004738 // Set InputMethod here
4739 mSettings.putSelectedInputMethod(imi != null ? imi.getId() : "");
4740 }
4741 }
4742
4743 private void resetSelectedInputMethodAndSubtypeLocked(String newDefaultIme) {
4744 InputMethodInfo imi = mMethodMap.get(newDefaultIme);
4745 int lastSubtypeId = NOT_A_SUBTYPE_ID;
4746 // newDefaultIme is empty when there is no candidate for the selected IME.
4747 if (imi != null && !TextUtils.isEmpty(newDefaultIme)) {
4748 String subtypeHashCode = mSettings.getLastSubtypeForInputMethodLocked(newDefaultIme);
4749 if (subtypeHashCode != null) {
4750 try {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004751 lastSubtypeId = InputMethodUtils.getSubtypeIdFromHashCode(
Narayan Kamatha09b4d22016-04-15 18:32:45 +01004752 imi, Integer.parseInt(subtypeHashCode));
satok723a27e2010-11-11 14:58:11 +09004753 } catch (NumberFormatException e) {
4754 Slog.w(TAG, "HashCode for subtype looks broken: " + subtypeHashCode, e);
4755 }
4756 }
4757 }
4758 setSelectedInputMethodAndSubtypeLocked(imi, lastSubtypeId, false);
satokab751aa2010-09-14 19:17:36 +09004759 }
4760
satokab751aa2010-09-14 19:17:36 +09004761 /**
4762 * @return Return the current subtype of this input method.
4763 */
satok42c5a162011-05-26 16:46:14 +09004764 @Override
satokab751aa2010-09-14 19:17:36 +09004765 public InputMethodSubtype getCurrentInputMethodSubtype() {
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004766 synchronized (mMethodMap) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08004767 // TODO: Make this work even for non-current users?
Yohei Yukawa46d74762019-01-22 10:17:22 -08004768 if (!calledFromValidUserLocked()) {
Yohei Yukawa91e6cd02018-12-09 22:58:57 -08004769 return null;
4770 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004771 return getCurrentInputMethodSubtypeLocked();
4772 }
4773 }
4774
4775 private InputMethodSubtype getCurrentInputMethodSubtypeLocked() {
satokfdf419e2012-05-08 16:52:08 +09004776 if (mCurMethodId == null) {
4777 return null;
4778 }
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004779 final boolean subtypeIsSelected = mSettings.isSubtypeSelected();
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004780 final InputMethodInfo imi = mMethodMap.get(mCurMethodId);
4781 if (imi == null || imi.getSubtypeCount() == 0) {
4782 return null;
satok4e4569d2010-11-19 18:45:53 +09004783 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004784 if (!subtypeIsSelected || mCurrentSubtype == null
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004785 || !InputMethodUtils.isValidSubtypeId(imi, mCurrentSubtype.hashCode())) {
4786 int subtypeId = mSettings.getSelectedInputMethodSubtypeId(mCurMethodId);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004787 if (subtypeId == NOT_A_SUBTYPE_ID) {
4788 // If there are no selected subtypes, the framework will try to find
4789 // the most applicable subtype from explicitly or implicitly enabled
4790 // subtypes.
4791 List<InputMethodSubtype> explicitlyOrImplicitlyEnabledSubtypes =
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004792 mSettings.getEnabledInputMethodSubtypeListLocked(mContext, imi, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004793 // If there is only one explicitly or implicitly enabled subtype,
4794 // just returns it.
4795 if (explicitlyOrImplicitlyEnabledSubtypes.size() == 1) {
4796 mCurrentSubtype = explicitlyOrImplicitlyEnabledSubtypes.get(0);
4797 } else if (explicitlyOrImplicitlyEnabledSubtypes.size() > 1) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004798 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004799 mRes, explicitlyOrImplicitlyEnabledSubtypes,
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004800 InputMethodUtils.SUBTYPE_MODE_KEYBOARD, null, true);
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004801 if (mCurrentSubtype == null) {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004802 mCurrentSubtype = InputMethodUtils.findLastResortApplicableSubtypeLocked(
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004803 mRes, explicitlyOrImplicitlyEnabledSubtypes, null, null,
4804 true);
satok4e4569d2010-11-19 18:45:53 +09004805 }
satok3ef8b292010-11-23 06:06:29 +09004806 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004807 } else {
Satoshi Kataoka8e303cc2013-01-11 15:55:28 +09004808 mCurrentSubtype = InputMethodUtils.getSubtypes(imi).get(subtypeId);
satok8fbb1e82010-11-02 23:15:58 +09004809 }
4810 }
Satoshi Kataoka4e1ab152012-09-13 21:34:20 +09004811 return mCurrentSubtype;
satokab751aa2010-09-14 19:17:36 +09004812 }
4813
Yohei Yukawaa878b952019-01-10 19:36:24 -08004814 private List<InputMethodInfo> getInputMethodListAsUser(@UserIdInt int userId) {
4815 synchronized (mMethodMap) {
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08004816 return getInputMethodListLocked(userId);
Yohei Yukawaa878b952019-01-10 19:36:24 -08004817 }
4818 }
4819
4820 private List<InputMethodInfo> getEnabledInputMethodListAsUser(@UserIdInt int userId) {
4821 synchronized (mMethodMap) {
4822 return getEnabledInputMethodListLocked(userId);
4823 }
4824 }
4825
Feng Cao16b2de52020-01-09 17:27:27 -08004826 private void onCreateInlineSuggestionsRequest(@UserIdInt int userId,
Feng Cao36960ee2020-02-18 18:23:30 -08004827 InlineSuggestionsRequestInfo requestInfo,
Feng Cao16b2de52020-01-09 17:27:27 -08004828 IInlineSuggestionsRequestCallback callback) {
Adam Hebc67f2e2019-11-13 14:34:56 -08004829 synchronized (mMethodMap) {
Feng Cao36960ee2020-02-18 18:23:30 -08004830 onCreateInlineSuggestionsRequestLocked(userId, requestInfo, callback);
Adam Hebc67f2e2019-11-13 14:34:56 -08004831 }
4832 }
4833
mincheli850892b2019-12-05 19:47:59 +08004834 private boolean switchToInputMethod(String imeId, @UserIdInt int userId) {
4835 synchronized (mMethodMap) {
4836 if (userId == mSettings.getCurrentUserId()) {
4837 if (!mMethodMap.containsKey(imeId)
4838 || !mSettings.getEnabledInputMethodListLocked()
4839 .contains(mMethodMap.get(imeId))) {
4840 return false; // IME is not is found or not enabled.
4841 }
4842 setInputMethodLocked(imeId, NOT_A_SUBTYPE_ID);
4843 return true;
4844 }
4845 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
4846 final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
4847 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
4848 new ArrayMap<>();
4849 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
4850 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap,
4851 methodMap, methodList);
4852 final InputMethodSettings settings = new InputMethodSettings(
4853 mContext.getResources(), mContext.getContentResolver(), methodMap,
4854 userId, false);
4855 if (!methodMap.containsKey(imeId)
4856 || !settings.getEnabledInputMethodListLocked()
4857 .contains(methodMap.get(imeId))) {
4858 return false; // IME is not is found or not enabled.
4859 }
4860 settings.putSelectedInputMethod(imeId);
4861 settings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
4862 return true;
4863 }
4864 }
4865
lpeter133fce02020-03-05 20:32:16 +08004866 private boolean transferTouchFocusToImeWindow(@NonNull IBinder sourceInputToken,
4867 int displayId) {
4868 //TODO(b/150843766): Check if Input Token is valid.
4869 final IBinder curHostInputToken;
4870 synchronized (mMethodMap) {
4871 if (displayId != mCurTokenDisplayId || mCurHostInputToken == null) {
4872 return false;
4873 }
4874 curHostInputToken = mCurHostInputToken;
4875 }
4876 return mInputManagerInternal.transferTouchFocus(sourceInputToken, curHostInputToken);
4877 }
4878
Yohei Yukawae24ed792018-08-28 19:10:32 -07004879 private static final class LocalServiceImpl extends InputMethodManagerInternal {
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004880 @NonNull
Yohei Yukawafffc0e52018-09-04 13:24:00 -07004881 private final InputMethodManagerService mService;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004882
Yohei Yukawafffc0e52018-09-04 13:24:00 -07004883 LocalServiceImpl(@NonNull InputMethodManagerService service) {
4884 mService = service;
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004885 }
4886
4887 @Override
lumarkd85e1582019-12-29 20:20:41 +08004888 public void hideCurrentInputMethod(@SoftInputShowHideReason int reason) {
Yohei Yukawaf16abb72018-09-05 11:28:42 -07004889 mService.mHandler.removeMessages(MSG_HIDE_CURRENT_INPUT_METHOD);
lumarkd85e1582019-12-29 20:20:41 +08004890 mService.mHandler.obtainMessage(MSG_HIDE_CURRENT_INPUT_METHOD, reason).sendToTarget();
Jorim Jaggi3c5d0f12016-05-24 19:04:30 -07004891 }
Tarandeep Singh89a6c482017-11-21 14:26:11 -08004892
4893 @Override
Yohei Yukawaa878b952019-01-10 19:36:24 -08004894 public List<InputMethodInfo> getInputMethodListAsUser(int userId) {
4895 return mService.getInputMethodListAsUser(userId);
4896 }
4897
4898 @Override
4899 public List<InputMethodInfo> getEnabledInputMethodListAsUser(int userId) {
4900 return mService.getEnabledInputMethodListAsUser(userId);
4901 }
Adam Hebc67f2e2019-11-13 14:34:56 -08004902
4903 @Override
Feng Cao36960ee2020-02-18 18:23:30 -08004904 public void onCreateInlineSuggestionsRequest(int userId,
4905 InlineSuggestionsRequestInfo requestInfo, IInlineSuggestionsRequestCallback cb) {
4906 mService.onCreateInlineSuggestionsRequest(userId, requestInfo, cb);
Adam Hebc67f2e2019-11-13 14:34:56 -08004907 }
mincheli850892b2019-12-05 19:47:59 +08004908
4909 @Override
4910 public boolean switchToInputMethod(String imeId, int userId) {
4911 return mService.switchToInputMethod(imeId, userId);
4912 }
Yuichiro Hanada34c4c0e2020-01-15 16:53:07 +09004913
4914 @Override
4915 public void registerInputMethodListListener(InputMethodListListener listener) {
4916 mService.mInputMethodListListeners.addIfAbsent(listener);
4917 }
lpeter133fce02020-03-05 20:32:16 +08004918
4919 @Override
4920 public boolean transferTouchFocusToImeWindow(@NonNull IBinder sourceInputToken,
4921 int displayId) {
4922 return mService.transferTouchFocusToImeWindow(sourceInputToken, displayId);
4923 }
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07004924 }
4925
Yohei Yukawac54c1172018-09-06 11:39:50 -07004926 @BinderThread
4927 private IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
Yohei Yukawa25e08132016-06-22 16:31:41 -07004928 @Nullable Uri contentUri, @Nullable String packageName) {
Yohei Yukawa25e08132016-06-22 16:31:41 -07004929 if (token == null) {
4930 throw new NullPointerException("token");
4931 }
4932 if (packageName == null) {
4933 throw new NullPointerException("packageName");
4934 }
4935 if (contentUri == null) {
4936 throw new NullPointerException("contentUri");
4937 }
4938 final String contentUriScheme = contentUri.getScheme();
4939 if (!"content".equals(contentUriScheme)) {
4940 throw new InvalidParameterException("contentUri must have content scheme");
4941 }
4942
4943 synchronized (mMethodMap) {
4944 final int uid = Binder.getCallingUid();
4945 if (mCurMethodId == null) {
4946 return null;
4947 }
4948 if (mCurToken != token) {
4949 Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
4950 + " token=" + token);
4951 return null;
4952 }
4953 // We cannot simply distinguish a bad IME that reports an arbitrary package name from
4954 // an unfortunate IME whose internal state is already obsolete due to the asynchronous
4955 // nature of our system. Let's compare it with our internal record.
4956 if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
4957 Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
4958 + mCurAttribute.packageName + " packageName=" + packageName);
4959 return null;
4960 }
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004961 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004962 final int imeUserId = UserHandle.getUserId(uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004963 // This user ID can never bee spoofed.
Yohei Yukawa25e08132016-06-22 16:31:41 -07004964 final int appUserId = UserHandle.getUserId(mCurClient.uid);
Yohei Yukawa3933a6e2016-11-10 00:47:48 -08004965 // This user ID may be invalid if "contentUri" embedded an invalid user ID.
4966 final int contentUriOwnerUserId = ContentProvider.getUserIdFromUri(contentUri,
4967 imeUserId);
4968 final Uri contentUriWithoutUserId = ContentProvider.getUriWithoutUserId(contentUri);
4969 // Note: InputContentUriTokenHandler.take() checks whether the IME (specified by "uid")
4970 // actually has the right to grant a read permission for "contentUriWithoutUserId" that
4971 // is claimed to belong to "contentUriOwnerUserId". For example, specifying random
4972 // content URI and/or contentUriOwnerUserId just results in a SecurityException thrown
4973 // from InputContentUriTokenHandler.take() and can never be allowed beyond what is
4974 // actually allowed to "uid", which is guaranteed to be the IME's one.
4975 return new InputContentUriTokenHandler(contentUriWithoutUserId, uid,
4976 packageName, contentUriOwnerUserId, appUserId);
Yohei Yukawa25e08132016-06-22 16:31:41 -07004977 }
4978 }
4979
Yohei Yukawac54c1172018-09-06 11:39:50 -07004980 @BinderThread
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08004981 private void reportFullscreenMode(@NonNull IBinder token, boolean fullscreen) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004982 synchronized (mMethodMap) {
Yohei Yukawa79e4c6a2018-12-09 19:07:17 -08004983 if (!calledWithValidTokenLocked(token)) {
Yohei Yukawa2bc66172017-02-08 11:13:25 -08004984 return;
4985 }
4986 if (mCurClient != null && mCurClient.client != null) {
4987 mInFullscreenMode = fullscreen;
4988 executeOrSendMessage(mCurClient.client, mCaller.obtainMessageIO(
4989 MSG_REPORT_FULLSCREEN_MODE, fullscreen ? 1 : 0, mCurClient));
4990 }
4991 }
4992 }
4993
4994 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06004996 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997
4998 IInputMethod method;
4999 ClientState client;
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08005000 ClientState focusedWindowClient;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 final Printer p = new PrintWriterPrinter(pw);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 synchronized (mMethodMap) {
5005 p.println("Current Input Method Manager state:");
5006 int N = mMethodList.size();
Yohei Yukawa5ec1fce2018-12-17 07:56:14 -08005007 p.println(" Input Methods: mMethodMapUpdateCount=" + mMethodMapUpdateCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 for (int i=0; i<N; i++) {
5009 InputMethodInfo info = mMethodList.get(i);
5010 p.println(" InputMethod #" + i + ":");
5011 info.dump(p, " ");
5012 }
5013 p.println(" Clients:");
Yohei Yukawaac9311e2018-11-20 19:25:23 -08005014 final int numClients = mClients.size();
5015 for (int i = 0; i < numClients; ++i) {
5016 final ClientState ci = mClients.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005017 p.println(" Client " + ci + ":");
5018 p.println(" client=" + ci.client);
5019 p.println(" inputContext=" + ci.inputContext);
5020 p.println(" sessionRequested=" + ci.sessionRequested);
5021 p.println(" curSession=" + ci.curSession);
5022 }
The Android Open Source Project10592532009-03-18 17:39:46 -07005023 p.println(" mCurMethodId=" + mCurMethodId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024 client = mCurClient;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005025 p.println(" mCurClient=" + client + " mCurSeq=" + mCurSeq);
Yohei Yukawa22a89232017-02-12 16:38:59 -08005026 p.println(" mCurFocusedWindow=" + mCurFocusedWindow
5027 + " softInputMode=" +
Yohei Yukawaa468d702018-10-21 11:42:34 -07005028 InputMethodDebug.softInputModeToString(mCurFocusedWindowSoftInputMode)
Yohei Yukawa22a89232017-02-12 16:38:59 -08005029 + " client=" + mCurFocusedWindowClient);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08005030 focusedWindowClient = mCurFocusedWindowClient;
Yohei Yukawad3ef8422018-06-07 16:28:07 -07005031 p.println(" mCurId=" + mCurId + " mHaveConnection=" + mHaveConnection
5032 + " mBoundToMethod=" + mBoundToMethod + " mVisibleBound=" + mVisibleBound);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 p.println(" mCurToken=" + mCurToken);
lumark90120a82018-08-15 00:33:03 +08005034 p.println(" mCurTokenDisplayId=" + mCurTokenDisplayId);
lpeter133fce02020-03-05 20:32:16 +08005035 p.println(" mCurHostInputToken=" + mCurHostInputToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 p.println(" mCurIntent=" + mCurIntent);
5037 method = mCurMethod;
5038 p.println(" mCurMethod=" + mCurMethod);
5039 p.println(" mEnabledSession=" + mEnabledSession);
5040 p.println(" mShowRequested=" + mShowRequested
5041 + " mShowExplicitlyRequested=" + mShowExplicitlyRequested
5042 + " mShowForced=" + mShowForced
5043 + " mInputShown=" + mInputShown);
Yohei Yukawa2bc66172017-02-08 11:13:25 -08005044 p.println(" mInFullscreenMode=" + mInFullscreenMode);
Yohei Yukawafa6e0a82015-07-23 15:08:59 -07005045 p.println(" mSystemReady=" + mSystemReady + " mInteractive=" + mIsInteractive);
Yohei Yukawa81482972015-06-04 00:58:59 -07005046 p.println(" mSettingsObserver=" + mSettingsObserver);
Yohei Yukawad7248862015-06-03 23:56:12 -07005047 p.println(" mSwitchingController:");
5048 mSwitchingController.dump(p);
Yohei Yukawa68645a62016-02-17 07:54:20 -08005049 p.println(" mSettings:");
5050 mSettings.dumpLocked(p, " ");
Yohei Yukawa357b2f62017-02-14 09:40:03 -08005051
5052 p.println(" mStartInputHistory:");
5053 mStartInputHistory.dump(pw, " ");
lumarkd85e1582019-12-29 20:20:41 +08005054
5055 p.println(" mSoftInputShowHideHistory:");
5056 mSoftInputShowHideHistory.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005058
Jeff Brownb88102f2010-09-08 11:49:43 -07005059 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005060 if (client != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 pw.flush();
5062 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07005063 TransferPipe.dumpAsync(client.client.asBinder(), fd, args);
5064 } catch (IOException | RemoteException e) {
5065 p.println("Failed to dump input method client: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 }
Jeff Brownb88102f2010-09-08 11:49:43 -07005067 } else {
5068 p.println("No input method client.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08005070
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08005071 if (focusedWindowClient != null && client != focusedWindowClient) {
5072 p.println(" ");
5073 p.println("Warning: Current input method client doesn't match the last focused. "
5074 + "window.");
5075 p.println("Dumping input method client in the last focused window just in case.");
5076 p.println(" ");
5077 pw.flush();
5078 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07005079 TransferPipe.dumpAsync(focusedWindowClient.client.asBinder(), fd, args);
5080 } catch (IOException | RemoteException e) {
5081 p.println("Failed to dump input method client in focused window: " + e);
Yohei Yukawae39d4ed2015-11-19 03:38:49 -08005082 }
5083 }
5084
Jeff Brownb88102f2010-09-08 11:49:43 -07005085 p.println(" ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 if (method != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087 pw.flush();
5088 try {
Jeff Sharkey850c83e2016-11-09 12:25:44 -07005089 TransferPipe.dumpAsync(method.asBinder(), fd, args);
5090 } catch (IOException | RemoteException e) {
5091 p.println("Failed to dump input method service: " + e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
Jeff Brownb88102f2010-09-08 11:49:43 -07005093 } else {
5094 p.println("No input method service.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 }
5096 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005097
5098 @BinderThread
5099 @Override
5100 public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
5101 @Nullable FileDescriptor err,
5102 @NonNull String[] args, @Nullable ShellCallback callback,
5103 @NonNull ResultReceiver resultReceiver) throws RemoteException {
Yohei Yukawab8d240f2018-12-26 10:03:11 -08005104 final int callingUid = Binder.getCallingUid();
5105 // Reject any incoming calls from non-shell users, including ones from the system user.
5106 if (callingUid != Process.ROOT_UID && callingUid != Process.SHELL_UID) {
5107 // Note that Binder#onTransact() will automatically close "in", "out", and "err" when
5108 // returned from this method, hence there is no need to close those FDs.
5109 // "resultReceiver" is the only thing that needs to be taken care of here.
5110 if (resultReceiver != null) {
5111 resultReceiver.send(ShellCommandResult.FAILURE, null);
5112 }
5113 final String errorMsg = "InputMethodManagerService does not support shell commands from"
5114 + " non-shell users. callingUid=" + callingUid
5115 + " args=" + Arrays.toString(args);
5116 if (Process.isCoreUid(callingUid)) {
5117 // Let's not crash the calling process if the caller is one of core components.
5118 Slog.e(TAG, errorMsg);
5119 return;
5120 }
5121 throw new SecurityException(errorMsg);
5122 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005123 new ShellCommandImpl(this).exec(
5124 this, in, out, err, args, callback, resultReceiver);
5125 }
5126
5127 private static final class ShellCommandImpl extends ShellCommand {
5128 @NonNull
5129 final InputMethodManagerService mService;
5130
5131 ShellCommandImpl(InputMethodManagerService service) {
5132 mService = service;
5133 }
5134
Yohei Yukawadb25df72018-12-27 08:40:41 -08005135 @RequiresPermission(allOf = {
Yohei Yukawa8a2b96b2018-12-28 17:23:29 -08005136 Manifest.permission.DUMP,
5137 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
Yohei Yukawadb25df72018-12-27 08:40:41 -08005138 Manifest.permission.WRITE_SECURE_SETTINGS,
Yohei Yukawa8a2b96b2018-12-28 17:23:29 -08005139 })
Yohei Yukawa926488d2017-12-11 17:24:55 -08005140 @BinderThread
5141 @ShellCommandResult
5142 @Override
5143 public int onCommand(@Nullable String cmd) {
Yohei Yukawadb25df72018-12-27 08:40:41 -08005144 // For shell command, require all the permissions here in favor of code simplicity.
Yohei Yukawa8a2b96b2018-12-28 17:23:29 -08005145 Arrays.asList(
5146 Manifest.permission.DUMP,
5147 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
5148 Manifest.permission.WRITE_SECURE_SETTINGS
5149 ).forEach(permission -> mService.mContext.enforceCallingPermission(permission, null));
Yohei Yukawadb25df72018-12-27 08:40:41 -08005150
5151 final long identity = Binder.clearCallingIdentity();
5152 try {
5153 return onCommandWithSystemIdentity(cmd);
5154 } finally {
5155 Binder.restoreCallingIdentity(identity);
5156 }
5157 }
5158
5159 @BinderThread
5160 @ShellCommandResult
5161 private int onCommandWithSystemIdentity(@Nullable String cmd) {
Yohei Yukawadfdab732018-02-21 07:16:30 +09005162 if ("refresh_debug_properties".equals(cmd)) {
5163 return refreshDebugProperties();
5164 }
5165
Yohei Yukawaa7babbb2019-01-08 16:45:34 -08005166 if ("get-last-switch-user-id".equals(cmd)) {
5167 return mService.getLastSwitchUserId(this);
5168 }
5169
Yohei Yukawacac97722017-12-15 16:52:05 -08005170 // For existing "adb shell ime <command>".
5171 if ("ime".equals(cmd)) {
5172 final String imeCommand = getNextArg();
5173 if (imeCommand == null || "help".equals(imeCommand) || "-h".equals(imeCommand)) {
5174 onImeCommandHelp();
5175 return ShellCommandResult.SUCCESS;
5176 }
5177 switch (imeCommand) {
5178 case "list":
5179 return mService.handleShellCommandListInputMethods(this);
5180 case "enable":
5181 return mService.handleShellCommandEnableDisableInputMethod(this, true);
5182 case "disable":
5183 return mService.handleShellCommandEnableDisableInputMethod(this, false);
5184 case "set":
5185 return mService.handleShellCommandSetInputMethod(this);
5186 case "reset":
5187 return mService.handleShellCommandResetInputMethod(this);
5188 default:
5189 getOutPrintWriter().println("Unknown command: " + imeCommand);
5190 return ShellCommandResult.FAILURE;
5191 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005192 }
Yohei Yukawacac97722017-12-15 16:52:05 -08005193
5194 return handleDefaultCommands(cmd);
Yohei Yukawa926488d2017-12-11 17:24:55 -08005195 }
5196
5197 @BinderThread
Tarandeep Singh75a92392018-01-12 14:58:59 -08005198 @ShellCommandResult
5199 private int refreshDebugProperties() {
5200 DebugFlags.FLAG_OPTIMIZE_START_INPUT.refresh();
Tarandeep Singheadb1392018-11-09 18:15:57 +01005201 DebugFlags.FLAG_PRE_RENDER_IME_VIEWS.refresh();
Tarandeep Singh75a92392018-01-12 14:58:59 -08005202 return ShellCommandResult.SUCCESS;
5203 }
5204
5205 @BinderThread
Yohei Yukawa926488d2017-12-11 17:24:55 -08005206 @Override
5207 public void onHelp() {
5208 try (PrintWriter pw = getOutPrintWriter()) {
5209 pw.println("InputMethodManagerService commands:");
5210 pw.println(" help");
5211 pw.println(" Prints this help text.");
5212 pw.println(" dump [options]");
5213 pw.println(" Synonym of dumpsys.");
Yohei Yukawacac97722017-12-15 16:52:05 -08005214 pw.println(" ime <command> [options]");
5215 pw.println(" Manipulate IMEs. Run \"ime help\" for details.");
5216 }
5217 }
5218
5219 private void onImeCommandHelp() {
5220 try (IndentingPrintWriter pw =
5221 new IndentingPrintWriter(getOutPrintWriter(), " ", 100)) {
5222 pw.println("ime <command>:");
5223 pw.increaseIndent();
5224
5225 pw.println("list [-a] [-s]");
5226 pw.increaseIndent();
5227 pw.println("prints all enabled input methods.");
5228 pw.increaseIndent();
5229 pw.println("-a: see all input methods");
5230 pw.println("-s: only a single summary line of each");
5231 pw.decreaseIndent();
5232 pw.decreaseIndent();
5233
Yohei Yukawae1771702019-04-10 23:20:51 -07005234 pw.println("enable [--user <USER_ID>] <ID>");
Yohei Yukawacac97722017-12-15 16:52:05 -08005235 pw.increaseIndent();
5236 pw.println("allows the given input method ID to be used.");
Yohei Yukawae1771702019-04-10 23:20:51 -07005237 pw.increaseIndent();
5238 pw.print("--user <USER_ID>: Specify which user to enable.");
5239 pw.println(" Assumes the current user if not specified.");
5240 pw.decreaseIndent();
Yohei Yukawacac97722017-12-15 16:52:05 -08005241 pw.decreaseIndent();
5242
Yohei Yukawae1771702019-04-10 23:20:51 -07005243 pw.println("disable [--user <USER_ID>] <ID>");
Yohei Yukawacac97722017-12-15 16:52:05 -08005244 pw.increaseIndent();
5245 pw.println("disallows the given input method ID to be used.");
Yohei Yukawae1771702019-04-10 23:20:51 -07005246 pw.increaseIndent();
5247 pw.print("--user <USER_ID>: Specify which user to disable.");
5248 pw.println(" Assumes the current user if not specified.");
5249 pw.decreaseIndent();
Yohei Yukawacac97722017-12-15 16:52:05 -08005250 pw.decreaseIndent();
5251
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005252 pw.println("set [--user <USER_ID>] <ID>");
Yohei Yukawacac97722017-12-15 16:52:05 -08005253 pw.increaseIndent();
5254 pw.println("switches to the given input method ID.");
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005255 pw.increaseIndent();
5256 pw.print("--user <USER_ID>: Specify which user to enable.");
5257 pw.println(" Assumes the current user if not specified.");
5258 pw.decreaseIndent();
Yohei Yukawacac97722017-12-15 16:52:05 -08005259 pw.decreaseIndent();
5260
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005261 pw.println("reset [--user <USER_ID>]");
Yohei Yukawacac97722017-12-15 16:52:05 -08005262 pw.increaseIndent();
5263 pw.println("reset currently selected/enabled IMEs to the default ones as if "
5264 + "the device is initially booted with the current locale.");
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005265 pw.increaseIndent();
5266 pw.print("--user <USER_ID>: Specify which user to reset.");
5267 pw.println(" Assumes the current user if not specified.");
5268 pw.decreaseIndent();
5269
Yohei Yukawacac97722017-12-15 16:52:05 -08005270 pw.decreaseIndent();
5271
5272 pw.decreaseIndent();
Yohei Yukawa926488d2017-12-11 17:24:55 -08005273 }
5274 }
5275 }
5276
5277 // ----------------------------------------------------------------------
5278 // Shell command handlers:
5279
Yohei Yukawaa7babbb2019-01-08 16:45:34 -08005280 @BinderThread
5281 @ShellCommandResult
5282 private int getLastSwitchUserId(@NonNull ShellCommand shellCommand) {
5283 synchronized (mMethodMap) {
5284 shellCommand.getOutPrintWriter().println(mLastSwitchUserId);
5285 return ShellCommandResult.SUCCESS;
5286 }
5287 }
5288
Yohei Yukawa926488d2017-12-11 17:24:55 -08005289 /**
5290 * Handles {@code adb shell ime list}.
5291 * @param shellCommand {@link ShellCommand} object that is handling this command.
5292 * @return Exit code of the command.
5293 */
5294 @BinderThread
5295 @ShellCommandResult
5296 private int handleShellCommandListInputMethods(@NonNull ShellCommand shellCommand) {
5297 boolean all = false;
5298 boolean brief = false;
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08005299 int userIdToBeResolved = UserHandle.USER_CURRENT;
Yohei Yukawa926488d2017-12-11 17:24:55 -08005300 while (true) {
5301 final String nextOption = shellCommand.getNextOption();
5302 if (nextOption == null) {
5303 break;
5304 }
5305 switch (nextOption) {
5306 case "-a":
5307 all = true;
5308 break;
5309 case "-s":
5310 brief = true;
5311 break;
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08005312 case "-u":
5313 case "--user":
5314 userIdToBeResolved = UserHandle.parseUserArg(shellCommand.getNextArgRequired());
5315 break;
Yohei Yukawa926488d2017-12-11 17:24:55 -08005316 }
5317 }
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08005318 synchronized (mMethodMap) {
5319 final PrintWriter pr = shellCommand.getOutPrintWriter();
5320 final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
5321 mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
5322 for (int userId : userIds) {
5323 final List<InputMethodInfo> methods = all
Yohei Yukawa2988d2d2019-01-22 21:29:09 -08005324 ? getInputMethodListLocked(userId)
Yohei Yukawa7f8ee4b2019-01-10 08:44:57 -08005325 : getEnabledInputMethodListLocked(userId);
5326 if (userIds.length > 1) {
5327 pr.print("User #");
5328 pr.print(userId);
5329 pr.println(":");
5330 }
5331 for (InputMethodInfo info : methods) {
5332 if (brief) {
5333 pr.println(info.getId());
5334 } else {
5335 pr.print(info.getId());
5336 pr.println(":");
5337 info.dump(pr::println, " ");
5338 }
5339 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005340 }
5341 }
5342 return ShellCommandResult.SUCCESS;
5343 }
5344
5345 /**
5346 * Handles {@code adb shell ime enable} and {@code adb shell ime disable}.
5347 * @param shellCommand {@link ShellCommand} object that is handling this command.
5348 * @param enabled {@code true} if the command was {@code adb shell ime enable}.
5349 * @return Exit code of the command.
5350 */
5351 @BinderThread
5352 @ShellCommandResult
Yohei Yukawa926488d2017-12-11 17:24:55 -08005353 private int handleShellCommandEnableDisableInputMethod(
5354 @NonNull ShellCommand shellCommand, boolean enabled) {
Yohei Yukawa3403f2d2019-04-15 08:16:58 -07005355 final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
Yohei Yukawae1771702019-04-10 23:20:51 -07005356 final String imeId = shellCommand.getNextArgRequired();
5357 final PrintWriter out = shellCommand.getOutPrintWriter();
5358 final PrintWriter error = shellCommand.getErrPrintWriter();
Yohei Yukawa926488d2017-12-11 17:24:55 -08005359 synchronized (mMethodMap) {
Yohei Yukawae1771702019-04-10 23:20:51 -07005360 final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
5361 mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
5362 for (int userId : userIds) {
5363 if (!userHasDebugPriv(userId, shellCommand)) {
5364 continue;
5365 }
5366 handleShellCommandEnableDisableInputMethodInternalLocked(userId, imeId, enabled,
5367 out, error);
Tarandeep Singh87e2e512019-03-06 14:28:52 -08005368 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005369 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005370 return ShellCommandResult.SUCCESS;
5371 }
5372
5373 /**
Yohei Yukawae1771702019-04-10 23:20:51 -07005374 * A special helper method for commands that only have {@code -u} and {@code --user} options.
5375 *
5376 * <p>You cannot use this helper method if the command has other options.</p>
5377 *
Yohei Yukawa3403f2d2019-04-15 08:16:58 -07005378 * <p>CAVEAT: This method must be called only once before any other
5379 * {@link ShellCommand#getNextArg()} and {@link ShellCommand#getNextArgRequired()} for the
5380 * main arguments.</p>
5381 *
Yohei Yukawae1771702019-04-10 23:20:51 -07005382 * @param shellCommand {@link ShellCommand} from which options should be obtained.
5383 * @return User ID to be resolved. {@link UserHandle#CURRENT} if not specified.
5384 */
5385 @BinderThread
5386 @UserIdInt
5387 private static int handleOptionsForCommandsThatOnlyHaveUserOption(ShellCommand shellCommand) {
5388 while (true) {
5389 final String nextOption = shellCommand.getNextOption();
5390 if (nextOption == null) {
5391 break;
5392 }
5393 switch (nextOption) {
5394 case "-u":
5395 case "--user":
5396 return UserHandle.parseUserArg(shellCommand.getNextArgRequired());
5397 }
5398 }
5399 return UserHandle.USER_CURRENT;
5400 }
5401
5402 @BinderThread
5403 private void handleShellCommandEnableDisableInputMethodInternalLocked(
5404 @UserIdInt int userId, String imeId, boolean enabled, PrintWriter out,
5405 PrintWriter error) {
5406 boolean failedToEnableUnknownIme = false;
5407 boolean previouslyEnabled = false;
5408 if (userId == mSettings.getCurrentUserId()) {
5409 if (enabled && !mMethodMap.containsKey(imeId)) {
5410 failedToEnableUnknownIme = true;
5411 } else {
5412 previouslyEnabled = setInputMethodEnabledLocked(imeId, enabled);
5413 }
5414 } else {
5415 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
5416 final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
5417 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
5418 new ArrayMap<>();
5419 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
5420 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap,
5421 methodMap, methodList);
5422 final InputMethodSettings settings = new InputMethodSettings(mContext.getResources(),
5423 mContext.getContentResolver(), methodMap, userId, false);
5424 if (enabled) {
5425 if (!methodMap.containsKey(imeId)) {
5426 failedToEnableUnknownIme = true;
5427 } else {
5428 for (InputMethodInfo imi : settings.getEnabledInputMethodListLocked()) {
5429 if (TextUtils.equals(imi.getId(), imeId)) {
5430 previouslyEnabled = true;
5431 break;
5432 }
5433 }
5434 if (!previouslyEnabled) {
5435 settings.appendAndPutEnabledInputMethodLocked(imeId, false);
5436 }
5437 }
5438 } else {
5439 previouslyEnabled =
5440 settings.buildAndPutEnabledInputMethodsStrRemovingIdLocked(
5441 new StringBuilder(),
5442 settings.getEnabledInputMethodsAndSubtypeListLocked(), imeId);
5443 }
5444 }
5445 if (failedToEnableUnknownIme) {
5446 error.print("Unknown input method ");
5447 error.print(imeId);
5448 error.println(" cannot be enabled for user #" + userId);
5449 } else {
5450 out.print("Input method ");
5451 out.print(imeId);
5452 out.print(": ");
5453 out.print((enabled == previouslyEnabled) ? "already " : "now ");
5454 out.print(enabled ? "enabled" : "disabled");
5455 out.print(" for user #");
5456 out.println(userId);
5457 }
5458 }
5459
5460 /**
Yohei Yukawa926488d2017-12-11 17:24:55 -08005461 * Handles {@code adb shell ime set}.
5462 * @param shellCommand {@link ShellCommand} object that is handling this command.
5463 * @return Exit code of the command.
5464 */
5465 @BinderThread
5466 @ShellCommandResult
5467 private int handleShellCommandSetInputMethod(@NonNull ShellCommand shellCommand) {
Yohei Yukawa3403f2d2019-04-15 08:16:58 -07005468 final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005469 final String imeId = shellCommand.getNextArgRequired();
5470 final PrintWriter out = shellCommand.getOutPrintWriter();
5471 final PrintWriter error = shellCommand.getErrPrintWriter();
Yohei Yukawadb25df72018-12-27 08:40:41 -08005472 synchronized (mMethodMap) {
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005473 final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
5474 mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
5475 for (int userId : userIds) {
5476 if (!userHasDebugPriv(userId, shellCommand)) {
5477 continue;
5478 }
mincheli850892b2019-12-05 19:47:59 +08005479 boolean failedToSelectUnknownIme = !switchToInputMethod(imeId, userId);
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005480 if (failedToSelectUnknownIme) {
5481 error.print("Unknown input method ");
5482 error.print(imeId);
5483 error.print(" cannot be selected for user #");
5484 error.println(userId);
5485 } else {
5486 out.print("Input method ");
5487 out.print(imeId);
5488 out.print(" selected for user #");
Yohei Yukawa3403f2d2019-04-15 08:16:58 -07005489 out.println(userId);
Yohei Yukawa099f80c2019-04-10 23:23:17 -07005490 }
Tarandeep Singh87e2e512019-03-06 14:28:52 -08005491 }
Yohei Yukawadb25df72018-12-27 08:40:41 -08005492 }
Yohei Yukawa926488d2017-12-11 17:24:55 -08005493 return ShellCommandResult.SUCCESS;
5494 }
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005495
5496 /**
5497 * Handles {@code adb shell ime reset-ime}.
5498 * @param shellCommand {@link ShellCommand} object that is handling this command.
5499 * @return Exit code of the command.
5500 */
5501 @BinderThread
5502 @ShellCommandResult
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005503 private int handleShellCommandResetInputMethod(@NonNull ShellCommand shellCommand) {
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005504 final PrintWriter out = shellCommand.getOutPrintWriter();
5505 final int userIdToBeResolved = handleOptionsForCommandsThatOnlyHaveUserOption(shellCommand);
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005506 synchronized (mMethodMap) {
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005507 final int[] userIds = InputMethodUtils.resolveUserId(userIdToBeResolved,
5508 mSettings.getCurrentUserId(), shellCommand.getErrPrintWriter());
5509 for (int userId : userIds) {
5510 if (!userHasDebugPriv(userId, shellCommand)) {
5511 continue;
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005512 }
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005513 final String nextIme;
5514 final List<InputMethodInfo> nextEnabledImes;
5515 if (userId == mSettings.getCurrentUserId()) {
Tarandeep Singh4fe5b652020-02-20 17:20:19 -08005516 hideCurrentInputLocked(mCurFocusedWindow, 0, null,
lumarkd85e1582019-12-29 20:20:41 +08005517 SoftInputShowHideReason.HIDE_RESET_SHELL_COMMAND);
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005518 unbindCurrentMethodLocked();
5519 // Reset the current IME
5520 resetSelectedInputMethodAndSubtypeLocked(null);
5521 // Also reset the settings of the current IME
5522 mSettings.putSelectedInputMethod(null);
5523 // Disable all enabled IMEs.
5524 mSettings.getEnabledInputMethodListLocked().forEach(
5525 imi -> setInputMethodEnabledLocked(imi.getId(), false));
5526 // Re-enable with default enabled IMEs.
5527 InputMethodUtils.getDefaultEnabledImes(mContext, mMethodList).forEach(
5528 imi -> setInputMethodEnabledLocked(imi.getId(), true));
5529 updateInputMethodsFromSettingsLocked(true /* enabledMayChange */);
5530 InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager,
5531 mSettings.getEnabledInputMethodListLocked(),
5532 mSettings.getCurrentUserId(),
5533 mContext.getBasePackageName());
5534 nextIme = mSettings.getSelectedInputMethod();
5535 nextEnabledImes = mSettings.getEnabledInputMethodListLocked();
5536 } else {
5537 final ArrayMap<String, InputMethodInfo> methodMap = new ArrayMap<>();
5538 final ArrayList<InputMethodInfo> methodList = new ArrayList<>();
5539 final ArrayMap<String, List<InputMethodSubtype>> additionalSubtypeMap =
5540 new ArrayMap<>();
5541 AdditionalSubtypeUtils.load(additionalSubtypeMap, userId);
5542 queryInputMethodServicesInternal(mContext, userId, additionalSubtypeMap,
5543 methodMap, methodList);
5544 final InputMethodSettings settings = new InputMethodSettings(
5545 mContext.getResources(), mContext.getContentResolver(), methodMap,
5546 userId, false);
5547
5548 nextEnabledImes = InputMethodUtils.getDefaultEnabledImes(mContext, methodList);
5549 nextIme = InputMethodUtils.getMostApplicableDefaultIME(nextEnabledImes).getId();
5550
5551 // Reset enabled IMEs.
5552 settings.putEnabledInputMethodsStr("");
5553 nextEnabledImes.forEach(imi -> settings.appendAndPutEnabledInputMethodLocked(
5554 imi.getId(), false));
5555
5556 // Reset selected IME.
5557 settings.putSelectedInputMethod(nextIme);
5558 settings.putSelectedSubtype(NOT_A_SUBTYPE_ID);
5559 }
5560 out.println("Reset current and enabled IMEs for user #" + userId);
5561 out.println(" Selected: " + nextIme);
5562 nextEnabledImes.forEach(ime -> out.println(" Enabled: " + ime.getId()));
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005563 }
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005564 }
Yohei Yukawa2d4b3432019-04-10 23:23:25 -07005565 return ShellCommandResult.SUCCESS;
Yohei Yukawacc97ebd2017-12-11 17:54:43 -08005566 }
Yohei Yukawac54c1172018-09-06 11:39:50 -07005567
Tarandeep Singh87e2e512019-03-06 14:28:52 -08005568 /**
5569 * @param userId the actual user handle obtained by {@link UserHandle#getIdentifier()}
5570 * and *not* pseudo ids like {@link UserHandle#USER_ALL etc}.
5571 * @return {@code true} if userId has debugging privileges.
5572 * i.e. {@link UserManager#DISALLOW_DEBUGGING_FEATURES} is {@code false}.
5573 */
5574 private boolean userHasDebugPriv(int userId, final ShellCommand shellCommand) {
5575 if (mUserManager.hasUserRestriction(
5576 UserManager.DISALLOW_DEBUGGING_FEATURES, UserHandle.of(userId))) {
5577 shellCommand.getErrPrintWriter().println("User #" + userId
5578 + " is restricted with DISALLOW_DEBUGGING_FEATURES.");
5579 return false;
5580 }
5581 return true;
5582 }
5583
Yohei Yukawac54c1172018-09-06 11:39:50 -07005584 private static final class InputMethodPrivilegedOperationsImpl
5585 extends IInputMethodPrivilegedOperations.Stub {
5586 private final InputMethodManagerService mImms;
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08005587 @NonNull
Yohei Yukawac54c1172018-09-06 11:39:50 -07005588 private final IBinder mToken;
Yohei Yukawad3e0fd932019-01-21 09:16:48 -08005589 InputMethodPrivilegedOperationsImpl(InputMethodManagerService imms,
5590 @NonNull IBinder token) {
Yohei Yukawac54c1172018-09-06 11:39:50 -07005591 mImms = imms;
5592 mToken = token;
5593 }
5594
5595 @BinderThread
5596 @Override
5597 public void setImeWindowStatus(int vis, int backDisposition) {
5598 mImms.setImeWindowStatus(mToken, vis, backDisposition);
5599 }
5600
5601 @BinderThread
5602 @Override
5603 public void reportStartInput(IBinder startInputToken) {
5604 mImms.reportStartInput(mToken, startInputToken);
5605 }
5606
5607 @BinderThread
5608 @Override
Yohei Yukawac54c1172018-09-06 11:39:50 -07005609 public IInputContentUriToken createInputContentUriToken(Uri contentUri,
5610 String packageName) {
5611 return mImms.createInputContentUriToken(mToken, contentUri, packageName);
5612 }
5613
5614 @BinderThread
5615 @Override
5616 public void reportFullscreenMode(boolean fullscreen) {
5617 mImms.reportFullscreenMode(mToken, fullscreen);
5618 }
Yohei Yukawac7ca3682018-09-09 20:48:38 -07005619
5620 @BinderThread
5621 @Override
5622 public void setInputMethod(String id) {
5623 mImms.setInputMethod(mToken, id);
5624 }
5625
5626 @BinderThread
5627 @Override
5628 public void setInputMethodAndSubtype(String id, InputMethodSubtype subtype) {
5629 mImms.setInputMethodAndSubtype(mToken, id, subtype);
5630 }
5631
5632 @BinderThread
5633 @Override
5634 public void hideMySoftInput(int flags) {
5635 mImms.hideMySoftInput(mToken, flags);
5636 }
5637
5638 @BinderThread
5639 @Override
5640 public void showMySoftInput(int flags) {
5641 mImms.showMySoftInput(mToken, flags);
5642 }
5643
5644 @BinderThread
5645 @Override
Yohei Yukawa41b094f2018-09-09 23:58:45 -07005646 public void updateStatusIcon(String packageName, @DrawableRes int iconId) {
Yohei Yukawac7ca3682018-09-09 20:48:38 -07005647 mImms.updateStatusIcon(mToken, packageName, iconId);
5648 }
5649
5650 @BinderThread
5651 @Override
5652 public boolean switchToPreviousInputMethod() {
5653 return mImms.switchToPreviousInputMethod(mToken);
5654 }
5655
5656 @BinderThread
5657 @Override
5658 public boolean switchToNextInputMethod(boolean onlyCurrentIme) {
5659 return mImms.switchToNextInputMethod(mToken, onlyCurrentIme);
5660 }
5661
5662 @BinderThread
5663 @Override
5664 public boolean shouldOfferSwitchingToNextInputMethod() {
5665 return mImms.shouldOfferSwitchingToNextInputMethod(mToken);
5666 }
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07005667
5668 @BinderThread
5669 @Override
Yohei Yukawa9b60ba02019-01-21 00:06:27 -08005670 public void notifyUserAction() {
Yohei Yukawac07fd4c2018-09-11 11:37:13 -07005671 mImms.notifyUserAction(mToken);
5672 }
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08005673
5674 @BinderThread
5675 @Override
5676 public void reportPreRendered(EditorInfo info) {
5677 mImms.reportPreRendered(mToken, info);
5678 }
5679
5680 @BinderThread
5681 @Override
Tarandeep Singhbb0e2f72020-01-15 13:58:29 -08005682 public void applyImeVisibility(IBinder windowToken, boolean setVisible) {
5683 mImms.applyImeVisibility(mToken, windowToken, setVisible);
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08005684 }
Yohei Yukawac54c1172018-09-06 11:39:50 -07005685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686}